Set up Google Analytics with Google Tag Manager
You can use Google Tag Manager and Google Analytics to enable traffic or page view analysis of Sitecore Content Hub data.
To set up Google Analytics with Google Tag Manager:
-
Create an account for Google Analytics. You need one Google Analytics account per Content Hub instance.
-
In Content Hub, set up and activate the Google Tag Manager.
-
In the Google Tag Manager, in the upper-left corner, click the account name (for example, mycompany.com) and then click the Google Analytics tab.
-
On the Google Analytics page, click Get started and follow the prompts.
-
In each Google Analytics account, enable GA4 to get a measurement ID.
-
To use the measurement ID and receive traffic into Google Analytics, configure Google Tag Manager by adding a new tag to its settings and use the GA4 configuration option.
-
Copy the measurement ID from step 6 to the new tag. This tag should be triggered on all page views to get the traffic.
-
Set Google Tag Manager to a version and publish.
To test your connection, look at the network tab in your browser where Content Hub is open. If you see traffic with collect.js, this means your traffic is being pushed to Google Analytics.
Example
To send information about custom actions and events to Google Tag Manager, use window.dataLayer.push:
const EVENT_NAME = "ENTITY_SAVED";
export default function createExternal (container) {
let eventHandler = null;
const removeListener = () => {
if (eventHandler) {
window.removeEventListener(EVENT_NAME, eventHandler);
eventHandler = null;
}
};
return {
render: context => {
removeListener();
eventHandler = event => {
window.dataLayer.push({
event: "demoGTM",
entityId: context.options.entityId,
definitionName: event.detail.definitionName,
description: 'This is sample code using window.dataLayer.push'
});
}
window.addEventListener(EVENT_NAME, eventHandler);
},
unmount: () => {
removeListener();
}
};
};
See the data layer for information about using window.dataLayer.push().