Re: How to set the _tracking_consent cookie to a domain that does not propagate to subdomains

How to set the _tracking_consent cookie to a domain that does not propagate to subdomains

alex_pandectes
Shopify Partner
4 0 2

Hi, so I was wondering if there is a way to stop the propagation of the Shopify _tracking_consent cookie to the subdomains of a main domain. For example: 

_tracking_consent should be on the domain.com site
but not on the subdomain.domain.com site.

So my question is: is it possible to change this cookie's domain scope? from .domain.com to domain.com
Thanks! 

Replies 3 (3)

Small_Task_Help
Shopify Partner
830 28 75

Hi,

 

It can be done by correctly setting up domain attribute 

 

Example script that you can add to your theme’s theme.liquid or use through Google Tag Manager:

document.addEventListener('DOMContentLoaded', function() {
  // Check if the existing cookie is set to the current domain
  var trackingConsentCookie = document.cookie.match(/_tracking_consent=([^;]*)/);
  
  if (trackingConsentCookie) {
    // Delete the existing cookie with .domain.com
    document.cookie = "_tracking_consent=; path=/; domain=.domain.com; expires=Thu, 01 Jan 1970 00:00:00 UTC;";

    // Set the cookie specifically to domain.com
    document.cookie = "_tracking_consent=" + trackingConsentCookie[1] + "; path=/; domain=domain.com; SameSite=Lax;";
  }
});
To Get Shopify Experts Help, E-mail - hi@ecommercesmalltask.com
About Us - We are Shopify Expert Agency
At Google My Business - Ecommerce Small Task - Hire Shopify Developers Ahmedabad
alex_pandectes
Shopify Partner
4 0 2

Thanks! I will test and let you know if this works on my store! 

 

alex_pandectes
Shopify Partner
4 0 2

Hi, it's me again here, Tested this approach, but it did not work. Tried many variants of this code, but in general, the following happens: 

The cookie is deleted (this is OK)
when setting the new cookie up only for the main domain, it automatically gets set on the .domain.com ... Not sure if I did something wrong, please let me know 🙂
Is there also any other approach to this? 


Thanks a lot!