In the past few weeks, I had the goal to improve the coveo.analytics.js project and its integration into Google Tag Manager.
I wanted to leverage its API to automatically deploy tags and variables that people would fill to easily configure page views and custom events in an existing Google Tag Manager workspace.
Then, magic happened, and the Google Tag Manager team released on May 23rd Custom Templates, which serves the exact purpose of what I wanted to integrate using the API.
This post explains how to use Coveo’s official Google Tag Manager templates.
Importing the Coveo Scripts
Thanks to the Google Tag Manager team and their kind and helpful reviews, the Coveo scripts are available in the GoogleTagManagerTemplates repository.
You need to download the two .tpl
files located in the sub-folders of this link.
Then, in the Templates
section of your Google Tag Manager workspace, create a new Tag Templates. Click on the menu button on the top right corner, and then Import
.
Start by importing both templates.
The Coveo Analytics Load Script.tpl
template allows you to include the coveo.analytics.js
script in your page.
The Coveo Analytics Log Event.tpl
template is the tag that sends the actual analytics event to Coveo Cloud.
Loading the Script in Your Pages
In the Tags
section of your Google Tag Manager workspace, create a new Tag. You should now see Coveo Analytics Load Script. Select it.
The tag requires a Coveo Analytics API key. For this, you need to get into your Coveo Cloud instance and create an API key with the Push
access level on the Analytics Data
privilege. I recommend creating a variable with this value so that you can easily change it later.
Set the script version to the latest release version of the coveo.analytics.js release (at least 1.0).
Add a new trigger to this tag, preferably on Page View
so that the script is loaded in all of your site pages.
Your final tag should look like this one:
You can validate that it is working by entering Preview mode in Google Tag Manager and visiting your page.
Logging Custom Events in Coveo Cloud
For this example, let’s say you want to track all clicked links on your site in your Coveo Analytics Reports.
Create a new Tag based on Coveo Analytics Log Event
.
Select Custom
as the Event Type, if not already selected.
Choose a custom event type. This is the name that appears in your Coveo Cloud Analytics visits.
Select an Event Value. In this case, the event needs to be associated with the clicked URL, so it would be wise to use Google Tag Manager’s Built-in Variable Click URL
.
You also need to provide the Language
key. Create a Page Language
variable and assign its value with the method of your choice.
You can also provide additional metadata, filled from other variables. In this case, I wanted to track whether the user was a returning customer when they clicked the link, so I added the isReturningCustomer
key and assigned its value to a variable. For it to appear in Coveo Cloud, I had to create a custom dimension on the isReturningCustomer
custom event metadata.
Your final tag should look like this one:
Once you load Preview mode in Google Tag Manager and visit your site, every link clicked will be logged into Coveo Cloud as part of the current visit.
You can then track your visit in Coveo Cloud in the Visit Browser.
Now, you are ready to create some reports on this event!
About Logging Coveo Events in Google Services
While this article is about logging Coveo Events from Google Tag Manager, there is also some work done to log events the other way around: from Coveo to Google Tag Manager.
The feature allows your Coveo Search Interface to automatically fill the dataLayer
with Coveo metadata. This makes it very easy to leverage Coveo events directly from Google Tag Manager.
It will be available pretty soon in the next JavaScript Search Framework release! For more technical details, you can see this pull request.
Next steps
This example here is just basic event reporting for Coveo Cloud through Google Tag Manager. The template currently only supports custom
and view
events, but you can get creative with just custom
events to get all kinds of reporting in Coveo!
Coveo plans to support more types of events in the future. Stay tuned for improvements on the templates!