The timestamp is something you can send with every single hit you send to Google Analytics (GA). You can use a timestamp to define at what time your users trigger certain hits/events on your website. For example, if you would like to know (in detail) when your visitors have bought something you can use the timestamp.

You can decide yourself when you would like to send the timestamp hit. This can be on every pageview or just on transactions. That’s completely up to you. In the article, I will use Google Tag Manager (GTM) to implement the timestamp. We will of course combine GTM with GA since we need GA to read all the data, but also to create a custom dimension so we can filter on Timestamp.

I assume you have already created a GTM account, implemented it, and have a little knowledge about this tool.

Step 1: Create custom dimension in GA

The first step you’ve got to take is to create a custom dimension in Google Analytics.

  1. Log in to Google Analytics
  2. Admin > Select account > Select property
  3. Click on Custom Definitions > Custom Dimensions
  4. Click on the button +NEW CUSTOM DIMENSION
  5. Name it: Timestamp
    Scope: Hit
    Active: Check
  6. Create
  7. Click Done when you see the code
  8. Remember the Custom Dimension Index (we’ll need it later)

You’ll now get some code back after the 6th step, but that’s only if you add the functionality hardcoded within the tracking code. We’re not going to do that, we will use Google Tag Manager for that.

This is what you’ll have when you’re finished with this step:

Adding Timestamp - Screenshot 1

Let’s dive into Google Tag Manager (GTM) now.

Step 2: Create a custom JavaScript variable

We will need to add some JavaScript (JS) to Google Tag Manager in order to find out the time of our users.

  1. Log in to Google Tag Manager
  2. Go to the Accounts > Container where you would like to add the Timestamp (should be the same as the Google Analytics account of course)
  3. Left side menu go to Variables > NEW
  4. Name the variable something like cJS – Timestamp (cJS = custom JavaScript)
  5. For Variable Configuration select Custom JavaScript (can be found under Page Variables)
  6. Copy and paste the code down belowfunction() {
    // Get local time as ISO string with offset at the end
    var now = new Date();
    var tzo = -now.getTimezoneOffset();
    var dif = tzo >= 0 ? ‘+’ : ‘-‘;
    var pad = function(num) {
    var norm = Math.abs(Math.floor(num));
    return (norm < 10 ? ‘0’ : ”) + norm;
    };
    return now.getFullYear()
    + ‘-‘ + pad(now.getMonth()+1)
    + ‘-‘ + pad(now.getDate())
    + ‘T’ + pad(now.getHours())
    + ‘:’ + pad(now.getMinutes())
    + ‘:’ + pad(now.getSeconds())
    + ‘.’ + pad(now.getMilliseconds())
    + dif + pad(tzo / 60)
    + ‘:’ + pad(tzo % 60);
    }
  7. Save it!

After finishing these steps you’ll have this:

 

Adding Timestamp - Screenshot 3

The Timestamp you’ll get with this code above here is something like 2021-06-23T15:14:46.528+02:00

  • The first part is the date [2021-06-23]
  • The second part is the time [T15:14:46.528]
  • The third part is the time zone [+02:00]

The timezone can be used to define at what time in the certain timezone somebody did something. This makes it easier to understand when visitors are for example buying something. Is this in the evenings for example? Without the timezone added you wouldn’t know this for sure (if your website is international).

Step 3: Connect the variable to the tag

If you’re like me, you’ll like to have everything structured within Google Tag Manager. There’s a good chance you have all the Google Analytics settings within a variable. Normally you would add the custom dimension to the GA variable but you can’t do that with Custom Dimension, it doesn’t work.

You’ll have to manually overwrite your Google Analytics tag and I will show you how.

  1. Go to Tags in Google Tag Manager
  2. Go to your Google Analytics tracking code (make sure you have the Google Analytics Universal)
  3. Check: Enable overriding settings in this tag
  4. Add the Google Analytics tracking ID manually
  5. Open More Settings
  6. Open Custom Dimensions
  7. Remember when I told you that you needed to keep the Index number of the custom dimension in your mind? This is where you’ll need it (check the last part of Step 1 if you can’t remember it)
  8. Index: [number of the custom dimension in Google Analytics]
  9. Dimension Value: click the icon and select the variable we made during step 2
  10. Save!

You’ll have something like this:

Adding Timestamp - Screenshot 4

Don’t publish yet! We need to test if it’s working.

Step 4: Test if the JavaScript Timestamp is working

You should test before you put anything live! Just to be sure.

  1. Hit the Preview button
  2. Add the website URL from the website where the GTM container is installed
  3. ConnectIf connecting isn’t working, make sure you disable any kind of adblockers like AdBlock or Ghostery
  4. New Tab opens
  5. Go to the Tag Assistant tab in your browser
  6. Select Message (step 1)
  7. Go to the Variables tab
  8. Check if the variable is there
  9. Is this the case? Then you’re ready to submit the changes! If not, retrace your steps and follow the instructions again.
  10. Go back to the GTM container
  11. Submit changes!

It’s live! Make good use of it.

This is how it looks like when checking the variable (I named my Timestamp cJS – Timestamp):

Adding Timestamp - Screenshot 5

Everything is timestamped now. You can now use it as a custom dimension in Google Analytics. Like everything in Google Analytics, it doesn’t work backward, so only things that happen from now on will get timestamped.

If this text version didn’t work for you and you need more details. Check this video on YouTube (it might be outdated, but the idea/steps are mostly the same).

Feel free to reach out to me if you can’t get it to work. Since you’re here, you might also be interested in Outbound link tracking and Scroll Depth tracking using Google Tag Manager. More data to collect!

Other interesting sources:

simoahava.com/analytics/13-useful-custom-dimensions-for-google-analytics/

simoahava.com/analytics/improve-data-collection-with-four-custom-dimensions/#4-hit-timestamp