top of page

A Beginner's Guide to Time Dependent Triggers in Google Tag Manager

Updated: Jul 29, 2023


With the help of the wonderfully helpful Google Tag Manager (GTM) tool, you can deploy and manage marketing tags to your website without having to change the source code.


GTM is the ideal tool for a rapid and painless setup of tracking, supporting anything from Google Analytics to the Facebook Pixel and any other measurement tag you can imagine.


However, there are situations when GTM's default capability is insufficient. Maybe firing a tag whenever a particular page loads or a button is pressed isn't specific enough. Triggers can help with that; one is the Scroll Depth Trigger.


Time Dependent Triggers


You must be inventive if you only want a tag to fire within a specified time period or on a particular day of the week. You can schedule your tags in GTM by using the time-specific trigger solutions we'll discuss in this post.


The Use Case


Let's first examine the potential benefits of this strategy of restricting tags to specific time frames.


You should have tags firing continuously, gathering data for each visitor who visits the site, for the majority of monitoring code, such as Facebook or Google Analytics. However, some tags may only be appropriate to remove during regular business hours or on a specific day of the week.


Think about a scenario where you wish to integrate GTM with Facebook Messenger Chat. Only use this contact option live on site during business hours, when someone is available to chat in real time, if your company needs to swiftly answer to customers.


Or how about a promotional code that is only valid on Wednesdays? A pop-up promoting their 15% off "We Love Wednesdays" promotion from GTM may be installed on an e-commerce website. However, displaying it to the public on a Monday is absurd.


The solution: time-specific triggering


Direct Tag Scheduling


There are some sophisticated parameters available when creating a tag in Google Tag Manager. It's unlikely that you would have found this sub-section with a conventional implementation, but it's the quickest way to have more control over your tags.



The option to enable a "custom tag firing schedule" can be found in Advanced Settings. This includes a beginning date, beginning time, ending date, and ending time. The tag will only fire during the specified time period once all four fields have been filled in and the modifications have been published.


The Facebook Messenger Chat tag can now be fired on Monday during work hours, which is brilliant. But what about the following Monday, Tuesday, or Wednesday? Instead of something with a set time frame, we need something with a repeating timetable.


Day of the Week


Google Tag Manager works in JavaScript. Dates and timings are quite simple to work with in JavaScript. The simple method that returns the current day of the week is provided below.


function () {
 // Enter your timezone name here
 // https://en.wikipedia.org/wiki/...
 var timezone = 'Asia/Kolkata'
 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()];
}

Copy the entire function and paste it into a Custom JavaScript variable to utilise it in your GTM container. Change the "timezone" variable to suit the location of your company. By doing this, you can make sure that the tags operate in accordance with your business timetable and not someone else's.


Once configured, this unique JavaScript variable will accept one of the following seven values: Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, or Saturday. You can use these seven values to base trigger conditions on. Our Wednesday special offer pop-up will be launched using the trigger shown below.



Hour of the Day


Even though we were able to limit it to certain days of the week, we still need something more specific to make sure that our Facebook Messenger Chat widget only activates during business hours. We cannot anticipate you responding to potential clients at three in the morning!


The below function returns the current hour.


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

Copy the entire function and paste it into a Custom JavaScript variable to utilise it in your GTM container. Update the timezone variable to suit the location of your office, just as you would with the Day of Week function.


An hour of the day, between 0 and 23, will be represented by the variable. You can use "greater than" and "less than" logic to create conditions in your trigger. The example below will only fire a tag from 9 am to 5 pm.



Bringing it All Together


We can now combine the Day and Hour variables we generated earlier to make incredibly precise timetables. With the following reasoning, we may combine days using RegEx and only fire tags on weekends.



Alternately, decide to fire tags with a variety of conditions just on Monday and Tuesday mornings.



In order to be even more exact, these additional requirements can be paired with conditions on common variables, such as Page Path. The user must visit the blog on a Monday or Tuesday for this trigger to fire a tag.



Get in touch with me if you require assistance with time-based triggers or if you have any queries about Google Tag Manager.

36 views0 comments

Recent Posts

See All

Comments


Post: Blog2_Post
bottom of page