--- name: dif-setup-analytics description: Set up dif analytics in an app and prove events arrive. Installs the site analytics script for page views, wires sign-in, consent and CSP, plans and adds marketing and product events with dif.track and data-dif-event, checks every experiment metric is tracked, and runs the install check. Use when the user wants to set up, install, add or check dif analytics, page views, visitors, UTM, conversion or product events, tracking, a tracking plan or instrumentation, or when triage reports fix_instrumentation or a metric has no data. --- # Setting up dif analytics Use this skill to make a project send the right events to dif analytics, and to prove that they arrive. At the end: - The app sends page views in exactly one way: the script tag. An app that uses `@dif.sh/sdk` keeps it for flags, tracks and exposures. - `dif/config.yaml` still has the same key as before. - `dif/tracking-plan.yaml` lists the events the app sends. - Every metric in a draft or active experiment has a `dif.track()` call with the exact same name. - You have a table of events from `dif events`, or the exact reason nothing arrived. If `dif` is not on the PATH, use `npx @dif.sh/cli` in place of `dif`. Never use `npx dif`. If `dif site` is an unknown command, the CLI is too old. Tell the person to update it, and use the fallbacks in this skill. Add `--json` to every `dif site`, `dif events` and `dif auth` command. Branch on `ok` and `error`, never on `message`. ## Step 0. Preflight 1. Read `dif/config.yaml`. If it does not exist, the project has no dif workspace. Do not run `dif init` for analytics. Use the analytics-only path below. 2. Look at the `events:` block. - `mode: custom`: events go to the person's own handlers, not to dif analytics. Ask which they want. To switch, use the `dif-connect-cloud` skill, then come back here. To stay custom, do only Steps 4, 5 and 8, and skip item 3 below. - `mode: cloud` with no `key`: use the `dif-connect-cloud` skill first, then come back. 3. Run `dif auth status --json`. If `logged_in` is false, use Steps 1 to 3 of the `dif-connect-cloud` skill, then come back here. **Analytics-only path (no `dif/config.yaml`).** `dif site setup` and `dif site check` work without a workspace, but they need `--project `. `dif events` and `dif validate` need a workspace, so skip them. 1. Run `dif auth status --json`. If `logged_in` is false, run `dif auth login`, then come back here. 2. Run `dif site check --env live --json` with no `--project`. It fails with `needs_selection` and lists the login's projects. Ask the person which project, even if there is only one. Add `--project ` to every `dif site` command after that. 3. Do Steps 1, 2, 3, 6, 7 and 8. In Step 3, the SDK and `events.url` notes do not apply unless the app already uses `@dif.sh/sdk`. In Step 7, skip items 2 and 3 and use only `dif site check`. In Step 8, report the `dif site check` counts in place of the table. Do not write `dif/tracking-plan.yaml`. ## Step 1. Read the app Find these before you change anything: - The framework and how pages render. Examples: Next.js (app or pages router), SvelteKit, Astro, React SPA, plain HTML. - The root layout or the file that writes ``. - The router: history (`pushState`) or hash (`#/route`). - Where the signed-in user id is known, on the server and in the browser, and where sign-out happens. - A consent banner or CMP (OneTrust, Cookiebot, Klaro, a custom banner). - A Content-Security-Policy: response headers, middleware, framework config or a `` tag. - Existing analytics (GA, PostHog, Segment, Plausible) and existing `dif.init`, `dif.track` and `data-dif-event` uses. - The production domains: deploy config, env files, README. ## Step 2. Turn on site analytics 1. Make the origin list. Use the exact scheme and host, and no path: `https://example.com`, `https://www.example.com`. Add the port only if it is not the default. At most 32 origins. Show the list to the person and ask them to confirm it. 2. Run: ```sh dif site setup --env live --origin https://example.com --origin https://www.example.com --json dif site setup --env test --origin http://localhost:5173 --json ``` Use the dev server's real port. `--env test` takes only local origins. 3. Use the key. The result has `key`, the full site key for that env. It goes only in the script tag's `data-key`. It is not the key in `dif/config.yaml` (`events.key`), and that is expected. The site key accepts only the origins on its list. The SDK sends tracks and exposures with the config key, so never change `dif/config.yaml`. - `--env live`: `key` is public. Put it in `data-key`, and commit it. - `--env test`: put `key` in a file only your dev server reads, for example `.env.development.local` for Vite or Next.js. A production build never reads that file. First run `git check-ignore `. If it prints nothing, add the file to `.gitignore` and tell the person. Use the key for `data-key` locally. Never commit it. - Only when `key` is `null`: ask the person to copy the key for that env from Settings → Site in dif analytics. Use the key they give you the same way. - Never guess a key or build one. 4. On `needs_selection`, ask the person which project, then run again with `--project `. With no `dif/` workspace, every `dif site` command needs `--project `. 5. Fallbacks: - `not_logged_in` or `token_invalid`: use Steps 1 to 3 of the `dif-connect-cloud` skill, then come back and run the same command again. - `missing_scope`: the login is from before this feature. Run `dif auth status --json`. If `source` is `env`, the token comes from `DIF_TOKEN`: ask the person to replace it with a new token, and stop. Otherwise do Steps 2 and 3 of `dif-connect-cloud` (skip Step 1), then come back and run the same command again. Try again at most once. - `forbidden`: this login lacks the role or scope. Tell the person, and stop this step. - `project_not_found`: the slug is wrong, or this login cannot see the project. Ask the person for the right slug. - `usage` or `bad_request`: an origin is not valid, or a test origin is not local. Fix the list and run again. - `not_project_manager`: tell the person an owner or admin must do this. Give them the origin list. - `site_analytics_unavailable`: site analytics is not on for this project yet. Skip Step 3 and the site part of Step 7. Product events still work. - `rate_limited`: wait `retry_after` seconds, then run the same command once more. If it fails again, tell the person. - `cloud_unreachable`: dif analytics cannot be reached. Check the network. Say the command could not run; never report zero. - `dif site` is unknown: tell the person to open Settings → Site in dif analytics, turn it on and paste the origin list. Wait until they say it is done. Never pass `--replace` unless the person asks to remove origins. Never pass `--rate-limit` unless the person asks. ## Step 3. Install page views Every app gets page views from the script tag in ``. Follow `references/install-paths.md`. If the app uses `@dif.sh/sdk`, keep it for flags, tracks and exposures. Add the script tag, and leave the SDK's `pageViews` off. If the code has `pageViews: "auto"`, remove it. Page views from both would count twice. Then: - Set `data-domains` to the production hostnames, so preview deploys send nothing. - Use `data-spa="hash"` only for a hash router. - Signed-in users: when the server knows the user, render `data-user-id` on the server. Use this when you can: the script sends the first page view as it starts, and only `data-user-id` is there in time. Otherwise, call `window.dif?.identify(id)` from code that runs after the script loads, such as a `load` listener or the app's sign-in callback. Never call it from inline code in ``: `window.dif` does not exist yet, and the script keeps no queue of early calls. The id is not stored, so call it on every page load. At sign-out, call `window.dif?.identify(null)` the same way. Do not use `dif.identify` from `@dif.sh/sdk` for page views: the script does not see it, and it sends traits with the config key. - Keys: the live `key` (`dif_pk_live_…`) in production and the test `key` (`dif_pk_test_…`) locally. Use the `key` from Step 2, item 3. The live key may be committed. Keep the test key out of committed files. Use the framework's env files to pick the key: for example `VITE_DIF_SITE_KEY` (or `NEXT_PUBLIC_DIF_SITE_KEY`) in a committed `.env.production` and in `.env.development.local`. - Consent: if there is a CMP, add `data-consent="required"` to the script tag. Right after the page loads, call `window.dif.consent()` with the CMP's current state, `true` or `false`. Also call it from the CMP's change callback. If the app uses the SDK, also add `consent: "required"` to `dif.init` and call the SDK's `dif.consent()` the same way. If there is no CMP, do not add consent. Say so in Step 8. - CSP: if a policy exists, add `https://cdn.dif.sh` to `script-src` and `https://ingest.difdb.com` to `connect-src`. If the app sends product tracks with a user id, also add the `events.url` origin from `dif/config.yaml` to `connect-src`. With no `events.url`, it is `https://cloud.dif.sh`. If there is no policy, change nothing. - SRI: only if the person asks to pin the version, or the repo already pins other third-party scripts. ## Step 4. Plan the events Make the tracking plan before you edit code. Use these sources, in this order: 1. **Required:** every `metrics.primary` and `metrics.guardrails` name in `dif/experiments/active/*.md` with `status: draft` or `active`. A name with whitespace or control characters, a leading `$`, or over 128 bytes cannot go in the plan (E012). Leave it out, tell the person, and expect W006 for it. Never edit the experiment. 2. Existing `dif.track()` calls and `data-dif-event` attributes. 3. Marketing events from the pages: the main calls to action (sign up, start trial, book a demo, contact), pricing, and links into the app. 4. Product events from the routes and handlers: account created, the first key action, the core actions, upgrade or checkout, invite. Follow `references/event-naming.md`. Keep the plan to about 5 to 15 events. Ask before you add more. Show the plan as a table: name, kind (marketing or product), where it fires, props, and the experiment that needs it. When the person says yes, write `dif/tracking-plan.yaml`. In autonomous mode, write it without asking; the PR is the review. ```yaml # Events this app sends to dif analytics. # dif validate checks experiment metrics against this list. events: - name: signup_completed kind: product where: src/routes/signup/+page.server.ts props: [plan, source] - name: pricing_cta_clicked kind: marketing where: src/routes/pricing/+page.svelte (data-dif-event) props: [plan] ``` Then run `dif validate`. `W006` means an experiment metric is missing from the plan. Fix the plan, or ask the person about the spelling. A W006 whose help says the name cannot go in the plan is expected; leave it. ## Step 5. Add the events - Marketing clicks: use attributes, not JavaScript. ```html Start free ``` - Without the SDK, the call in JavaScript is `window.dif.track(name, { plan: "pro" })`. The props are the second argument. This path has no `value` or `currency`. - With the SDK, call `dif.track(name, { props: { plan: "pro" }, value, currency })`. A track with no user id is dropped unless the init has `anonymous: true`. For events from visitors who are not signed in, set `dif.init({ anonymous: true })`. - Product events: call `dif.track()` where the action succeeds, after the server confirms it, not when the button is pressed. For a server-side success, use `DifServer` from `@dif.sh/sdk/server`. - Money (SDK only): pass `value` and `currency`, never a prop. - One call per event. Do not add a wrapper module. If the repo already has an analytics wrapper, add the call there. - Never rename a metric in an experiment file. Rename the `dif.track()` call, or ask. Then run the repo's typecheck and tests, and `dif validate`. ## Step 6. Test locally Do this only when the app can run here. Start the dev server with the test `key` from Step 2 (or the one the person gave you if it was `null`). Open one page, and click one call to action. Then: ```sh dif site check --env test --wait 60 --json ``` ## Step 7. Check that events arrive 1. When the change is live, run `dif site check --env live --wait 60 --json`. Before that, use the Step 6 result. 2. Run `dif events --since 24h --plan --json`. It lists live events only. Events sent with the test key show only in `dif site check --env test`. Each row has `in_plan`, and `missing` lists planned events with no count. If it fails with `cloud_error` or `cloud_unreachable`, the counts cannot be read right now. Say so; do not report zero. If it fails with `no_tracking_plan`, go back to Step 4. 3. Show a table: event, in plan, count in the last 24 hours, last seen. 4. For every refusal or missing event, use `references/troubleshooting.md`. On any other error, do what Step 2 says for that code. Read `status` in the `dif site check` result first: - `seen`: use the counts. - `unknown_key`: dif analytics has not seen this key yet. The page is not live, the request never left the browser, or the page sends another key. Use the part of `references/troubleshooting.md` that starts "No refusals and `accepted_last_hour` is 0, or `status` is `unknown_key`". - `unavailable` or `error`: the check cannot run right now. Say so; do not report zero. - Error `site_not_set_up`: go back to Step 2 for that env. Counts are `null` unless `status` is `seen`. When `status` is not `seen`, `refused_last_hour` is four zeros, and those zeros mean nothing. Never read `null` as zero. Report only what these commands return. If the change is not deployed, say "not checked yet: deploy, then run `dif site check --env live`". Never say events arrive when no command showed it. If `dif site check` is unknown, ask the person to press "Check live install" in Settings → Site and paste the result. ## Step 8. Hand back Tell the person: - What you changed, by file. - The table from Step 7. - What is left for them: deploy, a consent decision if there is no CMP, and any dashboard step from Step 2. If there is no CMP, add: "dif analytics uses no cookies, but reading screen size and language may still need consent under ePrivacy rules in the EU. Decide whether you need a consent banner. If you add one, I can wire `dif.consent()` to it." ### Autonomous mode (from `dif-triage-experiments`) You get one metric name. Do only Steps 4, 5 and 8 for that metric. Do not stop to ask: write the plan and the code, and let the PR be the review. - The metric must match `^[a-z0-9][a-z0-9_]{0,63}$`. If it does not, report `Skipped: unsafe metric` and stop. - If the code already has a `dif.track()` call with this exact name, change nothing. Report `Instrumented but not seen in 7d` and open no branch. - If you create `dif/tracking-plan.yaml`, add every required metric from Step 4 that passes the name rule, not only this one. If a required metric cannot go in the plan, list its expected W006 in the PR body. - Use one branch, `dif/instrument/`, cut from `origin/`. Follow the branch rules in the triage skill. - Open one PR. Do not deploy, and do not change dif analytics settings. ## Rules - Never print, ask for or write a `dif_cli_` or `dif_pat_` token. - Never add a second page-view path to a page that has one. - Never run `dif connect --key` with a site key, and never put a site key in dif/config.yaml. - Never proxy `ingest.difdb.com` through the app, and never set `data-host` outside a staging test. - Never send emails, names, free text or ids of other people in props. - Never track every keystroke, scroll or mouse move. - Ask before you add events beyond the required ones, add a consent integration, or change a production CSP.