# UTM Cleanup Exclusion Fix - Implementation Summary

**Last Updated:** 2026-01-29

## Problem Solved

UTM cleanup was still happening on excluded pages (`/v3`, `/gastro`, `/schichtbetriebe`) despite exclusion logic in the UTM tracker.

## Root Cause

The `pricing-page.js` script contains URL cleaning logic that runs immediately on page load (via IIFE) and removes ALL URL parameters, including UTMs, BEFORE the UTM tracker can initialize. This affected:

- `/gastro` (paid_nonbrand.php) - includes pricing-page.min.js
- `/schichtbetriebe` (paid_schichtbetriebe.php) - includes pricing-page.min.js
- `/v3` (landingpage_v3.php) - does NOT include pricing-page.js (no interference)

## Solution Implemented

### 1. Fixed pricing-page.js

**File:** `v2/js/pricing-page.js`

**Changes:**

- Added exclusion check in IIFE (lines 386-406) before URL cleaning
- Added exclusion check in comparison table initialization (lines 2545-2549)
- Console logging for debugging

**Code:**

```javascript
// Clean URL on page load if it has parameters (but NOT on excluded pages)
(function () {
  // Check if page is excluded from URL cleaning
  const excludedPages = ["/v3", "/gastro", "/schichtbetriebe"];
  const currentPath = window.location.pathname;
  const isPageExcluded = excludedPages.some(
    (path) => currentPath === path || currentPath.startsWith(path + "/"),
  );

  // Skip URL cleaning on excluded pages
  if (isPageExcluded) {
    console.log(
      "[Pricing Page] URL cleaning skipped - Excluded page:",
      currentPath,
    );
    return;
  }

  // Only clean URL if it has parameters AND page is not excluded
  if (window.location.search) {
    const cleanUrl = new URL(window.location.origin + window.location.pathname);
    window.history.replaceState({}, "", cleanUrl.toString());
  }
})();
```

### 2. Regenerated Minified File

**Action:** Ran `npm run minify` to regenerate `pricing-page.min.js`

**Result:**

- ✓ pricing-page.js → pricing-page.min.js (86.1 KB → 35.7 KB, 58.5% smaller)
- Exclusion check included in minified file

### 3. Verified No Other Scripts Interfere

**Checked:**

- No `history.replaceState` calls in paid_nonbrand.php
- No `history.replaceState` calls in paid_schichtbetriebe.php
- No `history.replaceState` calls in landingpage_v3.php
- No Alpine.js components modifying URLs on these pages

**Result:** pricing-page.js was the only interfering script

### 4. Updated Documentation

**Files Updated:**

1. `docs/development/UTM_CLEANUP_PAGE_EXCLUSIONS.md`
   - Added section on pricing-page.js interference
   - Updated troubleshooting section
   - Added detection steps

2. `docs/development/UTM_CLEANUP_TROUBLESHOOTING.md`
   - Added Issue 6: Script Interference section
   - Added diagnosis and solutions

3. `docs/development/UTM_CLEANUP_SCRIPT_INTERFERENCE.md` (NEW)
   - Comprehensive documentation of the issue
   - Root cause analysis
   - Solution details
   - Prevention best practices
   - Testing procedures

## Testing Required

### Manual Browser Testing

**Test URLs:**

1. `http://localhost:8003/gastro?utm_source=test&utm_campaign=test&utm_debug=true`
2. `http://localhost:8003/schichtbetriebe?utm_source=test&utm_campaign=test&utm_debug=true`
3. `http://localhost:8003/v3?utm_source=test&utm_campaign=test&utm_debug=true`

**Expected Results:**

- ✓ UTMs remain in URL on all 3 pages
- ✓ Console shows: `[Pricing Page] URL cleaning skipped - Excluded page: /gastro`
- ✓ Console shows: `[UTM Cleanup] Cleanup DISABLED via excluded page`
- ✓ No `utmCleanupComplete` events fired
- ✓ Analytics (GTM/GA4/HubSpot) still captures UTMs
- ✓ Form tracking still works (uses cookies/instance variables)

**Verification Steps:**

1. Open browser DevTools Console
2. Navigate to test URLs
3. Check console for exclusion messages
4. Verify UTMs remain in address bar
5. Check Network tab for analytics requests with UTMs
6. Verify no `utmCleanupComplete` events

### Test Scripts Available

1. **Pricing Page Exclusion Test:**

   ```
   /v2/scripts/dev-helpers/test-pricing-page-exclusion.php?utm_source=test&utm_campaign=test
   ```

2. **UTM Cleanup Exclusion Test:**
   ```
   /v2/scripts/dev-helpers/test-page-exclusions.php?utm_source=test&utm_campaign=test&utm_debug=true
   ```

## Files Changed

### Code Files

- `v2/js/pricing-page.js` - Added exclusion checks (2 locations)
- `v2/js/pricing-page.min.js` - Regenerated with exclusion checks

### Documentation Files

- `docs/development/UTM_CLEANUP_PAGE_EXCLUSIONS.md` - Updated with interference info
- `docs/development/UTM_CLEANUP_TROUBLESHOOTING.md` - Added script interference section
- `docs/development/UTM_CLEANUP_SCRIPT_INTERFERENCE.md` - New comprehensive guide
- `docs/development/UTM_CLEANUP_EXCLUSION_FIX_SUMMARY.md` - This file

### Test Scripts

- `v2/scripts/dev-helpers/test-pricing-page-exclusion.php` - New test script

## Success Criteria

- [x] pricing-page.js modified with exclusion check
- [x] pricing-page.min.js regenerated
- [x] No other interfering scripts found
- [x] Documentation updated
- [ ] Manual browser testing completed (requires user testing)
- [ ] UTMs preserved on /gastro (requires user verification)
- [ ] UTMs preserved on /schichtbetriebe (requires user verification)
- [ ] UTMs preserved on /v3 (requires user verification)
- [ ] Analytics capture verified (requires user verification)
- [ ] Form tracking verified (requires user verification)

## Next Steps

1. **Manual Testing:** Test all 3 excluded pages with UTMs
2. **Verify Console Logs:** Check for exclusion messages
3. **Verify Analytics:** Confirm GTM/GA4/HubSpot capture UTMs
4. **Verify Forms:** Test form submission with UTM data
5. **Monitor:** Watch for any regressions on other pages

## Related Documentation

- `docs/development/UTM_CLEANUP_PAGE_EXCLUSIONS.md` - Page exclusion guide
- `docs/development/UTM_CLEANUP_SCRIPT_INTERFERENCE.md` - Script interference guide
- `docs/development/UTM_CLEANUP_TROUBLESHOOTING.md` - Troubleshooting guide
- `v2/js/pricing-page.js` - Fixed pricing page script
- `v2/js/utm-tracking.js` - UTM tracker with exclusion checks
