We only want to show our live chat tag Monday to Friday, from 09.00 until 17.00. Is this possible with Google Tag Manager please?


2 Answers 2


Preferred solution

The best solution is probably to get the current date on the server side and push to the datalayer a variable that determines whether the chat tag should be triggered or not depending on this date.


The alternative is to retrieve the date on the client side which won't be 100% reliable. In order to use this solution, you need to create a Custom Javascript variable with the following code (replace "America/New_York" by your business' timezone) :

function (){
    const currentDate = new Date(new Date().toLocaleString("en-US", {timeZone: "America/New_York"}));
    const day = currentDate.getDay()
    const hours = currentDate.getHours()
    return day > 0 && day < 6 && hours >= 9 && hours < 17

Then, trigger your chat tag only when this variable's value is "true".

  • For the first solution, you need to factor in that the timezone of the server might be different than that of the client. For the second solution, custom tags and variables require that "eval" is allowed, which might not be that case if you have a strict CSP (and custom templates do not have the Date object). Dec 16, 2021 at 13:35
  • Thanks both! We're a UK-only website with UK-based servers, so the server side should work. We'll give this a go.
    – SHMC
    Jan 14 at 9:18

If you want to get a consistent, reliable date, ListenLayer.com returns a timestamp with every data dataLayer push. The time can appear in a single account timezone based on your account settings, so if you align your ListenLayer timezone to your chat time zone it will work.

You can then write rules in the platform to determine if the time returned should allow for chat. You will output a yes/no into a new data layer variable based on the rule. Then you will register this variable in GTM and use it to determine if the chat tag should fire.

Here is the process I would use at a high level

  1. Create an account and set the account time to match your chat timezone

  2. Turn on the User Source Listener and all it's features, enable it to push on every page load This will push the user's traffic source into the data layer, along with a bunch of other structured data on each page load. You could use another listener, but this one will allow us to ensure it pushes on every page load (container load) so that GTM will know on every page load whether to trigger the chat script.

  3. Create a custom data layer variable called chatTime. We will write a rule in ListenLayer to push a yes or a no into this variable based on the time that is returned in the userSource data layer event. This will happen in real time, in the same event in the data layer.

  4. Publish everything in listenlayer and then go to your website with the console open (the data layer is written there for ease). You'll see the userSource dataLayer event in the console on every page load. It will contain an eventTimestamp. You want the data that is inside the listenlayerAccountTime node because it will match your ListenLayer timezone, which should match the timezone you operate your chat hours from (or if all of this is based on the user's time zone, use that one to be localized to them). You will need to focus on these two variables that are automatically provided because we will write a rule based on them in step 5:



Here is a visual showing those variables I have given a path to above. One of the day of the week and one is the time of day.

  1. Inside ListenLayer, under the User Source listener you will create two rules, they will be lookup table rules. The first rule will run first, then the second. If a match is found, they will stop. This means we can write the first rule to basically just look at the day of the week and set chatTime to no if the value is Sat or Sun. The second rule will then focus on the time of the day since this rule will only be reached if it is not Saturday or Sunday. In this rule we will look at the first part of the timeOfDay variable using simple ReGex. Our rule has 24 rows for each hour of the day and will return a yes only for the hours you are open for chat. Certainly this rule could be made simpler with some additional regex, but this gives a better illustration. There is an import export so you could spin up the 24 row rule in a csv file and import it.
  2. After you save and publish this simple logic, your website will have a value in the data layer telling google tag manager whether chat should be on. It will appear in the data layer every time the userSource event is pushed, which is every page load.
  3. I assume things are easy from here. You would create a data layer variable inside GTM referencing sourceCustomValues.chatTime You'll also create a custom event trigger referencing userSource and only firing if chatTime = yes. Then you will set your chat tag to fire on this trigger.
  • Thanks screen1! I'd not heard of ListenLayer. It looks like a great tool. We'll go with this solution if the first doesn't work as expected.
    – SHMC
    Jan 14 at 9:21

Your Answer

