@ag.common/analytics
TypeScript icon, indicating that this package has built-in type declarations

0.7.0 • Public • Published

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>
	);
}

Readme

Keywords

none

Package Sidebar

Install

npm i @ag.common/analytics

Weekly Downloads

2

Version

0.7.0

License

MIT

Unpacked Size

36.5 kB

Total Files

23

Last publish

Collaborators

  • joanne-b
  • alistairphillips-ag
  • adrian.yee_agds
  • chrislaneau
  • stowball
  • toenails