/dif:dif-setup-analyticsfrom dif
Installs dif analytics, wires page views, sign-in, consent, and CSP, adds marketing/product events via dif.track and data-dif-event, validates experiment metrics, and confirms events arrive.
Ships in dif. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Use this skill to make a project send the right events to dif analytics, and to prove that they arrive. At the end:
@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.dif.track() call with the exact same name.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.
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.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.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 <slug>. dif events and dif validate need a workspace, so skip them.
dif auth status --json. If logged_in is false, run dif auth login, then come back here.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 <slug> to every dif site command after that.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.Find these before you change anything:
<head>.pushState) or hash (#/route).<meta> tag.dif.init, dif.track and data-dif-event uses.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.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.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 <file>. If it prints nothing, add the file to .gitignore and tell the person. Use the key for data-key locally. Never commit it.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.needs_selection, ask the person which project, then run again with --project <slug>. With no dif/ workspace, every dif site command needs --project <slug>.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.
Every app gets page views from the script tag in <head>. 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:
data-domains to the production hostnames, so preview deploys send nothing.data-spa="hash" only for a hash router.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 <head>: 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.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.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.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.Make the tracking plan before you edit code. Use these sources, in this order:
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.dif.track() calls and data-dif-event attributes.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.
# 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.
<a href="/signup" data-dif-event="signup_cta_clicked" data-dif-prop-plan="pro">Start free</a>
window.dif.track(name, { plan: "pro" }). The props are the second argument. This path has no value or currency.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 }).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.value and currency, never a prop.dif.track() call, or ask.Then run the repo's typecheck and tests, and dif validate.
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:
dif site check --env test --wait 60 --json
dif site check --env live --wait 60 --json. Before that, use the Step 6 result.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.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.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.
Tell the person:
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."
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.
^[a-z0-9][a-z0-9_]{0,63}$. If it does not, report Skipped: unsafe metric and stop.dif.track() call with this exact name, change nothing. Report Instrumented but not seen in 7d and open no branch.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.dif/instrument/<metric>, cut from origin/<default>. Follow the branch rules in the triage skill.dif_cli_ or dif_pat_ token.dif connect --key with a site key, and never put a site key in dif/config.yaml.ingest.difdb.com through the app, and never set data-host outside a staging test.What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
dif-analytics, event-tracking, analytics-setup, tracking-plan
npx claudepluginhub dif-sh/dif --plugin dif7 skills in this plugin
/dif-author-experimentAuthoring and iterating on dif.sh experiments — drafting frontmatter, choosing variants and weights, declaring audiences, and validating the file. Use whenever the user wants to create, draft, edit, or fix a dif experiment, or mentions A/B test, variant, audience, hypothesis, surface, exclusion group, or the commands dif new, dif validate, or dif build./dif-conclude-experimentConclude a dif.sh experiment or rollout with real numbers. Gets the result from dif analytics over MCP or REST, checks the verdict, writes the Decision from decision_draft, runs dif conclude, and records the surface Learning. Use when the user wants to conclude, finish, ship, kill, wrap up or document the outcome of a dif experiment, write a decision, record a learning, or run the command dif conclude./dif-connect-cloudConnect a dif.sh workspace to dif analytics from the terminal. Signs the dif CLI in with a code the person approves in the browser, links or creates the dif analytics project, writes its publishable key into dif/config.yaml, shows the GitHub App install link and runs dif build. Use when the user wants to connect, link or set up dif analytics, log in or sign up to dif, send events to dif analytics, or mentions dif cloud, dif auth login, dif connect or needs_selection./dif-docsReference for dif.sh — feature flags and A/B experiments stored as Markdown files in the repo and compiled by `dif build` into a typed TypeScript client. Use when adding feature flags, a percentage rollout, or an A/B test to a JavaScript or TypeScript app (Next.js, React, SvelteKit, plain TS), when the user wants flags as code instead of a dashboard, or when the user mentions dif, dif/config.yaml, dif build, dif validate, or @dif.sh/sdk./dif-generate-surfacesGenerating the initial set of dif.sh surfaces by reading the app's structure and product context. Use when the user wants to set up, scaffold, populate, auto-create, or propose surfaces for a dif project — especially right after `dif init`, when `dif/surfaces/` is empty or sparse, or when `dif new` fails with "surface does not exist". Reads the codebase (routes, pages, README, package.json) plus `dif/context.json`, proposes a candidate set of surfaces with one-line rationales, confirms with the user, then writes the matching `dif/surfaces/<name>.md` files./dif-triage-experimentsTriage every running dif.sh experiment and rollout from the dif analytics overview. Shows what needs action, then ramps rollouts, turns off or kills harmful variants, and concludes winners and inconclusive tests by editing dif/ files, with one branch and one PR per experiment in autonomous mode. Use when the user asks to triage experiments, review all experiments, see what needs action, ramp, turn off, kill, find winners, or check the dif analytics overview.Other plugins' skills for the same job