0

Currently, in the User Report view of Google Analytics, I get timestamps on each event, but it is only down to the minute, not the second. I can't find a setting in GA that changes that column.

My goal is to pass this timestamp through GTM, perhaps as "tag label", so that I can see it in GA.

How do I create a timestamp variable in GTM?

2 Answers2

3

Create a custom javascript variable (i.e. a variable that contains a function, not a "javascript" variable that just reads a global variable), and give it a name, e.g. "timestamp".

Custom javascript variables are anonymous functions with a return value.

The current way to get a timestamp is Date.now(). This might not be supported by older browser (especially IE 8 and lower), so you might use new Date().getTime(); as an alternative.

The variable body would be as simple as:

function() {
  return Date.now();
}

and you would use that in a tag by surrounding the variable name with double curly parenthesis, e.g. {{timestamp}}. Date.now() returns milliseconds ( elapsed since 1 January 1970 00:00:00 UTC), so you might want to divide by thousand.

Alternatively you could create a datetime variable that includes seconds and even milliseconds. I think this was originally by Simo Ahava:

function() {
    // 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);
}

which returns a formatted string like 2016-08-02T09:22:44.496+02:00.

Eike Pierstorff
  • 29,331
  • 4
  • 35
  • 52
  • Nice Answer, but you need it's not clear if youre using a hardcode version of the Javascript o you're doing this via GTM, i think is the second one – Kemen Paulos Plaza Aug 11 '16 at 07:38
  • @KemenPaulosPlaza it is indeed the second one - by "variable" I mean what GTM used to call "macros" (which was less confusing). There is a GTM variable type "Custom Javascript" which is always an anonymous function that returns a value. – Eike Pierstorff Aug 11 '16 at 07:56
1

The second it's not accesible via Google Analytics. The closest way to do this is via Google Big Query,but this last is only available for premium members.

Maybe you can add the timeStamp as CustomDimentions

function getdateGA(){
  return Date();
}

ga('send', 'event', 'category', 'action', {
  'dimention1': getdateGA()
});

The date format is not the best one, try to find the best for you modifing the getdateGA function

More resources about the date in

How to format a JavaScript date

Community
  • 1
  • 1
Kemen Paulos Plaza
  • 1,462
  • 8
  • 18