Set up analytics tracking (GA4, Mixpanel, PostHog)
✓Works with OpenClaudeYou are a data analytics engineer. The user wants to set up analytics tracking using GA4, Mixpanel, or PostHog in their web application.
What to check first
- Run
npm list | grep -E "(react|vue|angular|gtag|mixpanel|posthog)"to see which framework and analytics libraries are already installed - Check
package.jsonfor existing analytics dependencies and their versions - Verify your analytics account credentials and property/project IDs are available
Steps
- Install the appropriate SDK:
npm install @react-google-analytics/core(GA4),npm install mixpanel-browser(Mixpanel), ornpm install posthog-js(PostHog) - Create an
analytics.jsoranalytics.tsutility file in yoursrc/utils/directory to centralize tracking logic - Initialize the analytics client in your app's entry point (e.g.,
index.js,main.js, or_app.tsxfor Next.js) before rendering components - Set up page view tracking by wrapping route changes with the appropriate tracking call
- Create helper functions for common events (button clicks, form submissions, conversions) that developers can reuse throughout the app
- Add error boundary tracking to capture client-side errors
- Implement user identification to track authenticated vs. anonymous sessions
- Test events in your analytics dashboard using preview/debug mode before shipping to production
Code
// src/utils/analytics.js - Multi-provider setup
import mixpanel from 'mixpanel-browser';
import posthog from 'posthog-js';
const ANALYTICS_PROVIDER = process.env.REACT_APP_ANALYTICS_PROVIDER || 'ga4';
const GA4_ID = process.env.REACT_APP_GA4_ID;
const MIXPANEL_TOKEN = process.env.REACT_APP_MIXPANEL_TOKEN;
const POSTHOG_KEY = process.env.REACT_APP_POSTHOG_KEY;
const POSTHOG_HOST = process.env.REACT_APP_POSTHOG_HOST;
export const initializeAnalytics = () => {
switch (ANALYTICS_PROVIDER) {
case 'ga4':
if (GA4_ID) {
window.dataLayer = window.dataLayer || [];
function gtag() { window.dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', GA4_ID, {
anonymize_ip: true,
allow_google_signals: false
});
const script = document.createElement('script');
script.src = `https://www.googletagmanager.com/gtag/js?id=${GA4_ID}`;
script.async = true;
document.head.appendChild(script);
}
break;
case 'mixpanel':
if (
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Data & Analytics Skills
Other Claude Code skills in the same category — free to download.
CSV Parser
Parse and process CSV files
Data Transformer
Transform data between formats (JSON, XML, CSV)
Data Pipeline
Create data processing pipeline
Report Generator
Generate reports from data
Chart Creator
Create charts and visualizations (Chart.js, D3)
Data Exporter
Export data in multiple formats
ETL Script
Create ETL (Extract, Transform, Load) scripts
Data Validator
Validate data integrity and format
Want a Data & Analytics skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.