Tracking

Questions? Ask in Slack #vcc-ui

@volvo-cars/tracking

@volvo-cars/tracking@volvo-cars/tracking@volvo-cars/tracking

A declarative way to add Google Tag Manager tracking data to your application. It supports multiple ways of sending analytics events by supporting event data inheritance and the possibility to send events with vanilla JavaScript without rendering/hydrating your React application.

Installation

$ yarn add @volvo-cars/tracking

💡 This package includes Typescript definitions

useTracker

The simplest way of adding an event to GTM is by using the useTracker hook which returns a Tracker instance that exposes helpful methods that send different event types such as interaction or noninteraction. It sends these events by modifying the window.dataLayer global array. This array is watched for changes by GTM when embedded on a page, which means any pushes to this array will trigger a new GTM event.

In the above example, we send an interaction event and attach eventAction and eventLabel to it. Notice the event additions to window.dataLayer on each button click. It's also worth noticing how Tracker.interaction adds the event property automatically to each event. This is to distinguish between the types of events sent to GTM.

Arguments

useTracker takes 3 optional arguments. The first is an event data object that will be added to all events sent by the returned Tracker. The second is any Tracker options and the third is options?.ignoreIfEmptyContext which is used in conjunction with the TrackingProvider mentioned below, this will disable sending events if the hook is not wrapped with a parent TrackingProvider.

PageTrackingProvider

To send page load events without needing user input, immediately on page load, you can be done by adding the PageTrackingProvider. There can only be one PageTrackingProvider in the tree per page.

Notice how the events pushed to window.dataLayer in the above example include pageName and pageType.

forceLowerCase

All events are forced to be lowercase by default but it's also possible to disable this behaviour, based on specific requirements. This can be done with the forceLowerCase prop.

logging

We can enable logging of events in development with the logging prop on the PageTrackingProvider.

TrackingProvider

Simple

While useTracker works fine for simple cases, we sometimes want to send shared data between all events without needing to rewrite said data with every event we send. This can be done by passing the default data as props to TrackingProvider.

Notice how the events pushed to window.dataLayer in the above example include pageName and eventCategory.

Inheritance

TrackingProvider supports inheritance, meaning that for any data added to any of the parant TrackingProviders, all will be sent and not just the last in the tree.

In the above example, all event data from all TrackingProviders was sent with each event in that tree. Notice how eventCategory was overridden in the last TrackingProvider.

trackPageLoad [DEPRECATED] use PageTrackingProvider

It's sometimes desired to send page load events without needing user input. This can be done by adding the trackPageLoad prop on any TrackingProvider in the tree.

() => {
return (
<TrackingProvider trackPageLoad pageType="pdp" pageName="xc40">
<Block>...</Block>
</TrackingProvider>
);
};

forceLowerCase

All events are forced to be lowercase by default but it's also possible to disable this behaviour, based on specific requirements. This can be done with the forceLowerCase prop.

logging

We can enable logging of events in development with the logging prop on the TrackingProvider.

enableReactTracking

TrackingProvider can store information in data attributes. This allows us to push event tracking information in places where we don't want to hydrate/render our React application. A useful case is for static sites that don't need any user input except for sending tracking events. A useful usecase is the DotCom SiteFooter which is rendered using React server-side but does not render/hydrate client-side. This can be done by disabling the enableReactTracking prop on the TrackingProvider. A more detailed explanation can be found in the Dom Tracking.

Track

Allows any child element to attach tracking events to any event.

withTracker

This package also exports a HOC that helps with attaching tracking events based on domEvents

Dom Tracking

As mentioned earlier in the enableReactTracking section. TrackingProvider can store information in data attributes. This allows us to push event tracking information in places where we don't want to hydrate/render our React application.

To enable this, first set enableReactTracking to false on the TrackingProvider. This will generate the following html, notice the data-track-onclick attributes.

We then attach the tracking listeners with createDomTrackingListener.

import { createDomTrackingListener } from '@volvo-cars/tracking/domTracking';
document
// first get the parent of the nested components which have tracking
.getElementById('root')
// attach the listener which will traverse up the tree to get all the
// context data from parent `data` attributes
?.addEventListener('click', createDomTrackingListener('onClick'));

Caveats

When rendering the static content, and wanting to use the TrackingProvider passing custom react components will just pass in props with data. It's up to you to pass them down the line. It will only add them in two cases:

  1. Direct child is a simple dom element.
  2. Children is a fragment or multiple elements, in this case, the'll be wrappedj with div

Examples:

// this will add data attributes
// to the `main`
<TrackingProvider
pageType="catch all"
enableReactTracking={false}
>
<main>
{/* .... */}
</main>
</TrackingProvider>
// this will wrap children
// with `div` containing data
<TrackingProvider
pageType="catch all"
enableReactTracking={false}
>
{header}
<main>
{/* .... */}
</main>
</TrackingProvider>
// this will pass down `data-track-context`
// to the `App` it's up to developer to handle this
<TrackingProvider
pageType="catch all"
enableReactTracking={false}
>
<App />
</TrackingProvider>

