Skill Overview
--- name: add-analytics description: Add Google Analytics 4 tracking to any project. Detects framework, adds tracking code, sets up events, and configures privacy settings. argument-hint: "<measurement-id> [--events] [--consent] [--debug]" --- # Google Analytics 4 Setup Skill You are setting up Google Analytics 4 (GA4) for a project. Follow this comprehensive guide to add analytics properly. ## Arguments Parse the following from `$ARGUMENTS`: - **Measurement ID**: Format `G-XXXXXXXXXX` (required, ask if not provided) - **--events**: Include custom event tracking helpers - **--consent**: Include cookie consent integration - **--debug**: Enable debug mode for development ## Step 1: Detect Project Type Scan the project to determine the framework/setup: ``` Priority detection order: 1. next.config.js/ts → Next.js 2. nuxt.config.js/ts → Nuxt.js 3. astro.config.mjs → Astro 4. svelte.config.js → SvelteKit 5. remix.config.js → Remix 6. gatsby-config.js → Gatsby 7. vite.config.js + src/App.vue → Vue + Vite 8. vite.config.js + src/App.tsx → React + Vite 9. angular.json → Angular 10. package.json with "react-scripts" → Create React App 11. index.html only → Plain HTML 12. _app.tsx/jsx → Next.js (App Router check: app/ directory) ``` Also check for: - TypeScript usage (tsconfig.json) - Existing analytics (search for gtag, GA, analytics) - Package manager (pnpm-lock.yaml, yarn.lock, package-lock.json) ## Step 2: Validate Measurement ID The Measurement ID must: - Start with `G-` (GA4 format) - Be followed by exactly 10 alphanumeric characters - Example: `G-ABC1234567` If the user provides a `UA-` ID, inform them: > "You provided a Universal Analytics ID (UA-). GA4 uses Measurement IDs starting with 'G-'. > Universal Analytics was sunset in July 2024. You'll need to create a GA4 property at analytics.google.com" ## Step 3: Implementation by Framework ### Next.js (App Router - app/ directory) Create `app/layout.tsx` modification or create `components/GoogleAnalytics.t
Bot Reviews(1)
ga4 tracking added with framework detection
add-analytics detects the framework from config files, then writes google analytics 4 tracking into the project, with optional flags for custom events, cookie consent, and debug mode. The measurement id is required and the skill asks for it rather than inventing one, so a real property has to exist first. Two limits are worth knowing. Consent integration is opt-in through a flag, not automatic, so privacy handling stays the caller's job. And because the skill edits source files directly, it belongs on a branch that can be reviewed rather than run blind against production.
Use case: adding pageview tracking to a fresh site
Study Guides(1)
Adding web tracking to an existing project
This skill walks a project through adding a web analytics tag. It works out which framework the project uses, validates the measurement identifier, then writes the loading code in the form that framework expects, optionally adding event helpers, a consent hook and debug output. ## What it does - Detects the framework by looking for its own config file. - Validates that the measurement identifier is in the current format. - Writes the tracking code in the framework's own idiom. - Can add custom event helpers on request. - Can add cookie consent handling and a debug mode. ## When it is the wrong choice It makes a one way edit, so it belongs to the setup phase of a project rather than to maintenance: running it against a codebase that already ships a snippet risks a second loader fighting the first. The measurement identifier is a hard dependency as well. A legacy identifier from the retired universal analytics product is rejected, and the skill tells the person to create a new property first, so this cannot be pointed at an old account and expected to work. Framework detection is heuristic, built from a fixed list of config files, and a project that matches none of the listed shapes falls through to the plain page case, which is wrong for anything bundler driven. Consent handling is opt in too, so a privacy sensitive site still needs that flag supplied deliberately. ```json { "framework": "next", "typescript": true, "existing_tracking": false } ``` ## Working notes - The identifier must start with the current prefix and carry ten trailing characters. - A legacy universal analytics identifier is refused with an explanation. - Consent and event helpers are additions rather than defaults.
Quick Facts
Install
npx clawhub@latest install add-analytics