Example setup - Google Analytics and Dynatrace RUM
Quick setup
Follow these steps to quickly get Google Analytics (through Google Tag Manager) and Dynatrace RUM working using @hmcts/cookie-manager
1. Add the library and some event listeners
import cookieManager from '@hmcts/cookie-manager';
cookieManager.on('UserPreferencesLoaded', (preferences) => {
const dataLayer = window.dataLayer || [];
dataLayer.push({'event': 'Cookie Preferences', 'cookiePreferences': preferences});
});
cookieManager.on('UserPreferencesSaved', (preferences) => {
const dataLayer = window.dataLayer || [];
const dtrum = window.dtrum;
dataLayer.push({'event': 'Cookie Preferences', 'cookiePreferences': preferences});
if(dtrum !== undefined) {
if(preferences.apm === 'on') {
dtrum.enable();
dtrum.enableSessionReplay();
} else {
dtrum.disableSessionReplay();
dtrum.disable();
}
}
});
const config = {
userPreferences: {
cookieName: '<SERVICE-NAME>-cookie-preferences',
},
cookieManifest: [
{
categoryName: 'analytics',
cookies: [
'_ga',
'_gid',
'_gat_UA-'
]
},
{
categoryName: 'apm',
cookies: [
'dtCookie',
'dtLatC',
'dtPC',
'dtSa',
'rxVisitor',
'rxvt'
]
}
]
};
cookieManager.init(config);
2. Add the cookie banner to your pages
<div class="govuk-cookie-banner cookie-banner" data-nosnippet="" role="region" aria-label="Cookies on [name of service]" hidden>
<div class="govuk-cookie-banner__message govuk-width-container cookie-banner-message">
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<h2 class="govuk-cookie-banner__heading govuk-heading-m">Cookies on [name of service]</h2>
<div class="govuk-cookie-banner__content">
<p class="govuk-body">We use some essential cookies to make this service work.</p>
<p class="govuk-body">We’d like to set additional cookies so we can remember your settings, understand how people use the service and to improve government services.</p>
</div>
</div>
</div>
<div class="govuk-button-group">
<button type="button" class="govuk-button cookie-banner-accept-button" data-module="govuk-button">
Accept additional cookies
</button>
<button type="button" class="govuk-button cookie-banner-reject-button" data-module="govuk-button">
Reject additional cookies
</button>
<a class="govuk-link" href="/cookies">View cookies</a>
</div>
</div>
<div class="govuk-cookie-banner__message govuk-width-container cookie-banner-accept-message" role="alert" hidden="">
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<div class="govuk-cookie-banner__content">
<p class="govuk-body">You’ve accepted additional cookies. You can <a class="govuk-link" href="/cookies">change your cookie settings</a> at any time.</p>
</div>
</div>
</div>
<div class="govuk-button-group">
<button class="govuk-button cookie-banner-hide-button" data-module="govuk-button">
Hide this message
</button>
</div>
</div>
<div class="govuk-cookie-banner__message govuk-width-container cookie-banner-reject-message" role="alert" hidden="">
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<div class="govuk-cookie-banner__content">
<p class="govuk-body">You’ve rejected additional cookies. You can <a class="govuk-link" href="/cookies">change your cookie settings</a> at any time.</p>
</div>
</div>
</div>
<div class="govuk-button-group">
<button class="govuk-button cookie-banner-hide-button" data-module="govuk-button">
Hide this message
</button>
</div>
</div>
</div>
3. Add the preferences form to your cookies page
<form class="cookie-preferences-form">
<div class="govuk-form-group">
<fieldset class="govuk-fieldset">
<legend class="govuk-fieldset__legend govuk-fieldset__legend--s">
Allow cookies that measure website use?
</legend>
<div class="govuk-radios govuk-radios" data-module="govuk-radios">
<div class="govuk-radios__item">
<input class="govuk-radios__input" id="analytics" name="analytics" type="radio" value="on">
<label class="govuk-label govuk-radios__label" for="analytics">
Use cookies that measure my website use
</label>
</div>
<div class="govuk-radios__item">
<input class="govuk-radios__input" id="analytics-2" name="analytics" type="radio" value="off" checked="">
<label class="govuk-label govuk-radios__label" for="analytics-2">
Do not use cookies that measure my website use
</label>
</div>
</div>
</fieldset>
</div>
<div class="govuk-form-group">
<fieldset class="govuk-fieldset">
<legend class="govuk-fieldset__legend govuk-fieldset__legend--s">
Allow cookies that measure website application performance monitoring?
</legend>
<div class="govuk-radios govuk-radios" data-module="govuk-radios">
<div class="govuk-radios__item">
<input class="govuk-radios__input" id="apm" name="apm" type="radio" value="on">
<label class="govuk-label govuk-radios__label" for="apm">
Use cookies that measure website application performance monitoring
</label>
</div>
<div class="govuk-radios__item">
<input class="govuk-radios__input" id="apm-2" name="apm" type="radio" value="off" checked="">
<label class="govuk-label govuk-radios__label" for="apm-2">
Do not use cookies that measure website application performance monitoring
</label>
</div>
</div>
</fieldset>
</div>
<button class="govuk-button" data-module="govuk-button">
Save
</button>
</form>
4. Round-up
Now when the user updates their cookie preferences, or their preferences are loaded from the preferences cookie (when the page is first loaded), an event is fired which will activate the callbacks we added. These callbacks will
- Adds a variable to the Google Tag Manager (GTM) datalayer which represents
the current cookie preferences. This datalayer variable can be used within GTM
to enable/disable any Google Analytics based tags. The analytics preference is
represented in the
analytics
category - Tell Dynatrace to enable/disable it’s remote user monitoring (RUM) functionality,
based on the value of the
apm
category.
5. Configuring Google Analytics / GTM and Dynatrace.
Google Analytics through GTM
Ensure that you have configured your site and included the Google Tag Manager script for your app within your page. A guide on this can be found here.
With GTM configured and the above event listeners / callbacks added, whenever the user’s preferences are saved or loaded, an event is added to the GTM datalayer.
Next we need to add a variable into GTM, we need to use the Data Layer variable (version 2).
- We need to first set our default value to
off
, as we don’t want to auto-consent for users. - Next we set the
Data Layer Variable Name
tocookiePreferences.analytics
. We’ve named our cookie preferences ascookiePreferences
within the datalayer (set within the callback), and we’re interested in the value theanalytics
sub-property. - Lastly we just need to name and save our variable, I suggest
Analytics Preferences
. This is how it should look:
- We need to first set our default value to
Following the addition of the variable, we need to add a custom trigger event which will fire whenever any event is fired and the
cookiePreferences.analytics
/Analytics Preferences
variable we added is set tooff
.- We need to set the ‘Event name’ field to
*
and enable RegEx matching - Select
Some Custom Events
for the field ‘This trigger fires on’ - Set ‘Fire this trigger when an Event occurs and all of these conditions are true’ to
Analytics Preferences
(the variable from the previous step),contains
thenoff
. - Name it
Analytics off
and hit save. This is how it should look:
- We need to set the ‘Event name’ field to
You now need to add this as an ‘Exception’ trigger on each of your Google Analytics based tags, this should look like:
To ensure that ‘page view’ events are fired correctly after the user has accepted cookies, you need to add a second trigger which listens for the user updating their cookie preferences. We need to add another custom trigger event with the following settings
- ‘Event name’ set to
Cookie Preferences
. - ‘This trigger fires on’ set to
All custom events
- Name it
Cookie preferences
and save it. This should look like:
- ‘Event name’ set to
You now just need to add this as trigger to your Google Analytics page view tag and set the tag to ‘Tag firing options’ within the ‘Advanced tag options’ to
Once per page
. The triggers should look like:
Dynatrace RUM
- To configure Dynatrace RUM, we need to setup the RUM script to be injected through OneAgent.
- After setting up the script to be injected, we need to setup Dynatrace to respect cookie opt-in, this can be done through the ‘Advanced settings’ for an application within Dynatrace.
It should look like this: