Extend cookie lifetime using server-side Google Tag Manager

Jul 4, 2024
Mar 8, 2022
Also available in

Cookies are a cornerstone of marketing, and they cause many disputes and unrest in the industry. Growing user privacy concerns, data regulation policies like CCPA, GDPR, and ePrivacy Directive, as well as cookie deprecation timeline by Google Chrome make getting the most of cookies quite challenging. 

Intelligent Tracking Prevention and other anti-tracking mechanisms also restrict cookies. These factors challenge businesses that rely heavily on online marketing, especially third-party data collection for targeting ads.

It makes advertisers look for ways to target their audience without cookies and get the most out of them they can still use. One way to get the most out of cookies is to extend their lifetime. In this post, we will show you how tracking restrictions affect cookies, how they influence marketing, and how to use Google Tag Manager to extend cookie lifetime.

3rd party cookies and privacy concerns

3rd party cookies are being blocked by many browsers these days. The two most popular browsers that restrict 3rd party cookies are Safari and Firefox. Starting from January 4th, 2024, Chrome has restricted third-party cookies for 1% of its users, and they plan to restrict 100% of third-party cookies for all Chrome clients by the end of 2025.

Let’s discuss the difference between first-party and third-party cookies. To simplify, the main difference is that first-party cookies are set from your site to your domain, while third-party cookies are set from your site to other domains

Third-party cookies gained a bad reputation because of cross-site tracking. Advertisers use this type of cookie to track users across different domains and to profile users. With the help of third-party cookies, big platforms can follow you around the internet and see what sites you are visiting. In the end, they use this information to show you personalized ads.

How cookies affect analytic and marketing campaign

Intelligent Tracking prevention algorithms used in Firefox and Safari limit the cookie lifetime to 7 days (when cookies are set with JavaScript) or 24 hours (when all of the following conditions are met: cookies are set with JavaScript, link decoration is used, and referring to a website is a ´known tracker´).  

Attribution and reporting

All marketing and analytics platforms store in cookies Click ID and other parameters by which ad attribution and reporting work in analytics platforms.

The shortened lifetime of these cookies critically affects the results of attribution, your ad campaigns, and the data in analytics.

Analyzing user journey

Since cookies are deleted in 1 or 7 days, a user who visited your site 7 days ago will be considered a new one. It will have a massive effect on the customer journey. You won’t be able to see the complete picture of what traffic sources affected a customer's purchase decision.


Personalization is frequently used to provide customers with a seamless experience by showing relevant offers, content, products, etc. However, with the decreased cookie lifetime, personalization might have a negative impact. A user will be assigned to a new audience pool every time the cookie expires.

Affiliate marketing

Each affiliate offer has a cookie lifetime. If the user visited your site in Safari via referral link today and covered it in 10 days, this conversion won’t be credited to the affiliate, and the affiliate will not receive the commission.

Remarketing audience

When cookies reset every 7 days or 1 day, it negatively affects the size of the remarketing audience and ad frequency. Platforms will also have less data to create lookalike or similar audiences.

There is a website where you can check how cookies work in each browser. Cookie lifetime has been changed a few times over the past few years, and the restrictions on third-party and event first-party cookies are worsening. 

The recent changes in Safari are now limiting the lifetime of cookies set by third-party IPs. Using a custom domain only for sGTM is no longer enough to prolong the cookie lifetime. The main reason is that the IP of the tagging server URL will be different from the leading website IP. 

With stape you have four options on how to prolong cookie lifetime:

