Deleting Google Analytics cookies on server

We have been trying to remove the Cookies that GTM creates when we specify that the user don't want them enabled.

We load GTM the first thing of the header, then the JS Cookies library and finally our following code.

// Loaded page
window.dataLayer = window.dataLayer || [];
function gtag(){
    dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'XX-XXXXXXXX-X');

if (cookiesEnabled) { // a variable that shows if they are enabled
  // Cookies accepted
  window['ga-disable-XX-XXXXXXXX-X'] = false
} else {
  window['ga-disable-XX-XXXXXXXX-X'] = true
}

// Function to set the cookies
function setCookies (accepted) {
if (!accepted) {
  Cookies.set('_ga', undefined, {
    expires: -1,
    path: '', // Also tested with /
    domain: `.${document.domain}`
  })
  Cookies.set('_gid', undefined, {
    expires: -1,
    path: '', // Also tested with /
    domain: `.${document.domain}`
  })
  Cookies.set('_gat_gtag_XX_XXXXXXXX_X', undefined, {
    expires: -1,
    path: '', // Also tested with /
    domain: `.${document.domain}`
  })
  
  // This was also tested
  Cookies.remove('_ga', {
    path: '', // Also tested with /
    domain: `.${document.domain}`
  })
  Cookies.remove('_gid', {
    path: '', // Also tested with /
    domain: `.${document.domain}`
  })
  Cookies.remove('_gat_gtag_XX_XXXXXXXX_X', {
    path: '', // Also tested with /
    domain: `.${document.domain}`
  })
}
}

We have tried this on both ways, with set and remove. The code running on our local machine works perfectly, the cookies are being removed without any problem, but then, when we pass the exact same code to our test server, those cookies will not be removed.

There is something that maybe we are missing? We were unable to find what.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum