Skip to main content

How to write a value to a cookie using GTM

Victor Sarker10 November 20212 min read
How to write a value to a cookie using GTM

Guide

If there is a value stored in a variable (either as a Global JavaScript variable on a webpage or as a Variable within GTM) this can be written to a browser cookie using the following code using a Custom HTML tag:

<script>
  (function (cookieName, cookieValue, expirationTime, domain) {
    var date = new Date(); 
    date.setTime(date.getTime() + expirationTime); // Sets expiration time (Time now + expiration time)
    var expiry = date.toUTCString(); // Converts cookie expiry to UTC time string

    document.cookie = cookieName+"="+cookieValue+"; expires="+expiry+"; path=/; domain="+domain+"; SameSite=None; Secure"; // Sets cookie in browser
    return;
  })(<MY_COOKIE_NAME>, <MY_COOKIE_VALUE>, <EXPIRY_IN_MILLISECONDS>, <DOMAIN>); // pass cookie arguments here
</script>

Edit the placeholders in Line 9 with the 4 required arguments (as denoted by the placeholders in <ANGLED_BRACKETS>):

  1. cookieName: This is the name of the cookie that will be created. Only use alphanumeric US-ASCII characters.
  2. cookieValue: This will be the value that you want to set. This must be a string type and can take any US-ASCII characters excluding control characters, Whitespace, double quotes, comma, semicolon, and backslash.
  3. expirationTime: This is the cookie expiry time in milliseconds. This must be an integer type.
  4. domain: This is the domain that the cookie will be hosted on. For example, to set a cookie for the Measurelab website and all of it’s subdomains, use: ".measurelab.co.uk"

Attach the appropriate trigger to the tag to signify when the cookie should be set. Ensure that when the trigger is made that the value being set in the cookie is available. For example, if scraping a value off the webpage document, ensure that the tag is fired once the DOM is ready and the value/variable is defined.

Finally, test in Preview Mode and Publish.

Prerequisites

Edit access to a GTM Container and the necessary values in place to set the cookie.

Use cases

Ensuring a User ID that is set when a user logs in on a particular subdomain is available on another subdomain.


Suggested content

Measurelab awarded Google Cloud Marketing Analytics Specialisation

At the start of the year, if you’d asked us whether Measurelab would be standing shoulder to shoulder with Europe’s biggest consultancies by September, we would've been surprised. Not because we don't believe in ourselves, but because these things feel so distant - until suddenly, they’re not. So, here it is: we’ve been awarded the Marketing Analytics Services Partner Specialisation in Google Cloud Partner Advantage. What’s the big deal? In Google’s own words (with the obligatory Zs): “Spec

Will Hayes11 Sept 2025

BigQuery AI.GENERATE tutorial: turn SQL queries into AI-powered insights

BigQuery just got a major upgrade, you can now plug directly into Vertex AI using the new AI.GENERATE function. Translation: your analytics data and generative AI are now best friends, and they’re hanging out right inside SQL. That opens up a whole world of new analysis options for GA4 data, but it also raises some questions: * How do you actually set it up? * What’s it good for (and when should you avoid it)? * Why would you batch the query? Let’s walk through it step by step. Step 1: H

Katie Kaczmarek3 Sept 2025

How to start forecasting in BigQuery with zero training

If you’d told me five years ago that I’d be forecasting product demand using a model trained on 100 billion time points… without writing a single line of ML code… I probably would’ve asked how many coffees you’d had that day ☕️ But its a brand new world. And it’s possible. Let me explain What is TimesFM? TimesFM is a new foundation model from Google, built specifically for time-series forecasting. Think of it like GPT for time, instead of predicting the next word in a sentence, it predicts t

Katie Kaczmarek14 Jul 2025