How to Set Up Cookies in Google Tag Manager

Today I want to talk about cookies. Not those cookies, but they’re still good.

What are they? A cookie is basically a string of text that gets stored in your browser. Website owners use them to keep track of useful information about your online behavior because they want to understand your reasons for being on their website. That will tell them whether you’d be a good customer or not.

One important reason why you might need cookies is due to the fact that the dataLayers you set up for GTM don’t “persist.” In other words, the information inside dataLayers doesn’t hold from page to page, so you have to collect every detail all over again for each page if you want to capture that information for every stage of your reports.

Cookies help you accomplish this because the browser stores the info inside of them for you, which allows you to capture details and then reuse them again on any page you choose to.

The reason why I wrote this blog is because I haven’t seen the setup for cookies explained well (at least not for my purposes). So here’s my story:

I needed to use cookies to carry GTM product category data through my funnel from the product page all the way down to the Receipt page (aka Thank You page). Unfortunately, my category values were only available on the product page. Here’s what you need to know in order to handle problems like this:

Step 1) First, you need access to your category value. If you’re using server side variables, it might look like this: {{Category}}

If you’re getting your category values from one of your web pages by scraping the DOM, then you’ll need to use JavaScript code to get it from your template files and save it as a variable. In that case it’ll just be whatever you chose to name your variable: Category

Step 2) Next, you need to create a 1st-party cookie variable in GTM. This allows you to set the name for your cookie, which will be important when you use the special cookie format for your cookie’s code later on.

Once that’s done, you’ll need to create a custom HTML tag. It will use the name you set in Step 2 and the value you accessed in Step 1 along with some other settings such as the expiration date of your cookie. You’ll do all this by creating a JavaScript function inside of the custom HTML tag.

Step 3) Depending on your situation, you’ll want to add values to your cookie as your visitors take certain actions. I recommend defining your cookie value as an array inside of the HTML tag from Step 2. From there, you can push new values into the array using the .push() method from JavaScript.

Step 4) If your code is like mine, you may get some “blank” or “undefined” fields in your cookie, so I’m just going to save you lots of time and frustration by recommending you filter them out with the .filter(Boolean) function. Now you have a clean cookie. You’re welcome.

Step 5) Now that you’ve got a clean cookie, you may want to inject those values back into your dataLayer. In my case, I needed to insert category values into the pages of my shopping cart funnel.

To do this, you’ll need to make a custom JavaScript variable. This will insert the cookie values back into your dataLayer for you. Make a new Universal Google Analytics tag in GTM, enable Enhanced E-Commerce, and select “read from a variable” instead of “use DataLayer.”

Every time your trigger fires, GTM will send the last value in your Custom JavaScript Variable into the dataLayer (if you coded it to do that, like I did). For example, every time a page in my funnel loads, the last cookie value (product category) is injected into my dataLayer and sent by GTM to Google Analytics.

This works for me because I set up the category variables to go to my cookies whenever someone clicks the Add to Cart button. If you put a product in the cart, a category value goes into the cookie. When the next page loads, the same category value goes into the dataLayer object for that page hit. And so on and so on. By the way, you want to code things in such a way that the right category gets assigned to the right product. This could be a little tricky.


Why go through all the trouble? Because Google Analytics gives you the option to see the product categories for your sales transactions. That’s potentially very valuable info if you want to know which categories are performing well, as opposed to simply viewing one product at a time.

Additionally, you’re able to view your categories by hierarchy. For example, if your category value was “Men’s/Shoes/Nike/Jordan” then Google Analytics would allow you to zoom in on each of these levels separately. Pretty cool right?

Anyway, I hope this has been helpful. If you’re using cookies for the first time, it should definitely save you a few days if not weeks. Good luck out there!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s