Reputation: 3338
I am loading a 3rd party js file/script via a Tag in Google Tag Manager.
This script needs to set cookies from its own domain, however all cookies are being set as 1st party from the host domain, which means they are not being transferred to the server.
I am also developing the third party script, so have direct access to this, currently the code in that script for writing cookies, is based on:
const host = (window && window.location && window.location.hostname) || '';
const parts = host.split('.');
const COOKIE_DOMAIN = parts.length > 1 ? `;domain=.${parts.slice(-2).join('.')}` : '';
I thought this may grab the cookies from the origin, but sets them from host.
I have tried to just change and hardcode the COOKIE_DOMAIN to my server address, but this does not seem to work, ie no cookies are set.
EDIT: removing the reference to Tag Manager from the title as it is not overly relevant.
UPDATE: so it seems that when I load a script tag from a 3rd party server, it basically becomes 1st party - which makes it difficult to set cookie from the original domain.
Upvotes: 3
Views: 4068
Reputation: 944320
There is no way for JavaScript, running in the page, to set cookies for a different domain. document.cookie
refers to the current page's cookies.
If you want to set cookies for a different domain, then you have to do it using HTTP headers (e.g. when the .js
file is served by the remote domain).
Upvotes: 5