# Adding a new client to the AdHome CDN + Consent Platform

This covers onboarding a new website (e.g. a new HVAC/electric/commercial client)
onto `https://cdn.adhome.me`.

## 0. Before you start

Decide, for this client:
- Does the site use GA4? Clarity? Google Ads? Microsoft UET? Meta Pixel?
- Does it use ProfitFill DNI? What consent category should DNI fall under —
  this is a judgment call based on how that client's ProfitFill account is
  configured and what it tracks. Don't default to "necessary" without
  confirming that's accurate for this client.
- Does it use Scheduler Pro? Does Scheduler Pro set cookies or load an
  iframe/script? What category should it fall under?
- Are tags managed through GTM (recommended default) or does this specific
  site need scripts loaded directly (`tagMode: "direct"`)?
- Does the client have a `/privacy-policy` page? Link it in the config.

## 1. Create the client configuration

Add a `<script>` block to the site (in `<head>`, before the CDN script tags),
defining `window.ConsentConfig`. Example:

```html
<script>
window.ConsentConfig = {
  client: "beluga-hvac",
  consentVersion: "1",
  privacyPolicyUrl: "https://belugahvac.com/privacy-policy",

  tagMode: "gtm",              // "gtm" (default) or "direct"
  gtm: { id: "GTM-XXXXXXX", autoload: true },

  analytics: {
    enabled: true,
    googleAnalytics: true,
    clarity: true
  },
  advertising: {
    enabled: false
  },
  functional: {
    enabled: false
  },

  ids: {
    ga4: "G-XXXXXXXXXX",       // only needed if tagMode: "direct"
    clarity: "xxxxxxxxxx"
  },

  services: {
    dni: {
      enabled: true,
      category: "analytics",   // confirm this per client, don't assume
      src: "https://scripts.profitfill.com/dni.js",
      accountId: "XXXXX"
    },
    scheduler: {
      enabled: true,
      category: "functional",
      src: "https://cdn.schedulerpro.example/widget.js"
    }
  }
};
</script>
```

**On WordPress specifically:** put this in the theme's `header.php` (or a
snippet plugin) directly above the CDN script tags below — not in the footer,
and not via a plugin that defers/delays JS, unless that plugin lets you
exclude this specific script by name.

## 2. Add the CDN script and CSS

Immediately after the config block:

```html
<script src="https://cdn.adhome.me/js/site-config.js"></script>
<script src="https://cdn.adhome.me/js/consent-manager.js"></script>
```

`site-config.js` is optional but recommended — it validates the config above
and prints console warnings for common mistakes (missing IDs, category typos,
a duplicate `consent-manager.js` tag elsewhere on the page, etc.) before the
real script runs.

The consent manager auto-injects `consent-manager.css`; you don't need to add
the stylesheet `<link>` yourself unless you set `autoInjectCss: false`.

**Check for exactly one `consent-manager.js` tag on the rendered page.**
Duplicate tags (one injected by a theme/plugin/page builder in addition to
your own) are the most common cause of "the config looks right but the
banner never shows" — the first copy to execute wins and locks in defaults.

## 3. Configure GTM

In Google Tag Manager for this container:
- Create a trigger (or use Consent Mode's built-in gating) so analytics/ads
  tags only fire once the relevant consent signal is granted.
- Listen for the `dataLayer` events this script pushes:
  - `consent_ready` — fires once on load with the current (stored or default) state
  - `consent_updated` — fires whenever the user changes their choice
  - Both include `consentAnalytics`, `consentAdvertising`, `consentFunctional` (booleans)
    and `consentClient` (this site's client id)
- Google Consent Mode v2 signals (`analytics_storage`, `ad_storage`,
  `ad_user_data`, `ad_personalization`) are set automatically by
  `consent-manager.js` via `gtag('consent', ...)` before GTM/GA4 tags fire —
  most GA4/Ads tags in GTM will respect this automatically once Consent Mode
  is enabled in GTM's container settings.

## 4. Configure analytics

- GA4: either let GTM fire it (default `tagMode: "gtm"`), or set
  `tagMode: "direct"` and supply `ids.ga4` to have `analytics.js` load it
  directly, gated on the `analytics` category.
- Clarity: same pattern via `ids.clarity`.

## 5. Configure advertising tags (if applicable)

Set `advertising.enabled: true` and supply whichever of `ids.googleAds`,
`ids.uet`, `ids.metaPixel` apply — either for GTM triggers to reference, or
for direct loading if `tagMode: "direct"`.

## 6. Configure DNI

Set `services.dni` as shown above. Confirm the real ProfitFill script URL
and account parameters for this client — `dni.js` will warn in the console
if `src` is missing. Confirm the consent category with whoever owns this
client's privacy posture; don't assume "necessary".

## 7. Configure Scheduler Pro

Set `services.scheduler` with the client's actual widget/script URL, and
decide its category the same way — does it set cookies, load an iframe, or
have no tracking at all? Adjust `category` (or `"necessary"` if it's genuinely
required for core site function) accordingly.

## 8. Test

Work through `TESTING-CHECKLIST.md` for this client before calling it done.
At minimum: first visit (banner appears), Accept All, Reject Optional,
Manage Preferences, withdrawal of consent, and confirm the right
network requests fire only after the matching category is granted.

## 9. Verify in GTM Preview + DevTools

- Use GTM's Preview mode to confirm tags fire only after consent, not before.
- In DevTools Network tab, reload with cache disabled and confirm no GA4,
  ads, Clarity, or DNI requests fire before you interact with the banner.

## Common issues

| Symptom | Likely cause |
|---|---|
| Banner never appears, no errors | No category has `enabled: true`, or a duplicate `consent-manager.js` tag loaded before your config was defined |
| `ConsentManager.config.client` shows `"unknown"` | Something loaded `consent-manager.js` before `window.ConsentConfig` was set — check for a second script tag, or a caching/optimization plugin reordering scripts |
| Config looks right in the page source but wrong in the console | A caching layer (WP Rocket, LiteSpeed, Cloudflare, browser cache) is serving stale HTML — hard refresh, purge all cache layers, check for a stray Service Worker |
| DNI/Scheduler never loads | `services.<name>.src` missing, or the category it's gated on is never granted in testing |
