Toggle theme

Tracking🔗

@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🔗

💡 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.

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🔗

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.

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.

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:

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

Examples:

Strict types🔗

This package exports non-strict types for TrackingData and CustomDimension

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🔗

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:

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

Any additional event data can be sent using additionalEventData property:

Custom App🔗

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

API🔗

Tracker🔗

Name
Description
Type
Default Value
eventDataDefault event data to be sent with every eventObjectundefined
trackerOptions.forceLowerCaseForce all event values to be lowercasebooleantrue
trackerOptions.disabledDisables sending eventsbooleanfalse
Tracker.interaction(eventData?: TrackingData)Sends an event with event as interactionFunctionFunction
Tracker.nonInteraction(eventData?: TrackingData)Sends an event with event as noninteractionFunctionFunction
Tracker.virtualPageView(eventData?: TrackingData)Sends an event with event as virtualPageViewFunctionFunction
Tracker.pushCustomDimension(name: CustomDimension, value?: string)Pushes a custom eventFunctionFunction

useTracker🔗

returns a Tracker instance.

Name
Description
Type
Default Value
hookDataAny default Tracking data to be added to Tracker eventDataObject, nullundefined
trackerOptionsAny trackerOptions to be forwarded to the TrackerTrackerOptions undefined
options?.ignoreifEmtpyContextDisables the Tracker if no top level TrackerProvider wraps the treebooleanundefined

Props - TrackingProvider🔗

Name
Description
Type
Default Value
trackPageLoadAutomatically sends pageLoad eventbooleanundefined
forceLowerCaseForce all event values to be lowercasebooleanfalse
loggingEnable logging of sent analytics data in developmentbooleanfalse
enableReactTrackingIf disabled, data- attributes are used to maintain tracking databooleantrue
...restAny other props will be sent as tracking dataObjectundefined

Props - Track🔗

Name
Description
Type
Default Value
childrenA single React node, Fragments not supportedReact.nodeundefined
eventActionOptional action to send with analytics data.stringclick
eventLabelLabel to send with analytics data.stringundefined
customDataCustom tracking data to pass through to TrackerTrackingDataundefined
domEventA dom/react event to watch and attach tracking data tostringonClick

withTracker🔗

returns a new React.ComponentType with tracking data attached.

Name
Description
Type
Default Value
ComponentAny valid React componentReact.ComponentTypen/a
eventonClick eventstringn/a
defaultActionDefault action to be sent with the eventstringn/a

createDomTrackingListener🔗

return a new event listener.

2022 © Volvo Car Corporation