Prerequisites for GTM integration
You can use Google Tag Manager to add Acoustic Personalization tags to your Single-Page Application (SPA) or Multi-Page Application (MPA).
Assumptions
Before proceeding with Google Tag Manager (GTM) integration with Acoustic Personalization, ensure
that the following prerequisites and assumptions are fulfilled.
- This topic covers Google Tag Manager implementation for Single-Page Applications (SPA) or Multi-Page Applications (MPA).
- GTM tags are used with Google Analytics – Universal Analytics.
- You must have a GTM account configured with a Google Analytics container.
- The Google Analytics settings variable should be created as a User-defined variable. In this topic, the Google Analytics setting variable is named as GA-Id. Also, enable the Ecommerce option for this variable by selecting the Enable Enhanced Ecommerce features and Use data layer check boxes.
- The event actions in the tags must use the standard Google nomenclature.
Creating a Page View tag
It is required to have a Page View tag for each page in your website. If you
do not have this tag, create it with the details as below:
- Tag Type: Google Analytics – Universal Analytics
- Track Type: Page View
- : Select the Google Analytics Settings variable (in this case, GA-Id that you created as a prerequisite (step 4 in Assumptions).
- Tag Firing Options: Once per page
- : All Pages
Adding GTM tracking code to your SPA or MPA
- In your Single-Page Application, add the following tracking code at the beginning of HEAD tag of the index.html page.
- In your Multi-Page Application, add the following tracking code at the beginning of HEAD tag of each HTML page.
<script>
(function (w, d, s, l, i)
{
w[l] = w[l] || []; w[l].push({'gtm.start': new Date().getTime(), event: 'gtm.js'});
var f = d.getElementsByTagName(s)[0],
j = d.createElement(s), dl = l != 'dataLayer' ? '&l=' + l : ''; j.async = true; j.src =
'https://www.googletagmanager.com/gtm.js?id=' + i + dl; f.parentNode.insertBefore(j, f);
})(window, document, 'script', 'dataLayer', 'GTM_ID');
</script>
Creating custom variables in Google Tag Manager
Create the following custom variables in Google Tag Manager.
- Create a user-defined variable gtmDimensionVariable in Google Tag Manager as
shown below.
- Variable Type: Data Layer Variable
- Data Layer Variable Name: dimension1
- Data Layer Version: Version 2
- Next, create another custom variable named SendHitTaskOverrideVariable of type Custom JavaScript.
- Add the following code snippet to this custom variable. Note:
- Note that the snippet uses gaClientID as an example. You must decide the identifier and use the same.
- Update the identifiers mapper array according to the UBX unique identifier types
function() { return function(model) { var globalSendTaskName = '_' + model.get('trackingId') + '_sendHitTask'; var clientId = model.get('clientId'); var identifiersMapperArray = []; identifiersMapperArray.push({ "name": "gaClientId", "value": clientId }); var originalSendHitTask = window[globalSendTaskName] = window[globalSendTaskName] || model.get('sendHitTask'); model.set('sendHitTask', function(sendModel) { originalSendHitTask(sendModel); var payLoad = sendModel.get('hitPayload'); var hitType = model.get("hitType"); //var searchKeyword ; this must be declared globally on the website, and holds value of the search term if (hitType=="pageview" && typeof searchKeyword !== 'undefined' && searchKeyword !== null) { var result = {}; payLoad.split("&").forEach(function (part) { var item = part.split("="); result[item[0]] = decodeURIComponent(item[1]); }); var eveObj = google_ubx.mapToUBXEvent(result, google_ubx.googleToUBXPageViewEvent); eveObj.attributes.push({"name":"searchTerms", "value": searchKeyword, "type": "string"}); eveObj.identifiers = identifiersMapperArray; eveObj.eventCode = "ibmsearchedSite"; ibm_ubx.sendEvent(eveObj, google_ubx.host, google_ubx.authKey,"GOOGLEANALYTICS"); } else{ google_ubx.sendEventFromPayload(payLoad, identifiersMapperArray); } }); }; } - In your Google Analytics settings variable (GA-Id), set the references to
custom variables as shown below.
- : customTask, Value: {{SendHitTaskOverrideVariable}}
- : 1, Dimension Value: {{gtmDimensionVariable}}
- Next, enable the following in-built GTM variables:
- Container ID
- HTML ID