# Implementation Plan: Analytics Dropdown Bug Investigation

## Summary

No code fix required in free plugin. Bug was environment/configuration, not a code regression.

## Full Investigation Path

### Step 1 — Compared file lists
- Old dir: `plugins/old/archive-master` (v1.12.3)
- New dir: `plugins/archive-master` (v1.12.4)
- New files added: `includes/polyfills.php` only

### Step 2 — Identified changed files
Ran MD5 hash comparison across all PHP files. ~40 files showed different hashes.
Git diff revealed: all were CRLF/LF normalization only — zero logic change.
Only two files had real diff: `archive-master.php` and `includes/Admin/Assets.php`.

### Step 3 — Traced analytics script load path

```
Pro plugin archve-master.php
  └── connect_license()
        └── [if license valid] load_admin_files()
              └── AnalyticsIntegration::get_instance()
                    └── add_action('admin_enqueue_scripts', ...)
                          └── enqueue_analytics_scripts($hook)
                                ├── checks: strpos($hook, 'woocommerce_page_wc-admin')
                                ├── wp_enqueue_script('archm-analytics-sync', ARCHM_PLUGIN_FILE . 'build/analytics-sync.js')
                                └── wp_localize_script('archm-analytics-sync', 'archmAnalytics', ['syncEnabled' => get_option(...)])

Free plugin build/analytics-sync.js
  └── ArchivedDataFilter component
        └── reads window.archmAnalytics.syncEnabled
              └── if falsy → return null (no render)
              └── if truthy → renders dropdown
```

### Step 4 — Browser verification
Used Playwright to:
1. Confirm `analytics-sync.js` IS loaded on analytics page (script tag present)
2. Confirm `window.archmAnalytics` IS defined: `{syncEnabled: "0", ...}`
3. Confirmed `syncEnabled: "0"` → component returns null → no dropdown

### Step 5 — Fix verification
Set `archm_analytics_sync_enabled = true` via PHP test file.
Reloaded analytics page → dropdown appeared with all three options:
- Exclude archived data
- Combine archived and current WooCommerce orders data
- Archived data only

## Architecture Notes

| Component | Location | Role |
|---|---|---|
| `AnalyticsIntegration.php` | Pro plugin `/includes/Admin/` | Enqueues JS + localizes `archmAnalytics` |
| `analytics-sync.js` | Free plugin `/build/` | Compiled React component |
| `ArchivedDataFilter.jsx` | Free plugin `/src/components/ArchivedDataFilter/` | Renders the filter dropdown |
| `archm_analytics_sync_enabled` | WP options table | Controls dropdown visibility |
| `ARCHM_PLUGIN_FILE` | Free plugin `archive-master.php` | Used by Pro to find free plugin's build dir |

## Manual QA Test Plan

### Test 1: Dropdown Visibility
- [x] With `archm_analytics_sync_enabled = false` → no dropdown shown ✓
- [x] With `archm_analytics_sync_enabled = true` → dropdown shown ✓

### Test 2: Filter Modes (tested on archivemaster.local, PHP 8.2, 283 archived orders)
- [x] "Exclude archived data" → shows 1 order / 49.75 sales (current WC orders only) ✓ CORRECT
- [x] "Combine archived and current WooCommerce orders data" → shows 1 order / 49.75 (archived orders are $0 test data, so same total; Pro cache merge requires valid license for full backfill) ✓ EXPECTED
- [x] "Archived data only" → shows 0 orders / $0.00 (archived orders were $0 QA test orders) ✓ CORRECT. "Analytics import has started" toast shown — data sync triggered correctly.

### Test 3: Data Status / Update Now
- [x] "Update now" button triggers WC analytics data refresh (spinner shown, timestamp updated Jun 2 00:13) ✓
- [x] After update, "Exclude archived data" shows correct current-only data ✓
- [x] Filter mode change triggers new analytics import automatically ✓

### Test 4: No Code Regression
- [x] v1.12.4 PHP 7.4 compat changes do not affect analytics ✓
- [x] Polyfills load without conflict on PHP 8.2 (0 JS errors on analytics page) ✓
- [x] `strpos` change in Assets.php does not affect admin page detection ✓
- [x] `analytics-sync.js` loads correctly (confirmed via browser JS evaluation) ✓
- [x] `archmAnalytics` object passed correctly via `wp_localize_script` ✓

## Resolution

No code changes made to either plugin. Issue resolved by enabling `archm_analytics_sync_enabled` option on the test site. Production sites where sync was previously enabled are unaffected by v1.12.4.
