Google Analytics and Google Tag Manager Integration
Overview
This page contains a guide on how to integrate the Overlay with analytics platforms such as GA or GTM.
- Instructions for version 1.4
- Instructions for version 1.3
- Instructions for version 1.2
- Instructions for version 1.1
Step-by-step guide
Instructions for version 1.4
Version 1.4 introduces a new way to manage custom events that requires a few changes in the Overlay integration snippet. By default, the Overlay provides callbacks to measure the following user events:
- Query
- Click
- Add to cart
- Opening the Overlay
- Closing the Overlay
And it also provides a callback to run custom code every time a change in the results set happens.
First of all, please initialize the GA or GTM service (it's just a sample, please use the right parameters for your implementation). This piece of code must be in all the pages that contain the search bar. You can find more detailed instructions in these guides:
- https://developers.google.com/analytics/devguides/collection/analyticsjs/
- https://support.google.com/tagmanager/answer/6103696
<!-- Google Analytics --> <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-XXXX-Y', 'auto'); ga('send', 'pageview'); </script> <!-- End Google Analytics -->
For GTM there are two options: adding this code in the HTML head tag or adding this code just before the Google Tag Manager code:
<body> <script> dataLayer = []; </script> <!-- Google Tag Manager --> ... <!-- End Google Tag Manager -->
Finally, you can start defining custom callback functions for each type of event.
1. Search tracking
The data object received by the function contains, at least, the following information:
Parameter | Description |
---|---|
q | The query typed by the user |
page | Page number for grabbing pagination data |
totalHits | Number of results returned by the query |
scope | The scope that has been set up in the Overlay configuration |
lang | The language that has been set up in the Overlay configuration |
origin | Component of the Overlay where the query was originated. The possible values are:
|
originalQuery | Original query sent to the API, useful for spellcheck situations |
Any other parameter returned by Search&Tagging for your instance will also be part of this object, so our recommendation is to explore all the information it contains using the console.
Search Callback Sample - GA
function searchCallback(data) { ga('set', 'page', 'http://site.com/overlay?q=' + data.q); ga('send', 'pageview'); ga('send', 'event', 'EmpathyOverlay', 'search', data.q, data.totalHits); // it's just a sample, you can define the GA metric with other parameters }
Search Callback Sample - Single GTM Tag
This option uses different pieces of code for each event, one Google Analytics trigger, multiple Google Analytics variables, and one Google Analytics tag. The piece of code in the callbacks will be different for different data tracking. If you want to track different data, Google Tag Manager configuration will be the same, and each change must be done in the code.
function searchCallback(data) { dataLayer.push({'eventCategory':'EmpathyOverlay', 'eventAction':'search', 'eventLabel':data.q, 'eventValue':1, 'event':'gaEvent'}); // The chosen keys and values can be different. The keys must have the same name in the code and in Google Tag Manager }
Search Callback Sample - Multiple GTM Tags
This option uses different pieces of code for each event, different Google Analytics events and different Google Analytics tags. The piece of code in the callbacks will always be the same. If you want to change the data for Google Analytics tracking, each change must be done from Google Tag Manager.
function searchCallback(data) { dataLayer.push({'data': data}); dataLayer.push({'event': 'searchCallback'}); }
2. Product related events tracking (click and add to cart)
The data object received by the function contains, at least, the following information:
Parameter | Description |
---|---|
productId | Unique identifier of the product where the event was triggered |
q | The query typed by the user |
page | Page number for grabbing pagination data |
title | Title of the product where the event was triggered |
url | Product page URL |
scope | The scope that has been set up in the Overlay configuration |
follow | Whether the user was automatically redirected to the product page after tracking the event (redirection though EmpathyBroker's Stats API) |
lang | The language that has been set up in the Overlay configuration |
Product Event Callback Sample - GA
function clickCallback(data) { ga('send', 'event', 'EmpathyOverlay', 'click', data.q); // it's just a sample, you can define the GA metric with other parameters }
Product Event Callback Sample - Single GTM Tag
function clickCallback(data) { dataLayer.push({'eventCategory':'EmpathyOverlay', 'eventAction':'click', 'eventLabel':data.q, 'eventValue':1, 'event':'gaEvent'}); // The chosen keys and values can be different. The keys must have the same name in the code and in Google Tag Manager }
Search Callback Sample - Multiple GTM Tags
function clickCallback(data) { dataLayer.push({'data': data}); dataLayer.push({'event': 'clickCallback'}); }
3. Other events
The rest of the events don't provide any additional data to the callback functions and can be tracked in the same way as the above.
4. Update the Overlay initialization snippet using these functions
<script> function queryCallback(data) { /* GA or GTM Code */ } function clickCallback(data) { /* GA or GTM Code */ } function add2cartCallback(data) { /* GA or GTM Code */ } function openCallback() { /* GA or GTM Code */ } function closeCallback() { /* GA or GTM Code */ } function resultsChangeCallback() { /* GA or GTM Code */ } EmpathyOverlay.config({ actionCallbacks: { query: queryCallback, click: clickCallback, add2cart: add2cartCallback, open: openCallback, close: closeCallback, resultsChange: resultsChangeCallback } }); </script>
Please note that this Overlay feature can be used to run custom code of any other nature, not necessarily related with analytics.
5. Next steps (GTM only)
Single GTM Tag
- Go to Google Tag Manager to continue with the integration process. No more code changes are needed.
- In Google Tag Manager you have to add triggers. To do it:
- Go to Triggers
- Click on New
- Name: gaEvent
- Choose Event: Custom Event
- Fire On:
- Event name: gaEvent
- Create Trigger
- Then, you have to create variables for received data in each event. The variables will always be the same, and they must have the dataLayer key names (In this case: eventCategory, eventAction, eventLabel and eventValue). For each of those variables repeat this process:
- Go to Variables
- At the bottom of the page, in the User-Defined Variables section, click New
- Name: One of the dataLayer key names (eventCategory, eventAction, eventLabel and eventValue)
- Choose Type: Data Layer Variable
- Configure Variable:
- Data Layer Variable Name: Same name as the one in point c.
- Data Layer Version: Version 2
- Create Variable
- Finally, a Google Analytics tag must be created. To do it:
- Go to Tags
- Click on New
- Name: gaEvent
- Choose Product: Google Analytics
- Choose a Tag Type: Google Analytics option you are using
- Configure Tag:
Tracking ID: Tracking ID de Google Analytics (UA-XXXXXXXX-X)
Track Type: Event
Category: {{eventCategory}} //Variable created on point 5
Action: {{eventAction}} //Variable created on point 5
Label: {{eventLabel}} //Variable created on point 5
Value: {{eventValue}} //Variable created on point 5
Fire On: More > Choose from existing Triggers: gaEvent
Create Tag
Multiple GTM tags
- Go to Google Tag Manager to continue with the integration process. No more code changes are needed.
- In Google Tag Manager you have to add triggers. The next process has to be done for each event:
- Go to Triggers
- Click on New
- Name: Event Name (ebClickCallback, ebSearchCallback or ebConversionCallback)
- Choose Event: Custom Event
- Fire On:
- Event name: The same name of c step.
- Create Trigger
- Then, you have to create variables for received data in each event. The next process has to be done for each data:
- Go to Variables
- At the bottom of the page, in the User-Defined Variables section, click New
- Name: It should identify the data it represents. For example: Product Name
- Choose Type: Data Layer Variable
- Configure Variable:
- Data Layer Variable Name: One of the Data column values of available data table.
- Data Layer Version: Version 2
- Create Variable
- Finally, Google Analytics tags must be created. These tags depends on the chosen Google Analytics metrics, so the next process describes a general example to track a product click:
- Go to Tags
- Click on New
- Name: GA Overlay Product Click
- Choose Product: Google Analytics
- Choose a Tag Type: Google Analytics option you are using
- Configure Tag:
Tracking ID: Tracking ID de Google Analytics (UA-XXXXXXXX-X)
Track Type: Event
Category: Overlay
Action: {{Event}}
Label: {{Product Name}} (Variable created in Variables section. It represents the product name)
Value: Empty
Fire On: More > Choose from existing Triggers: ebClickCallback
Create Tag
With this tag, when a user clicks in a product, an event in Google Analytics is created with these values:
Event Category: Overlay
Event Action: ebClickCallback
- Event Label: Clicked products' name
Instructions for versions 1.2 and 1.3
Note: GTM instructions for these versions live in this separate page.
By default, the Overlay provides callbacks to measure queries and clicks using the GA API in addition to EmpathyBroker Tagging. Simply you should define two functions, one for sending query information to GA and another one for sending clicks information.
First of all, please initialize the GA service (it's just a sample, please use the right parameters for your implementation). You can find more information in this guide.
<!-- Google Analytics --> <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-XXXX-Y', 'auto'); ga('send', 'pageview'); </script> <!-- End Google Analytics -->
1. Search tracking
The data object received by the function will contain:
Parameter | Description |
---|---|
q | The query typed by the user |
page | Page number for grabbing pagination data |
totalHits | Number of results returned by the query |
Sample (the function name can be modified)
function searchCallback(data) { // ga('set', 'page', 'http://site.com/overlay?q=' + data.q); // ga('send', 'pageview'); // ga('send', 'event', 'Search-Overlay', 'search', data.q, data.totalHits); // it's just a sample, you can define the GA metric with other parameters }
2. Product click tracking
Let us know what data you want to receive in your custom function, by default the title of the clicked resource will be included.
Parameter | Description |
---|---|
q | The query typed by the user |
page | Page number for grabbing pagination data |
totalHits | Number of results returned by the query |
title | Title of clicked resource |
Sample (the function name can be modified)
function productClickCallback(data) { // ga('send', 'event', 'Search-Overlay', 'click', data.q); // it's just a sample, you can define the GA metric with other parameters }
3. Update the Overlay creation using these functions
<link rel="stylesheet" href="https://assets.empathybroker.com/overlay/1.2/css/eb.resources.min.css" /> <script type="text/javascript" src="https://assets.empathybroker.com/overlay/1.2/js/eb.libs.min.js"></script> <script type="text/javascript" src="https:/assets.empathybroker.com/overlay/1.2/js/eb.resources.min.js"></script> <script type="text/javascript"> function clickCallback(data) { //GA Code } function searchCallback(data) { //GA Code } function conversionCallback(data) { //GA Code } EmpathyBrokerUI.config('https://{EB_URL}/{EB_INSTANCE_NAME}', { lang: 'en', displayLang: 'en', defaultCurrency: 'EUR', scope: 'default', pageRows: 12, customTrackSearch: searchCallback, customTrackClick: clickCallback, customTrackConversion: conversionCallback }, document.getElementById('EmpathyBrokerUI')); </script>
Instructions for version 1.1
Note: GTM instructions for these versions live in this separate page.
By default, the Overlay provides callbacks to measure queries and clicks using the GA API in addition to EmpathyBroker Tagging. Simply you should define two functions, one for sending query information to GA and another one for sending clicks information.
First of all, please initialize the GA service (it's just a sample, please use the right parameters for your implementation). You can find more information in this guide.
<!-- Google Analytics --> <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-XXXX-Y', 'auto'); ga('send', 'pageview'); </script> <!-- End Google Analytics -->
1. Search tracking
The data object received by the function will contain:
Parameter | Description |
---|---|
term | The query typed by the user |
page | Page number for grabbing pagination data |
numFound | Number of results returned by the query |
Sample (the function name can be modified)
function searchCallback(data) { // ga('set', 'page', 'http://site.com/overlay?q=' + data.term); // ga('send', 'pageview'); // ga('send', 'event', 'Search-Overlay', 'search', data.term, data.numFound); // it's just a sample, you can define the GA metric with other parameters }
2. Product click tracking
Let us know what data you want to receive in your custom function, by default the title of the clicked resource will be included.
Parameter | Description |
---|---|
name | Document name or title |
Sample (the function name can be modified)
function productClickCallback(data) { // ga('send', 'event', 'Search-Overlay', 'click', data.term); // it's just a sample, you can define the GA metric with other parameters }
3. Update the Overlay creation using these functions
<link rel="stylesheet" href="https://assets.empathybroker.com/overlay/{VERSION}/css/eb.resources.min.css"/> <script type="text/javascript" src="https://assets.empathybroker.com/overlay/{VERSION}/js/eb.libs.min.js"></script> <script type="text/javascript" src="https:/assets.empathybroker.com/overlay/{VERSION}/js/eb.resources.min.js"></script> <script type="text/javascript"> function clickCallback(data) { // GA Code } function searchCallback(data) { // GA Code } function conversionCallback(data) { // GA Code } EmpathyBrokerUI.config('https://{EB_URL}/{EB_INSTANCE_NAME}', { lang: 'en', displayLang: 'en', defaultCurrency: 'EUR', scope: 'default', pageRows: 12, customTrackSearch: searchCallback, customTrackClick: clickCallback, customTrackConversion: conversionCallback }, document.getElementById('EmpathyBrokerUI')); </script>
Do you need additional GA metrics to be applied into the Overlay?
Please contact EmpathyBroker Support.