Strict types

This package exports non-strict types for TrackingData and CustomDimension

export interface TrackingData extends Record<string, any> {}
export type CustomDimension = string;

Those types can be made stricter depending on your use case. To override those types you can create a declartions file in your types directory somewhere in your application and overide them as needed.

Example

import '@volvo-cars/tracking';
declare module '@volvo-cars/tracking' {
export interface TrackingData {
eventAction?: string;
eventLabel?: string;
eventCategory?: string;
}
}

Web Vitals

You can measure Web Vitals metrics on real users, in a way that accurately matches how they're measured by Chrome and reported to other Google tools. This can be done in two ways depending on the use case:

Using Next.js

Starting from Next.js v10.0.0, you can export a reportWebVitals function from _app which helps provide Web Vital metrics:

import { reportWebVitals as webVitals } from '@volvo-cars/tracking/webVitals';
export function reportWebVitals(metrics: NextWebVitalsMetric) {
return webVitals({
metrics,
});
}

This will report something like the following, depending on the metric dispatched by Next.js

{
event: 'noninteraction',
eventAction: 'fcp',
eventCategory: 'web vitals',
eventLabel: 'uniqueid',
eventValue: 2,
},

Any additional event data can be sent using additionalEventData property:

import { reportWebVitals as webVitals } from '@volvo-cars/tracking/webVitals';
export function reportWebVitals(metrics: NextWebVitalsMetric) {
return webVitals({
metrics,
additionalEventData: {
pageName: 'my page name',
pageType: 'my page type',
},
});
}

Custom App

If not using Next.js, Web Vitals can be reported and measured using measureWebVitals:

import { measureWebVitals } from '@volvo-cars/tracking/webVitals';
measureWebVitals();

API

Tracker

constructor(
eventData?: TrackingData | null,
{
forceLowerCase = true,
logging = false,
disabled = false,
}: TrackerOptions = {}
)
NameDescriptionTypeDefault Value
eventDataDefault event data to be sent with every event.Objectundefined
options.forceLowerCaseForce all event values to be lowercase.booleantrue
options.deferNonInteractionDefer nonInteraction events until a pageType or pageName event is present in the dataLayer.booleantrue
options.loggingLog all sent events to the console.booleanfalse
options.disabledDisables sending events.booleanfalse
options.modeDecides what events to send for ga3 or ga4 or bothstringga3
options.ga3GA3 values to keep sending to ga3GA3Eventundefined

Tracker.interaction(eventData?: TrackingData)

Pushes an event: interaction event to the data layer.

Tracker.nonInteraction(eventData?: TrackingData)

Pushes an event: noninteraction event to the data layer. If no object with the pageType or pageName is in the data layer yet, nonInteraction events are queued for up to 90 seconds to avoid affecting the order of events used to determine bounce rates.

Tracker.virtualPageView(name: string, value?: string)

Pushes an event: virtualPageView event to the data layer.

Tracker.pushCustomDimension(name: string, value?: string)

Pushes a custom dimension to the data layer.

useTracker

useTracker(
hookData?: TrackingData | null,
trackerOptions?: TrackerOptions,
options?: { ignoreIfEmptyContext?: boolean }
): Tracker

returns a Tracker instance.

NameDescriptionTypeDefault Value
hookDataAny default Tracking data to be added to Tracker eventDataObject, null
trackerOptionsAny trackerOptions to be forwarded to the TrackerTrackerOptions
options?.ignoreifEmptyContextDisables the Tracker if no top level TrackerProvider wraps the treebooleanfalse
options?.modeDecides what events to send for ga3 or ga4 or bothstringga3
options.ga3GA3 values to keep sending to ga3GA3Eventundefined

Props - TrackingProvider

NameDescriptionTypeDefault Value
enableReactTrackingIf disabled, data- attributes are used to maintain tracking data instead of React context.booleantrue
deferNonInteractionDefer nonInteraction events until a pageType or pageName event is present in the dataLayer.booleantrue
trackPageLoad[DEPRECATED] Automatically sends pageLoad event.booleanfalse
forceLowerCaseForce all event values to be lowercase.booleanfalse
loggingEnable logging of sent analytics data in development.booleanfalse
modeDecides what events to send for ga3 or ga4 or bothstringga3
eventWhat type of event to pass down to children. Only works for ga4stringcustom_event
ga3GA3 values to keep sending to ga3GA3Eventundefined
...restAny other props will be sent as tracking dataObject

Props - PageTrackingProvider

