A unified TypeScript library for tracking analytics across Google Analytics 4, Meta (Facebook) Pixel, and TikTok Pixel
Built for React applications and beyond
Modern front-ends often require multiple analytics platforms. Analytics offers:
- A single API that works across GA4, Meta Pixel, and TikTok Pixel.
- Lazy loading of tracking scripts and automatic queueing of events.
- Built with TypeScript, offering type definitions out of the box.
- Designed for React (hooks + context) but easy to adapt.
- Built with Vite for optimal bundle size and performance.
- Front-end engineers using React who want to track events across multiple analytics platforms.
- Teams looking for a shared analytics library with a consistent API.
- Developers who want a lightweight, configurable alternative to multiple SDKs.
npm install @jadecubes/analytics
# or
pnpm add @jadecubes/analytics
# or
yarn add @jadecubes/analyticsimport { createAnalyticsProviders, AnalyticsPlatforms } from '@jadecubes/analytics'
// Initialize analytics providers at app startup
createAnalyticsProviders({
[AnalyticsPlatforms.GA]: {
measurementId: 'G-XXXXXXXXXX'
},
[AnalyticsPlatforms.META]: {
measurementId: 'YOUR_META_PIXEL_ID'
},
[AnalyticsPlatforms.TIKTOK]: {
measurementId: 'YOUR_TIKTOK_PIXEL_ID'
}
})import { getAnalyticsInstance, AnalyticsPlatforms } from '@jadecubes/analytics'
// Get analytics instance for a specific platform
const ga = getAnalyticsInstance(AnalyticsPlatforms.GA)
const meta = getAnalyticsInstance(AnalyticsPlatforms.META)
const tiktok = getAnalyticsInstance(AnalyticsPlatforms.TIKTOK)
// Track events for Google Analytics 4
ga?.processAnalyticsEvent({
eventName: 'add_to_cart',
eventParams: {
currency: 'USD',
value: 99.99,
items: [
{
item_id: 'SKU_12345',
item_name: 'Product Name',
price: 99.99
}
]
}
})
// Track events for Meta (Facebook) Pixel
meta?.processAnalyticsEvent({
eventName: 'AddToCart',
eventParams: {
content_name: 'Product Name',
content_ids: ['SKU_12345'],
content_type: 'product',
value: 99.99,
currency: 'USD'
}
})
// Track events for TikTok Pixel
tiktok?.processAnalyticsEvent({
eventName: 'AddToCart',
eventParams: {
content_id: 'SKU_12345',
content_name: 'Product Name',
value: 99.99,
currency: 'USD'
}
})The processAnalyticsEvent method:
- Queues events if the analytics script isn't loaded yet
- Automatically sends queued events once the platform is ready
- Handles lazy loading - you can call it immediately after initialization without worrying about script load timing
import { useEffect } from 'react'
import { createAnalyticsProviders, getAnalyticsInstance, AnalyticsPlatforms } from '@jadecubes/analytics'
function App() {
useEffect(() => {
// Initialize on app mount
createAnalyticsProviders({
[AnalyticsPlatforms.GA]: { measurementId: 'G-XXXXXXXXXX' },
[AnalyticsPlatforms.META]: { measurementId: 'YOUR_PIXEL_ID' },
[AnalyticsPlatforms.TIKTOK]: { measurementId: 'YOUR_TIKTOK_PIXEL_ID' }
})
}, [])
const handlePurchase = () => {
const ga = getAnalyticsInstance(AnalyticsPlatforms.GA)
const meta = getAnalyticsInstance(AnalyticsPlatforms.META)
// Track purchase event
ga?.processAnalyticsEvent({
eventName: 'purchase',
eventParams: {
transaction_id: 'T_12345',
value: 299.99,
currency: 'USD',
items: [{
item_id: 'SKU_12345',
item_name: 'Product Name',
price: 299.99
}]
}
})
meta?.processAnalyticsEvent({
eventName: 'Purchase',
eventParams: {
value: 299.99,
currency: 'USD'
}
})
}
return <YourApp />
}This library is published as @jadecubes/analytics on npm. If you're a maintainer, follow these steps to publish a new version:
-
npm Account: Ensure you have an npm account and are logged in
npm login
-
Organization Access: You need publish access to the
@jadecubesorganization on npm
-
Update Version: Update the version in
libs/analytics/package.jsonfollowing semantic versioning:- Patch (0.0.x): Bug fixes
- Minor (0.x.0): New features, backward compatible
- Major (x.0.0): Breaking changes
# Edit libs/analytics/package.json and update the version field # For example: "version": "0.0.2"
-
Build the Library:
pnpm build
-
Test the Build: Verify the build output in
dist/libs/analytics/ls -la dist/libs/analytics/
-
Copy package.json to dist: The package.json needs to be in the dist directory
cp libs/analytics/package.json dist/libs/analytics/
-
Publish to npm:
cd dist/libs/analytics npm publish --access public
You can add a publish script to your root package.json:
{
"scripts": {
"publish:analytics": "pnpm build && cp libs/analytics/package.json dist/libs/analytics/ && cd dist/libs/analytics && npm publish --access public"
}
}Then simply run:
pnpm publish:analyticsFor pre-release versions:
# Update version to include tag, e.g., "0.1.0-beta.1"
cd dist/libs/analytics
npm publish --access public --tag betaAnalytics/
βββ libs/
β βββ analytics/ # Main analytics library
β βββ src/
β β βββ core/ # Core analytics implementations
β β βββ react/ # React-specific providers
β β βββ types/ # TypeScript types
β βββ vite.config.ts # Vite build configuration
βββ tools/ # Build and development tools
# Build the library
pnpm build
# Run tests
pnpm test
# Run tests in watch mode
pnpm test:watch
# Run tests with coverage
pnpm test:coverage
# Lint code
pnpm lint