Set data value to function

Silver Contributor
Silver Contributor


I need to send a timezone-specific hit with every event that I am passing to Google Analytics. Specifically, the function is:

// 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;
};['ga_hit_timestamp'] = 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);

This is too complicated to set with JS code within the 'Set data values' extension, and since this needs to trigger on e.g. page loads, clicks, and potentially others, using a single jQuery onHandler extension will not cover all cases.

I though about using a JS code extension to set the above to a globally scoped function, and pass that function within 'Set data values' extension. Would this be possible? Something like the image below?


Otherwise, what would be the best practice way of achieving this?

Within Google Tag Manager (we are migrating to Tealium), this function can be used anywhere the token {{timestamp}} is declared, since the variable is set with custom HTML (the function) as soon as it is declared. I am trying to replicate this functionality.


Set data value to function

Tealium Employee

Yes, that approach would definitely work.  Just make sure to set an appropriate scope to your JS extension (e.g. pre-loader), so that the function is available before you call it in the Set Data Value extension.

Another option would be to set the variable directly in your JS exention.  (e.g. instead of returning the value from a function, you would just set b['ga_hit_timestamp'] or['ga_hit_timestamp'] at the end of your JS code block).  There are pros and cons to either approach, though frankly, I prefer your approach overall (using the JS code combined with the Set Data Value extension).

And since you are writing JS extensions, this article may be a useful knowing how to reference the data object (, b, etc.) at various points:

And lastly, remember that whenver you make changes in your JS extension, you must hit the "approved for publish" button in the extension, along with publishing the profile changes in TiQ in order for the JS extension code to begin running on your site.  This little step trips me up more often that I should admit.


Set data value to function

Silver Contributor
Silver Contributor
Thanks very much. I did indeed create a globally scoped function for the timestamp, and have successfully passed it as JS code in all jQuery handler events.