#!/usr/bin/env python3 """ Add Umami tracking script to all public HTML files Excludes admin and koha directories """ import os import re from pathlib import Path # Tracking script to insert TRACKING_SCRIPT = ''' ''' def add_tracking_to_file(filepath): """Add tracking script before tag if not already present""" try: with open(filepath, 'r', encoding='utf-8') as f: content = f.read() # Check if tracking already exists if 'umami-tracker.js' in content: return None, 'Already has tracking' # Check if exists if '' not in content: return None, 'No tag found' # Insert tracking script before updated_content = content.replace('', TRACKING_SCRIPT + '', 1) # Write back to file with open(filepath, 'w', encoding='utf-8') as f: f.write(updated_content) return True, 'Tracking added' except Exception as e: return False, f'Error: {str(e)}' def main(): # Base directory public_dir = Path('public') # Find all HTML files html_files = [] for root, dirs, files in os.walk(public_dir): # Skip admin and koha directories if 'admin' in root or 'koha' in root: continue for file in files: if file.endswith('.html'): html_files.append(Path(root) / file) html_files.sort() print("=" * 60) print(" Adding Umami Tracking to Public Pages") print("=" * 60) print() stats = {'added': 0, 'skipped': 0, 'errors': 0} for filepath in html_files: success, message = add_tracking_to_file(filepath) if success is True: print(f"✓ {filepath}: {message}") stats['added'] += 1 elif success is None: print(f"⊘ {filepath}: {message}") stats['skipped'] += 1 else: print(f"✗ {filepath}: {message}") stats['errors'] += 1 print() print("=" * 60) print(" Summary") print("=" * 60) print(f"Files updated: {stats['added']}") print(f"Files skipped: {stats['skipped']}") print(f"Errors: {stats['errors']}") print() print("NOTE: Update website ID in public/js/components/umami-tracker.js") print(" after completing Umami setup") print("=" * 60) if __name__ == '__main__': main()