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:

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:

Getting the First Visit Timestamp

1

2

3

4

5

6

7

8

9

function(){

vargaCid="{{CJS - Client ID}}";

if(gaCid!="n/a"){

varfirstVisitTimestamp=gaCid.split('.')[1];

returnfirstVisitTimestamp;

}else{

returnnull;

}

}

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:

Getting the First Visit Timestamp

1

2

3

4

5

function(){

vartime=Date.now();

varfinalTimestamp=time.toString().substring(0,10);

returnfinalTimestamp;

}

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:

GTM Variable to check time after the first visit

JavaScript

1

2

3

4

5

6

7

8

9

10

11

12

function(){

varfirstTime="{{CJS - firstVisitTimestamp}}";

varcurrentTime="{{CJS - currentTimestamp}}";

vartimestampDiff=currentTime-firstTime;

varhoursPassed=48;

vartimePassed=hoursPassed*60*60;

if(timestampDiff>=timePassed){

returntrue;

}else{

returnfalse;

}

}

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:

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!