0.7.0 • Published 3 months ago

@ag.common/analytics v0.7.0

Weekly downloads
-
License
MIT
Repository
-
Last release
3 months ago

Analytics

The @ag.common/analytics package aims to coordinate analytics capture across the export service. We ship the correct tracking codes out of the box for Hotjar and Google Analytics so that everything is reported to the right place.

It also provides convenient helpers for reporting custom events and managing data contexts.

See the storybook for more examples.

Installation

yarn add @ag.common/analytics

Basic usage

import Script from 'next/script';
import { Analytics } from '@ag.common/analytics';

function App() {
	return (
		<Analytics scriptComponents={{ Script }}>
			<YourApplication />
		</Analytics>
	);
}

Disable an analytics script

Hotjar and Google Analytics are enabled by default but can be turned off one by one.

import Script from 'next/script';
import { Analytics } from '@ag.common/analytics';

function App() {
	return (
		<Analytics
			scriptComponents={{ Script }}
			hotjar={false}
			googleAnalytics={false}
		>
			<YourApplication />
		</Analytics>
	);
}

Trigger analytics events

Events are reported to Google Analytics by default

import Script from 'next/script';
import { Analytics, useAnalytics } from '@ag.common/analytics';

function TrackingButton() {
	const { trackEvent } = useAnalytics();

	return (
		<Button
			onClick={() => {
				trackEvent('login');
			}}
		>
			Log in
		</Button>
	);
}

function App() {
	return (
		<Analytics scriptComponents={{ Script }}>
			<p>Log in to get started</p>

			<TrackingButton />
		</Analytics>
	);
}

Custom event handlers

You can report to an alternative analytics provider by providing an onEvent callback

import Script from 'next/script';
import { Analytics } from '@ag.common/analytics';

function App() {
	return (
		<Analytics
			scriptComponents={{ Script }}
			onEvent={(name, data) => logger.info(name, data)}
		>
			<YourApplication />
		</Analytics>
	);
}
0.7.0

3 months ago

0.5.0

10 months ago

0.4.0

10 months ago

0.6.0

7 months ago

0.3.1

11 months ago

0.3.0

12 months ago

0.2.0

12 months ago

0.1.0

12 months ago