# Jobcenteremote — Tracking & Speed Patch (May 2026)

## What changed

**4 files** — drop-in replacements / new files. Nothing else in your project needs to be touched.

| File | Status | What changed |
|---|---|---|
| `meta_config.php` | **NEW** | Central Pixel ID + CAPI access token + hashed-PII helpers. Single source of truth — no more pasting tokens in 3 places. |
| `partials/header.php` | **REPLACE** | Uses `meta_config.php`. Server-side PageView + ViewContent CAPI now have short timeouts (max 4s) so they don't block page loads. |
| `submit.php` | **REPLACE** | Sends redirect to user FIRST, then runs CAPI + Telegram in background. User no longer waits 5–20s. Also fires `Lead` + `SubmitApplication` server-side here (with hashed email/phone/name/address) for much higher Event Match Quality. |
| `success.php` | **REPLACE** | Only fires the browser pixel now (server CAPI moved to submit.php). Reads event_ids from URL so refreshes don't double-count. |

**Untouched** — keep your existing files: `index.php`, `pre_upload.php`, `telegram_config.php` (after you rotate the token!), `telegram_notify.php`, `about-us.php`, `cookie-policy.php`, `gdpr-compliance.php`, `privacy-policy.php`, `success-stories.php`, `terms-of-service.php`, `partials/footer.php`, `assets/`.

## Deployment steps

1. **Rotate your Telegram bot token** (it leaked in our chat). `@BotFather` → `/revoke` → pick your bot → paste the new token in `telegram_config.php` on your server.

2. **Generate your CAPI access token.** Events Manager → JOB APPLY DE → Settings → Conversions API → Generate access token. Copy the long `EAA…` string.

3. **Edit `meta_config.php`** locally, paste the token at line:
   ```php
   define('META_ACCESS_TOKEN', 'EAA...paste_here...');
   ```

4. **Upload the 4 files** to your server (preserving paths — `partials/header.php` goes inside the `partials/` folder).

5. **Test end-to-end** — see below.

## Testing checklist

### A) Test the speed fix

Submit a real test application from your phone on cellular data (worst case for upload speed). Time how long from clicking "Bewerbung abschicken" → seeing success.php. Should be under 3 seconds even with both ID images attached. Pre-fix it would be 10–20 seconds.

If still slow → your hosting probably doesn't support `fastcgi_finish_request()`. Run `php -i | grep -i sapi` on the server. If it says `cgi-fcgi` or `fpm` you're fine. If it says `apache2handler` (mod_php) the fallback flush is doing its best but isn't guaranteed — ask your host to switch you to PHP-FPM (most enable it for free).

### B) Test CAPI is firing

Events Manager → Test Events tab → copy the test event code (e.g. `TEST12345`).

Edit `meta_config.php` temporarily:
```php
define('META_TEST_EVENT_CODE', 'TEST12345');
```

Submit a real test form. Within ~30 seconds in the Test Events tab you should see:

- `PageView` — Browser ✅ Server ✅ (deduplicated)
- `ViewContent` — Browser ✅ Server ✅ (deduplicated)  
- `Lead` — Browser ✅ Server ✅ (deduplicated)
- `SubmitApplication` — Browser ✅ Server ✅ (deduplicated)

If you see "Browser only" — your CAPI token paste failed.
If you see "Server only" — your domain isn't verified yet, or fbevents.js is being blocked client-side (CAPI saves you anyway).

After confirming everything works: **set `META_TEST_EVENT_CODE` back to empty** (`''`). Test events do NOT count toward optimization.

### C) Confirm Event Match Quality

Events Manager → your dataset → Overview → click on `Lead` → Event Match Quality score. Aim for **6.0+** (you should hit 7–9 with the hashed PII). Anything under 5 means matching is weak.

## Troubleshooting

| Symptom | Likely cause | Fix |
|---|---|---|
| Still no conversions in Ads Manager after 24h | CAPI token still empty | Double-check `meta_config.php` line 28 |
| Test Events shows "Server only" repeatedly | Domain not verified for `.de` | Re-do DNS TXT verification |
| Submit still slow | Host uses mod_php, no FPM | Ask host to enable PHP-FPM |
| Lead count looks doubled in Ads Manager | event_ids not matching | Check both browser and server use the same ids — view-source on success.php to verify |
| Telegram never delivers | Bot token wrong / chat ID wrong / cron killing background PHP | Check error log for "Telegram" entries |

## Architecture diagram (so you remember why this works)

```
USER SUBMITS FORM
       │
       ▼
   submit.php
   ├─ Validate + save files + write JSON     (50ms)
   ├─ Generate Lead/Submit event_ids          (1ms)
   ├─ Send 302 redirect with event_ids in URL (1ms)
   ├─ fastcgi_finish_request() ───────────────┐
   │                                          │
   │                                          ▼
   │                                   USER SEES success.php INSTANTLY
   │                                   (browser pixel fires Lead w/ event_id)
   │
   ├─ [BACKGROUND] CAPI Lead with hashed PII  ←── deduped with browser pixel
   ├─ [BACKGROUND] CAPI SubmitApplication
   ├─ [BACKGROUND] Telegram sendMessage
   └─ [BACKGROUND] Telegram sendMediaGroup (slow upload of ID photos)
```
