Calculating the time-to-event metric in Google Analytics

time-to-event metric

Google Analytics does not calculate the time-to-event metric – how long did it take a user to click on a button, or fire any other event.
All of the pages reports are very rich, so much more than the events.
1. We can see how many times a page is viewed
2. How many time as unique (distinct views in session)
3. How much time users spent on each page.
4. Which pages are the entrance page
5. Which are the exit pages
And much more

In this article, I’m going to show you how to collect with the Google Tag Manager (GTM) the time-to-event metric. Then present in Google Analytics, how long did it take a user to click on a button, or fire any other event.
See how we deal with gaming custom metrics

Creating the time-to-event metric, step by step

There are 7 steps we need to do starting in implementation in GTM until the final report in Google Analytics:
1. Create a Variable that collects the current TimeStamp (GTM)
2. Create a cookie that contains the second a user started viewing a page (GTM)
3. Collect the timestamp that we stored in the cookie (GTM)
4. Creating a Variable that calculates the interval from page load to this current event (GTM)
5. Creating an event interval custom metric (GA)
6. Firing the Interval variable on each event tag (GTM)
7. Creating a custom report that contains the custom metric (GA)

1. Create a Variable that collects the current TimeStamp

In order to create this Variable
a. click on the button NEW under the variables.
b. Choose a Custom JavaScript variable
c. Insert the following code:

function() {
var now = new Date();
return now.getTime();
}

d. Save this variable as TimeStamp
time-to-event metric - Creating current TimeStamp variable

2. Create a cookie that contains the second a user started viewing a page (GTM)

In order to create a cookie that contains the timestamp that the user entered the page you need to:
a. create a Custom HTML Tag
b. Paste the following code:

// ADD HERE THE SCRIPT SYNTAX (see in the image below)
var cookieName = ‘LoadPageTS’;
var cookieValue = {{TimeStamp}};
var myDate = new Date();
myDate.setMonth(myDate.getMonth() + 24);
document.cookie = cookieName +”=” + cookieValue + “;expires=” + myDate
+ “;domain=OptimizingAudience.com;path=/”;
// ADD HERE THE SCRIPT SYNTAX (see in the image below)

c. Save this tag (the naming doesn’t matter)
time-to-event metric - Creating a loadPageTS cookie

3. Collect the timestamp that we stored in the cookie (GTM)

In order to Collect the timestamp that we stored in our first party cookie, we need to:
a. Create a 1st party cookie variable.
b. Insert the cookie name you just created: LoadPageTS
c. Name this variable as LoadPageTS
time-to-event metric - Collect the timestamp from the cookie

4. Creating a Variable that calculates the interval from page load to this current event (GTM)

In order to create this variable that calculates the time since page was loaded until user trigerd the event you need to:
a. Create a Custom JavaScript variable
b. Insert the following code:

function() {
var timeInterval = ( {{TimeStamp}} – {{LoadTS cookie}} ) / 1000;
return timeInterval;
}

c. Save this variable as timeInterval
time-to-event metric - Creating a Variable that calculates the interval from page load to this current event

5. Creating an event interval custom metric (GA)

In Google Analytics create a new Custom metric.
Name it seconds to event, make it as Time format
Remember the index of your custom metric
time-to-event metric - Creating an event interval custom metric

6. Firing the Interval variable on each event tag (GTM)

In order to fire the Interval variable on each event tag
a. In Google Tag Manager, open the event tag you want to fire this measure
b. Go to More Fields -> Custom Metrics
c. Insert the index from section 5 and under the metric value, insert {{timeInterval}}
time-to-event metric - Firing the Interval variable on each event tag

7. Creating a custom report that contains the custom metric (GA)

Now you have all the data and you can create the report in Google Analytics
a. Under custom reports click on + New Custom Report
b. Insert the following as metrics: Total Events, Unique Events, Event Value, and seconds to event
c. Under the Dimension Drilldown add the following: Event Category, Event Action, and Event Label
time-to-event metric - Creating a custom report that contains the custom metric

Learn Google Analytics, at your own pace, from wherever you are, with the best platform!

Start the course now (free)