NameDescriptionTypeDefault Value
enableReactTrackingIf disabled, data- attributes are used to maintain tracking data instead of React context.booleantrue
forceLowerCaseForce all event values to be lowercase.booleanfalse
loggingEnable logging of sent analytics data in development.booleanfalse
modeDecides what events to send for ga3 or ga4 or bothstringga3
ga3GA3 values to keep sending to ga3GA3Eventundefined
...restAny other props will be sent as tracking dataObject

Props - Track

NameDescriptionTypeDefault Value
childrenA single React node, Fragments not supportedReact.node
eventActionOptional action to send with analytics data.stringclick
domEventA dom/react event to watch and attach tracking data tostringonClick
eventLabelLabel to send with analytics data.string
customDataCustom tracking data to pass through to TrackerTrackingData
eventWhat type of event to send. Only works for ga4stringcustom_event
ga3GA3 values to keep sending to ga3GA3Eventundefined

withTracker

withTracker(
Component: React.ComponentType,
{
event: 'onClick';
defaultAction: string;
ga3?: GA3Event;
});

returns a new React.ComponentType with tracking data attached.

NameDescriptionTypeDefault Value
ComponentAny valid React componentReact.ComponentType
eventonClick eventstring
defaultActionDefault action to be sent with the eventstring
trackEventWhat type of event to send. Only works for ga4stringcustom_event
trackGA3GA3 values to keep sending to ga3GA3Eventundefined

createDomTrackingListener

createDomTrackingListener(eventName:string, options?: TrackerOptions, event?: TrackEvent )

return a new event listener.

Google Analyctis 4

Tracker.sendEvent(event?: TrackEvent, eventData?: TrackingData)

Pushes an event: TrackEvent event to the data layer.

TrackEvent =
| 'chat_interaction'
| 'custom_event'
| 'sitenav_interaction'
| 'view_item'
| 'page_view'
| 'web_vitals'
| 'add_to_cart'
| 'cart_view'
| 'begin_checkout'
| 'add_payment_info'
| 'purchase'
| 'config_start'
| 'config_finish'
| 'login'
| 'account_created'
| 'form_load'
| 'form_submit'
| 'search';

Tracker.customEvent(customEventData: CustomEventData)

Pushes an event: custom_event event to the data layer.

Tracker.siteNavInteraction(siteNavInteractionData: SiteNavInteractionData)

Pushes an event: site_navInteraction event to the data layer.

Tracker.chatInteraction(chatInteractionData: ChatInteractionData)

Pushes an event: chat_interaction event to the data layer.

Tracker.pageView(pageViewData: PageViewData)

Pushes an event: page_view event to the data layer.

Tracker.viewItem(viewItemData: ViewItemData)

Pushes an event: view_item event to the data layer.

Tracker.addToCart(addToCartData: AddtoCartData)

Pushes an event: add_to_cart event to the data layer.

Tracker.cartView(cartViewData: CartViewData)

Pushes an event: cart_view event to the data layer.

Tracker.beginCheckout(beginCheckoutData: BeginCheckoutData)

Pushes an event: begin_checkout event to the data layer.

Tracker.addPaymentInfo(addPaymentInfoData: AaddPaymentInfoData)

Pushes an event: add_payment_info event to the data layer.

Tracker.purchase(purchaseData: PurchaseData)

Pushes an event: purchase event to the data layer.

Tracker.configStart(configStartData: ConfigStartData)

Pushes an event: config_start event to the data layer.

Tracker.configFinish(configFinishData: ConfigFinishData)

Pushes an event: config_finish event to the data layer.

Tracker.formLoad(formLoadData: FormLoadData)

Pushes an event: form_load event to the data layer.

Tracker.formSubmit(formSubmitData: FormSubmitData)

Pushes an event: form_submit event to the data layer.

Tracker.webVitals(webVitalsData: WebVitalsData)

Pushes an event: web_vitals event to the data layer.

How to upgrade to Google Analytics 4

Start to set mode to both and set logging to true to see that two events are sent in the console.

<TrackingProvider logging mode="both">
children
</TrackingProvider>

To have every event sent for example add_to_cart

<TrackingProvider event="add_to_cart" logging mode="both">
children
</TrackingProvider>

You can override event in children to set a new event prop for example

<TrackingProvider event="add_to_cart" logging mode="both">
children
<TrackingProvider event="purchase" logging mode="both">
children
</TrackingProvider>
</TrackingProvider>

You can also specify on every child for example

<TrackingProvider event="add_to_cart" logging mode="both">
children
<Click trackEvent="purchase" trackEventLabel="label">
button
</Click>
<Track event="add_payment_info" eventLabel="label">
<>
<button />
<button />
</>
</Track>
</TrackingProvider>

You can now also use our new typed functions instead

const tracker = new Tracker(null, { mode: 'both' });
tracker.addToCart({
countryCode: 'countryCode',
pageName: 'pageName',
pageType: 'pageType',
ecommerce: {
items: [
{
itemName: 'itemName',
},
],
},
});
2024 © Volvo Car Corporation