Expand my Community achievements bar.

SOLVED

[SOLVED] Storing data across two different DTM properties?

Avatar

Level 4

Hi there,

I'm currently working on a new implementation for a client, where they have two different DTM properties in place for the same domain. 

The screen shot underneath here explains the logic. 

I'm assuming that DTM can hold data values across http and https. But after doing some thorough testing, I can see that an issue occurs when you are going from http to https during your session and at the same time the DTM property changes from "http - dtm property A" to "https - dtm property B". My main objective is pretty simple. Store a value when the user enters the website and use it on the confirmation page. In this example i'm storing from a url parameter. I'm defining the data elements with the url listening function found inside data elements. (this part works). 

on http:

I'm then creating a page load rule with a custom javascript that is listening to _satellite.getVar("my unique url paramter"); and if it doesn't exist, nothing happens, but if it does exist, I create a cookie named "refID"  and store the data elemtents value inside that cookie. If I call _satellite.readCookie("refID");  I get the value back. Which is good.. means the logic is working. 

If I then click on to the https area, the following happens:

I'm trying to call _satellite.readCookie("refID"); in the secure area and I get a "undefined" value back. The same thing happens if I store it to localStorage. 

My hypothesis: 

I'm assuming that this is due to the security concern, because i'm changing DTM property and at the same time going from http to https. 

My question:

Is there any way I can solve this through DTM? 

p.s setting the same property across the website is NOT an option at the moment.

Any help/advice on this issue would be appreciated. Thanks. :)

 

1 Accepted Solution

Avatar

Correct answer by
Level 4

SOLVED IT.

Could see that the cookie from DTM was setting the window.location.host as the domain for the cookie which was "www.nameofbrand.com". But that meant, when I went to the secure area "secure.nameofbrand.com" it didn't match. I have made a alternative solution that can be used by anyone who want to get parameter from URL and store it in a cookie and retrieve it later. Will work across subdomains. 

function getParameterByName(name) {

    var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}

var cookieName = 'Insert unique name of cookie here';
var cookieValue = getParameterByName("insert parameter from URL here");
var myDate = new Date();
myDate.setMonth(myDate.getMonth() + 3); //this sets the expiry date for the cookie for 3 months. Modify as you want. 
document.cookie = cookieName +"=" + cookieValue + ";expires=" + myDate + ";domain=.INSERTDOMAINHERE.com;path=/";

Works for me across any subdomains. 

View solution in original post

6 Replies

Avatar

Level 10

Hi Waqas,

By default, a cookie can be read by both http and https at the same URL i.e. same domain  However it also  means that a cookie set on one domain can not be read on another domain

However, a server can optionally specify the 'secure' flag while setting a cookie this tells the browser to only send it over a secure channel, such as an SSL connection.

In this case the cookie will only be sent over https. A cookie not marked as secure will be sent over both http and https.

Can you please let us know your Website URL so that we can reproduce the issue at our end.

Thanks & Regards

Parit Mittal

Avatar

Level 4

Hi Parit,

Thanks for coming back. The cookie is set to a test domain, where the host is the same across https and https. As you can see from a security point of view, all the settings are set the way it should be possible for me to read the cookie with a simple _satellite.readCookie(); call. But it's not.

Any chance I can send you the URL to the test domain in a private message? You need credentials and password in order to login. :)

 

Kind regards.
Waqas 

Avatar

Correct answer by
Level 4

SOLVED IT.

Could see that the cookie from DTM was setting the window.location.host as the domain for the cookie which was "www.nameofbrand.com". But that meant, when I went to the secure area "secure.nameofbrand.com" it didn't match. I have made a alternative solution that can be used by anyone who want to get parameter from URL and store it in a cookie and retrieve it later. Will work across subdomains. 

function getParameterByName(name) {

    var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}

var cookieName = 'Insert unique name of cookie here';
var cookieValue = getParameterByName("insert parameter from URL here");
var myDate = new Date();
myDate.setMonth(myDate.getMonth() + 3); //this sets the expiry date for the cookie for 3 months. Modify as you want. 
document.cookie = cookieName +"=" + cookieValue + ";expires=" + myDate + ";domain=.INSERTDOMAINHERE.com;path=/";

Works for me across any subdomains. 

Avatar

Level 10

Great Waqas. Thanks for sharing your solution with the community. It certainly Helps :) :)

Thanks & Regards

Parit Mittal

Avatar

Level 4

@Waqas: Can you please share the steps where we can write this code and how we can access the Parameters across any sub-domains.

Thanks!
Roshan

Avatar

Level 4

Hi roshanc44331495​,
As I remember the code was setup inside a page load rule (but it can be in a DCR or Event rule as well).
The value is stored in a cookie that you name yourself, and to get the actual value of the cookie, you can just write "_satellite.readCookie('Name of your cookie')". So inside DTM, in your "data element" you could retrieve the value of the cookie and then use that value across your different tags. In case you don't want the cookie to be stored for longer than after retrieving the value, I would recommend to use a logic where you remove the cookie once the value is read. example:

You set the cookie on "www.siteA.com" (based on my example above) and then when the user goes to "secure.siteA.com" you read the value with the _satellite.readCookie('Name of your cookie'). In your DTM rule, you create an If statement, that looks for the _satellite.readCookie and if it's set, you read the value and delete the cookie at the same time with this request: _satellite.removeCookie('Name of your Cookie')

Hope the above helps, if not, then send me a PM here.

Kind regards,
Waqas