Implement analytics tracking in Google Antigravity with privacy-focused tools including Plausible PostHog and custom events
# Analytics Integration for Google Antigravity
Analytics help understand user behavior. This guide establishes patterns for privacy-focused analytics in Google Antigravity projects.
## Plausible Analytics
```typescript
// components/PlausibleProvider.tsx
"use client";
import Script from "next/script";
export function PlausibleProvider() {
return (
<Script
defer
data-domain="yourdomain.com"
src="https://plausible.io/js/script.js"
/>
);
}
// Track custom events
declare global {
interface Window {
plausible?: (event: string, options?: { props?: Record<string, string> }) => void;
}
}
export function trackEvent(name: string, props?: Record<string, string>) {
window.plausible?.(name, { props });
}
```
## PostHog Integration
```typescript
// lib/posthog.ts
import posthog from "posthog-js";
export function initPostHog() {
if (typeof window !== "undefined") {
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
api_host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
capture_pageview: false,
});
}
}
export function trackPageView(url: string) {
posthog.capture("$pageview", { $current_url: url });
}
export function trackEvent(name: string, properties?: Record<string, unknown>) {
posthog.capture(name, properties);
}
export function identifyUser(userId: string, traits?: Record<string, unknown>) {
posthog.identify(userId, traits);
}
```
## Page View Tracking
```typescript
"use client";
import { usePathname, useSearchParams } from "next/navigation";
import { useEffect } from "react";
import { trackPageView } from "@/lib/posthog";
export function AnalyticsProvider({ children }: { children: React.ReactNode }) {
const pathname = usePathname();
const searchParams = useSearchParams();
useEffect(() => {
const url = pathname + (searchParams.toString() ? `?${searchParams}` : "");
trackPageView(url);
}, [pathname, searchParams]);
return <>{children}</>;
}
```
## Best Practices
1. **Privacy first**: Choose privacy-respecting tools
2. **Consent management**: Respect user preferences
3. **Custom events**: Track meaningful interactions
4. **User identification**: Connect sessions
5. **Performance**: Load analytics asyncThis Analytics prompt is ideal for developers working on:
By using this prompt, you can save hours of manual coding and ensure best practices are followed from the start. It's particularly valuable for teams looking to maintain consistency across their analytics implementations.
Yes! All prompts on Antigravity AI Directory are free to use for both personal and commercial projects. No attribution required, though it's always appreciated.
This prompt works excellently with Claude, ChatGPT, Cursor, GitHub Copilot, and other modern AI coding assistants. For best results, use models with large context windows.
You can modify the prompt by adding specific requirements, constraints, or preferences. For Analytics projects, consider mentioning your framework version, coding style, and any specific libraries you're using.