Google Tag Manager (GTM) is an incredibly useful tool that enables you to manage and deploy marketing tags to your website without having to modify the source code.

From Google Analytics to the Facebook Pixel - and any measurement tag you can think of besides - GTM is the perfect tool for a quick and hassle-free implementation of tracking.

Sometimes, however, the out-of-the-box functionality of GTM isn’t quite enough. Perhaps firing a tag every time a certain page loads or whenever a button is clicked isn’t specific enough. That’s where triggers come in - for instance, the Scroll Depth Trigger…

Time Dependent Triggers

If you want a tag to only fire during a specific time range, or on a certain day of the week, you need to get creative. In this post, we’ll explore some time-specific trigger solutions to help you schedule your tags in GTM.

The Use Case

Firstly, let’s explore why this approach of limiting tags to certain time periods might be useful.

For most tracking code - Google Analytics or Facebook, for instance - you’ll want to have tags firing continually, collecting data for every user who visits the site. But for some tags it might only be relevant to fire them during office hours, or on a particular day of the week.

Consider Simo Ahava’s guide to embedding Facebook Messenger Chat with GTM. A business that wants to quickly respond to customers will only want this contact method live on site during the working day, when someone is available to chat live.

Or what about a discount code that’s only available on Wednesdays? An ecommerce website may want to install a pop-up showcasing their ‘We Love Wednesdays’ 15% off deal via GTM. But, it makes no sense to show it to people on a Monday.

Time-specific triggering to the rescue!

Direct Tag Scheduling

When you create a tag in Google Tag Manager there are some advanced settings you can play around with. With a standard implementation it’s unlikely you’ll ever have unearthed this sub-section, but it’s the simplest way to get extra control over your tags.

Google-Tag-Manager-Triggers

Within Advanced Settings is the option to enable a ‘custom tag firing schedule’. This consists of a start date, start time, end date, and end time. Once you’ve entered all four fields and published the changes, the tag will only fire during the defined time period.

Brilliant, this means we can fire that Facebook Messenger Chat tag on Monday, during business hours. But what about Tuesday, or Wednesday, or Monday the following week? We need something with a recurring schedule, rather than a fixed time range.

Day of the Week

Google Tag Manager works in JavaScript. In JavaScript it’s really easy to deal with dates and times. Below is a short function that returns the current day of the week.

function () {
 // Enter your timezone name here
 // https://en.wikipedia.org/wiki/...
 var timezone = 'Europe/London'
 var localTime = new Date();
 var time = new Date(localTime.toLocaleString('en-US', {timeZone: timezone}));
 var daysOfWeek = new Array(7);
 daysOfWeek[0]= "Sunday";
 daysOfWeek[1] = "Monday";
 daysOfWeek[2] = "Tuesday";
 daysOfWeek[3] = "Wednesday";
 daysOfWeek[4] = "Thursday";
 daysOfWeek[5] = "Friday";
 daysOfWeek[6] = "Saturday";
 return daysOfWeek[time.getDay()];
}

To use this in your GTM container, copy the whole function and paste it into a Custom JavaScript variable. Update the ‘timezone’ variable to reflect where your business is based. This will ensure the tags fire according to your business hours, rather than the schedule of someone on the other side of the world.

Once set up, this custom JavaScript variable will take one of seven values – Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, or Saturday – which you can build conditions on, within a trigger. The below example is a trigger that will be used to fire our Wednesday special offer pop-up.

Time-Dependent-Triggers-GTM

Hour of the Day

We’ve been able to narrow it down to specific days of the week, but we need something even more granular to fire our Facebook Messenger Chat widget only during office hours. We can’t expect “Susan” to be replying to potential customers at 3am!

The below function returns the current hour.

function () {
 // Enter your timezone name here
 // https://en.wikipedia.org/wiki/...
 var timezone = 'Europe/London'
 var localTime = new Date();
 var time = new Date(localTime.toLocaleString('en-US', {timeZone: timezone}));
 return time.getHours();
}

To use this in your GTM container, copy the whole function and paste it into a Custom JavaScript variable. As with the Day of Week function, update the timezone variable to reflect where your office is located.

The variable will be a number between 0 and 23, representing an hour of the day. In your trigger you can apply ‘greater than’ and ‘less than’ logic to build conditions. The below example will only fire a tag between 9am and 5pm.

GTM-Time-Triggers

Bringing it All Together

Now that we’ve built our Day and Hour variables, we can combine them to create very specific schedules. Using RegEx we can combine days and fire a tag just on weekends with the following logic.

Or choose to fire a tag only on Monday and Tuesday mornings with multiple conditions.

These new conditions can also be combined with conditions on standard variables, like Page Path, to be even more specific. This trigger will only fire a tag when the user visits the blog on a Monday or Tuesday.



If you need a hand with time-based triggers, or have any questions about Google Tag Manager, get in touch with the SearchStar team here...