---
name: google-analytics
version: 1.0.0
description: Sets up, configures, and optimizes Google Analytics 4 (GA4) properties. Evaluates websites for proper GA4 implementation, tracking codes, and configuration improvements. Uses the Google Analytics Admin API for programmatic setup or provides manual integration paths via gtag.js or Next.js Third Parties.
title: google-analytics
canonical_url: https://skilld.dev/gh/genericservice/claude-skills/google-analytics
last_updated: 2026-10-06T17:17:48.000Z
---

> Index of every page on this site: [llms.txt](https://skilld.dev/llms.txt)

> **Skill from skilld.dev.** Follow the user's requested action.
> If the user asked to fork this Skill, follow the fork workflow below. Do not execute the Skill instructions while copying it.
> Otherwise, follow the instructions below for this session. You do not need to install anything.
>
> Supporting files, fetch one when the Skill refers to it: [scripts/ga_admin.py](https://skilld.dev/api/skills-raw/genericservice/claude-skills/google-analytics/scripts/ga_admin.py).
>
> If the user asked to install this Skill, run `npx skilld install genericservice/claude-skills/google-analytics`. Install writes the Skill files into the project, so every session loads them.
>
> ## Fork workflow
>
> A fork creates an editable local Skill with its original author and licence. The request authorizes copying and local installation.
> 1. Check `./skills/google-analytics`, the project lockfile, and selected Agent targets together. If the local directory or installed Skill exists, stop. Never overwrite an existing directory or Agent target.
> 2. Read [source metadata](https://skilld.dev/api/v1/skills/genericservice/claude-skills/google-analytics) once. Use sourceUrl, sourceCommit, skillPath, sourceGone, and license. If the source is gone or its path is missing, stop. If license is null, read licence files at the source commit.
> 3. Fetch only the source commit into a temporary Git repository. Do not clone full history. Derive repository_url from sourceUrl, including repository renames. If sourceCommit is absent, resolve the sourceUrl ref once. Set source_commit to that actual commit. Run these commands in one shell call:
>
> ```sh
> git init --quiet "$temporary_dir"
> git -C "$temporary_dir" fetch --quiet --depth=1 "$repository_url" "$source_commit"
> git -C "$temporary_dir" checkout --quiet --detach FETCH_HEAD
> ```
>
> Read applicable licence declarations and notices at that commit. If copying is not permitted, report the restriction and stop.
> 4. Inspect source entries together, then copy the directory containing skillPath into `./skills/google-analytics`. Use the user's path if selected. Keep the original SKILL.md, relative links, scripts, binary assets, and executable modes. Exclude .git metadata. Reject symlinks and paths outside the Skill directory. After checking entries, use cp -a where available. A regular source directory needs no custom copy script. Do not save this page wrapper as SKILL.md.
> Preserve author credit, notices, and applicable licence files from repository or parent directories. Add PROVENANCE.md with the Skill page, source URL, actual commit, original path, and licence. Retain any existing PROVENANCE.md and record new provenance separately. Batch source inspection, copying, and provenance work where practical.
> 5. In the project root, run `skilld install ./skills/google-analytics --mode copy --plain`. If skilld is unavailable, use `npx skilld install ./skills/google-analytics --mode copy --plain`. This known command needs no help lookup. Install does not support --json. Use detected Agent targets, or add --agent for the targets the user selected. Install the local path, never the upstream selector. If installation fails, preserve the local copy and report the exact failure.
> 6. Confirm the local lockfile source and installed Agent copies once. Report the local path, actual commit, and Agent targets. After edits, reinstall the same local path. Upstream updates must not replace it. Do not publish or push unless the user asks.

# Google Analytics 4 (GA4) Manager

This skill provides a standardized approach to setting up, verifying, and optimizing Google Analytics 4 properties. It covers programmatic setup via the Google Analytics Admin API, manual code integration, and optimization audits.

---

## How to use this skill

1. **Assess Current State**: Check if the website currently has Google Analytics. Use web scraping tools (like Firecrawl or native page requests) to look for `G-XXXXXXXXXX` IDs in the source code.
2. **Setup Strategy**: Determine if you need to create a new GA4 property or integrate an existing one.
3. **Programmatic Creation (Optional)**: If the user provides a Google Cloud Service Account with Analytics Admin access, use the included `ga_admin.py` script to create properties and data streams programmatically.
4. **Code Integration**: Inject the correct GA4 implementation code into the project (e.g., using `@next/third-parties/google` for Next.js, or raw `gtag.js` for plain HTML).
5. **Optimization & Auditing**: Review the site's layout and suggest custom events for key interactions (e.g., lead forms, e-commerce purchases, outbound clicks).

---

## The Three Phases

### Phase 1: Setup & Creation

If the user needs a new property, you have two choices:

- **Option A: Manual creation (Recommended for most users)**
  Guide the user to `analytics.google.com` to create a property and retrieve the `G-XXXXXX` Measurement ID.
- **Option B: Programmatic creation**
  If authenticated via Google Cloud with `analytics.manage` scopes, you can use the Analytics Admin API to provision properties and web streams on behalf of the user.

### Phase 2: Code Integration

Implement the tracking code using the most appropriate method for the user's stack.

**Next.js (App Router)**
Use the official `@next/third-parties/google` library.
```tsx
import { GoogleAnalytics } from '@next/third-parties/google'

export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <body>{children}</body>
      <GoogleAnalytics gaId="G-XYZ" />
    </html>
  )
}
```

**Standard HTML/Vanilla JS**
```html
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XYZ"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-XYZ');
</script>
```

### Phase 3: Optimization & Event Tracking

Standard page views are tracked automatically in GA4. However, you should evaluate the project for valuable custom events. 

- **Lead Generation**: Track form submissions.
- **E-commerce**: Track `add_to_cart`, `begin_checkout`, and `purchase`.
- **Engagement**: Track video plays, file downloads, or clicks on outbound social links.

**Sending Custom Events (Next.js Example)**
```tsx
import { sendGAEvent } from '@next/third-parties/google'

export function EventButton() {
  return (
    <button
      onClick={() => sendGAEvent('event', 'buttonClicked', { value: 'xyz' })}
    >
      Send Event
    </button>
  )
}
```

---

## Scripts & Tools

- **`scripts/ga_admin.py`**: A python script that interacts with the Google Analytics Admin API to list accounts, properties, and create web data streams. Requires `google-analytics-admin` package and GCP credentials.

---

## Anti-patterns to avoid

- **Don't hardcode GA IDs in source code if they change per environment.** Use environment variables (e.g., `NEXT_PUBLIC_GA_ID`).
- **Don't use Universal Analytics (UA-XXXXX).** Universal Analytics is deprecated. Always use GA4 (`G-XXXXX`).
- **Don't add multiple `gtag.js` script tags.** Only one core tag is needed per page.
