# UTM Cleanup Rollback Plan

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

## Rollback Procedures

### Quick Rollback (Feature Flag)

**Method:** Use feature flag to disable cleanup immediately

**Steps:**
1. Open browser console
2. Run: `localStorage.setItem('utmCleanupEnabled', 'false')`
3. Reload page
4. Verify cleanup disabled (UTMs remain in URL)

**For All Users:**
- Update `utm-cleanup-feature-flag.js` default to `false`
- Deploy change
- Monitor impact

**Time to Rollback:** < 5 minutes

### Partial Rollback (Revert Specific Changes)

**Method:** Revert specific improvements while keeping core cleanup

**Steps:**
1. Identify problematic change
2. Revert specific commit/file
3. Test functionality
4. Deploy fix

**Time to Rollback:** 15-30 minutes

### Full Rollback (Complete Reversion)

**Method:** Revert all cleanup-related changes

**Steps:**
1. Identify last known good commit
2. Revert cleanup-related files:
   - `v2/js/utm-tracking.js`
   - `v2/base/include_form-hs.php`
   - `html/form-hs.php`
   - `v2/base/head.php`
3. Run `npm run minify`
4. Test thoroughly
5. Deploy

**Time to Rollback:** 1-2 hours

## Rollback Scripts

### Script 1: Disable Cleanup via Feature Flag

```javascript
// Run in browser console or inject via script
(function() {
    try {
        localStorage.setItem('utmCleanupEnabled', 'false');
        console.log('UTM cleanup disabled via feature flag');
        // Reload page to apply
        window.location.reload();
    } catch (e) {
        console.error('Failed to disable cleanup:', e);
    }
})();
```

### Script 2: Enable Cleanup via Feature Flag

```javascript
// Run in browser console or inject via script
(function() {
    try {
        localStorage.setItem('utmCleanupEnabled', 'true');
        console.log('UTM cleanup enabled via feature flag');
        // Reload page to apply
        window.location.reload();
    } catch (e) {
        console.error('Failed to enable cleanup:', e);
    }
})();
```

### Script 3: Check Cleanup Status

```javascript
// Run in browser console
(function() {
    const status = {
        featureFlag: localStorage.getItem('utmCleanupEnabled'),
        trackerExists: typeof window.utmTracker !== 'undefined',
        cleanupMethodExists: typeof window.utmTracker?.setupUTMCleanup === 'function',
        urlHasUTMs: (() => {
            const params = new URLSearchParams(window.location.search);
            return ['utm_source', 'utm_medium', 'utm_campaign'].some(p => params.has(p));
        })()
    };
    console.table(status);
    return status;
})();
```

## Rollback Triggers

### Critical Triggers (Immediate Rollback)

- Analytics capture failures > 10%
- Form tracking failures > 10%
- Data loss incidents
- Critical bugs affecting tracking

### Warning Triggers (Investigate First)

- Analytics capture failures 5-10%
- Form tracking failures 5-10%
- Performance degradation > 20%
- User complaints about tracking

### Monitoring Triggers (Monitor Closely)

- Analytics capture failures < 5%
- Form tracking failures < 5%
- Performance degradation < 20%
- Edge case issues

## Rollback Checklist

### Before Rollback

- [ ] Identify issue/trigger
- [ ] Document problem
- [ ] Assess impact
- [ ] Choose rollback method
- [ ] Notify team

### During Rollback

- [ ] Execute rollback procedure
- [ ] Verify rollback successful
- [ ] Test critical functionality
- [ ] Monitor metrics
- [ ] Document rollback

### After Rollback

- [ ] Verify issue resolved
- [ ] Monitor for 24-48 hours
- [ ] Document root cause
- [ ] Plan fix/improvement
- [ ] Update documentation

## Rollback Testing

### Test Rollback Procedure

1. **Enable Feature Flag:**
   ```javascript
   localStorage.setItem('utmCleanupEnabled', 'true');
   ```

2. **Visit Test URL:**
   ```
   http://localhost:8003/schichtbetriebe?utm_source=test&utm_campaign=test&utm_debug=true
   ```

3. **Verify Cleanup Works:**
   - Check console logs
   - Verify UTMs removed after 1.5s
   - Verify form tracking works

4. **Disable Feature Flag:**
   ```javascript
   localStorage.setItem('utmCleanupEnabled', 'false');
   ```

5. **Reload Page:**
   - Verify UTMs remain in URL
   - Verify form tracking still works
   - Verify analytics capture works

6. **Re-enable Feature Flag:**
   ```javascript
   localStorage.setItem('utmCleanupEnabled', 'true');
   ```

7. **Verify Rollback Works:**
   - Cleanup should work again
   - All functionality restored

## Rollback Documentation

### Document Rollback

When rolling back, document:

1. **Issue:**
   - What went wrong?
   - When did it happen?
   - What was the impact?

2. **Rollback:**
   - What method was used?
   - When was it executed?
   - Who executed it?

3. **Verification:**
   - How was it verified?
   - What tests were run?
   - What metrics were checked?

4. **Root Cause:**
   - Why did it happen?
   - What was the root cause?
   - How can it be prevented?

5. **Next Steps:**
   - What needs to be fixed?
   - What improvements needed?
   - Timeline for fix?

## Emergency Contacts

**Primary:** Hady Elhady (hady@ordio.com)
**Backup:** Development Team

## Related Documentation

- `docs/analysis/UTM_CLEANUP_FINAL_RECOMMENDATION.md` - Final recommendation
- `docs/development/UTM_TRACKING_DEBUGGING.md` - Debugging guide
- `v2/scripts/dev-helpers/test-cleanup-behavior.php` - Test script
