Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Third party script setting first party cookies

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.

like image 545
dendog Avatar asked Oct 16 '25 04:10

dendog


1 Answers

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).

like image 111
Quentin Avatar answered Oct 17 '25 19:10

Quentin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!