GTM Trigger for X days after the first user visit

What if you need a trigger that checks if X days passed since the first visit of your returning visitor? It’s very specific trigger, but it could be used in a lot of situations (triggering surveys or polls for returning visitors, triggering discount codes, using it in custom remarketing lists, etc…).

On first sight it seems impossible to create such a trigger (Yes, GA has this information, but there is no way GA could send this info to GTM). However it’s easier to do this than you think! Just follow my steps and you can create your own custom triggers for returning visitors based on the days after their first visit!

The GA Client ID Variable

I wrote about extracting the Client Id from the GA cookie before (and you’ll need to check it out, as it’s vital part of this trigger setup). As mentioned on that article – the GA cookie contains the timestamp of your first visit for any website.

So – the first step is creating the Client Id Variable:
Get the GA Client ID with GTM

Again – check this GTM article for more information about this process. If you already have this tag – you can go directly to the next step:

Creating the First Visit Timestamp Variable

Now, as you have the Client ID Variable – you’ll need to extract the timestamp of the first visit. Create new Custom JavaScript Variable, name it (in this example I’ll call it {{CJS - firstVisitTimestamp}}) and paste the following code:

function() {
	var gaCid = "{{CJS - Client ID}}";
  	if(gaCid != "n/a") {
        var firstVisitTimestamp = gaCid.split('.')[1]; 
	return firstVisitTimestamp;
    } else {
        return null;

Have in mind, that you need to use Universal Analytics in order to use this script!

Creating the Current Visit Timestamp Variable

The next step is easier – you just need to create a Custom JavaScript Variable, that records the current timestamp of the visitor (we created something similar in the Timestamp Custom Dimension article before, this one is more simplified version of the same script. Create your variable (In this example it’s named {{CJS - currentTimestamp}}) and add the following code:

function() {
  var time =;
  var finalTimestamp = time.toString().substring(0,10);
  return finalTimestamp;

Here we are using the substring to get just the first 10 symbols of the current timestamp, as the GA cookie stores just the first 10 too.

Creating the Time Calculation Variable

Now it’s time to do the math and check if the time you want since the users first visit has passed or not. For this example we’ll create a trigger that fires 2 days after the first visit of the user.
First – create a new Custom JavaScript Variable and name it – for this examle I’ll give it a name {{CJS - 48h since First Visit}}. Then add the following code:

function() {
  var firstTime = "{{CJS - firstVisitTimestamp}}";
  var currentTime = "{{CJS - currentTimestamp}}";
  var timestampDiff = currentTime - firstTime;
  var hoursPassed = 48;
  var timePassed = hoursPassed*60*60;
  if(timestampDiff >= timePassed) {
  	return true;
  } else {
    return false;

On Line 5 you can change the value to another, per your needs.
The script returns true if the time you want passed (2 days in this example), otherwise it returns false

The Trigger

Congratulations, you are done! Now you need to create the trigger and use it whenever you need it.
Choose “Window Loaded” or “Dom Loaded” as trigger type (I use Window Loaded” just in case), so you are sure all the calculations are done before executing the trigger. Afterwards just choose your variable from the previous step to be equal to true:
The GTM Trigger for 2 days after first visit

You can tweak this trigger per your need, it gives you very powerful custom way to separate your users depending of their recency. If you have any feedback or suggestions – I’ll be happy to read them in the comments!

Check these related articles:


  1. Hi Emil.

    You have another post where you customize the code of the timestamp so it returns you the day, month, and year. Title: “Add Timestamp to all of your GA Events”.

    I’ve been trying to capture the day, month and year of the first visit of the user, do you know how would I customize your code of the firstVisitTimestamp so I have that information available? What I get know is number like 1590773118.

    Would it be something like this? I’m not (very) knowledgeable in JavaScript.

    function() {
    var gaCid = “{{cjv – Client ID}}”;
    if(gaCid != “n/a”) {
    var firstVisitTimestamp = gaCid.split(‘.’)[1];
    return time[3]+ ” ” +time[1]+ ” ” +time[2]+ ” “+time[4];
    } else {
    return null;

    Thank you very much in advance!

  2. This is more or less exactly what I was looking for. Code looks good and not spaghetti. Great write up! Thankyou!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.