Same originOwn CDNCookie KeeperCookie extender tag
How it worksFirst-party serving.Proxy a server GTM container URL from the same IP as a website domain. Uses master cookie to store and restore cookies. Designed to extend certain cookies using sGTM. 
Technical requirementsConfigure a CDN or load balancer to forward requests. May need to update DNS entries.Configure a page Rule.Use the stape plugin for your CMS or manually configure the master cookie in the stape interface to store and restore cookies for each user. Configure a tag inside the server Google Tag Manager. 
AdvantagesGoogle recommended a way of configuring a custom domain for sGTM. Prolong cookie lifetime in all browsers. Prolongs cookie lifetime in all browsers; no additional costs.Easy to configure for standard CMS like WP, Shopify, Magento, Big commerce, etc. It is helpful in cases when other methods of extending cookie lifetime do not work. For example, the native tag does not extend cookies, needs to extend cookies set by JS, etc. 
DisadvantagesComplex setup may require additional costs for each request to the worker.Some technical knowledge is required to configure a page rule.For custom websites, a user needs to specify the master cookie manually. Works in specific use cases.
How to configureLinkLinkLinkLink

Stape created an sGTM Cookie Extender tag designed explicitly to extend cookies. There might be multiple situations when you need to use this tag to extend cookies; some of the most popular scenarios from our experience are:

  • A native tag does not extend cookies.
  • Extend the validity of a cookie that is set by JS (e.g. by a tag on a web container).
  • Cross-site tracking using web-generated cookies.
  • Necessity to use web-generated cookies for user identification for other platforms.

Multiple times, we needed to extend the cookie for platforms like affiliate networks and email services, which their native tags cannot do. Or maybe you need to extend web cookies since server cookies, the ones with Httponly, can’t be accessed by JavaScript. And you can’t use these cookies in web GTM. In all these situations, a Cookie Extender can help.

So, how can we extend cookies using the Cookie Extender tag in such cases?

Let's look at two popular platforms that do not support server-side side tracking and can only be used on a web container (so their cookies are set via JS): Hotjar and Microsoft (Bing) ADS.

Let’s see how to use the Cookie Extender tag to extend the lifetime of these platforms' cookies.

1. Identify the names of the cookies you want to extend

In our example, we look at Hotjar and Microsoft ADS.

Microsoft sets quite a few cookies, but the most important one is usually the click ID, which is used for attribution. In Microsoft's case, this is the cookie named '_uetmsclkid'. We need to extend this one.

t's also important to know what a cookie's lifetime should be. In the case of click ID, it is usually set to the duration of the maximum available attribution window on the platform. Most often, it is 90 days.

However, the lifetime of cookies like user ID should be 400 days max.

Often, the platform's official complete documentation describes what cookies are set, their lifespan, and their uses, which makes it very easy to set this up.

From this documentation, we can easily understand that we need to extend the '_hjSessionUser_{site_id}' cookie for 365 days, which is actually the user ID.

Now that we know which cookies to extend, all that's left is to configure the Cookie Extender tag.

In your server GTM container, find the Cookie Extender tag in the gallery and add it.

cookie extender tag
  • Create a new tag with the Cookie Extender type you added to your container in the last step.

This setting creates backup cookies and restores them if the main cookies are not found. For example, for _uetmsclkid, it will create _uetmsclkid_backup. This backup cookie will be used to restore the original cookie if it is deleted. You can always leave this option enabled.

  • Add a new row for each cookie you want to extend and specify the desired lifetime for the cookie. Lifetime is set in seconds.

In our example, we extend _uetmsclkid to 7776000 seconds (90 days) and

_hjSessionUser_55576969 to 31536000 (365 days).

  • Add a trigger. Depending on what you use, it is better to use either the GA4 trigger with all hits or the Data Client trigger with all hits.
configure cookie extender tag

Test the setup

Since you can't run GTM container previews in Safari, the changes you make need to be published, and then the cookie extension needs to be checked afterward.

Publish the changes in your containers and make sure that the cookies for which you have configured Cookie Extender in Safari have the lifetime you specify:

test your setup


With ITP's cookies and other data collection restrictions, Safari has led the privacy crusade. However, other browsers, including Chrome, which has more than 50% of the browser market, have also started implementing anti-tracking mechanisms.

Using Cookie Extender will help you enjoy the advantages of server-side tracking even for platforms that do not support server-side tracking or cannot technically be used on the server.

Cookies considerably impact campaign performance, attribution, conversion tracking, etc. That is why you might want to implement server-side tracking to extend the cookie lifetime.

Tagged with:gtm server

Host your GTM server at Staperight now!