Skip to content

Commit d4ee9f9

Browse files
committed
Add setting to automatically use top-level domain for duid (closes snowplow#409)
1 parent 41ecdb4 commit d4ee9f9

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

src/js/lib/helpers.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
var
3737
lodash = require('../lib_managed/lodash'),
38+
cookie = require('browser-cookie-lite'),
3839

3940
object = typeof exports !== 'undefined' ? exports : this; // For eventual node.js environment support
4041

@@ -290,4 +291,62 @@
290291
}
291292
};
292293

294+
/**
295+
* Finds the root domain
296+
*/
297+
object.findRootDomain = function () {
298+
var cookiePrefix = '_sp_root_domain_test_';
299+
var cookieName = cookiePrefix + new Date().getTime();
300+
var cookieValue = '_test_value_' + new Date().getTime();
301+
302+
var split = window.location.hostname.split('.');
303+
var position = split.length - 1;
304+
while (position >= 0) {
305+
var currentDomain = split.slice(position, split.length).join('.');
306+
cookie.cookie(cookieName, cookieValue, 0, '/', currentDomain);
307+
if (cookie.cookie(cookieName) === cookieValue) {
308+
309+
// Clean up created cookie(s)
310+
object.deleteCookie(cookieName, currentDomain);
311+
var cookieNames = object.getCookiesWithPrefix(cookiePrefix);
312+
for (var i = 0; i < cookieNames.length; i++) {
313+
object.deleteCookie(cookieNames[i], currentDomain);
314+
}
315+
316+
return currentDomain;
317+
}
318+
position -= 1;
319+
}
320+
321+
// Cookies cannot be read
322+
return window.location.hostname;
323+
};
324+
325+
/**
326+
* Deletes an arbitrary cookie by setting the expiration date to the past
327+
*
328+
* @param cookieName The name of the cookie to delete
329+
* @param domainName The domain the cookie is in
330+
*/
331+
object.deleteCookie = function (cookieName, domainName) {
332+
cookie.cookie(cookieName, '', -1, '/', domainName);
333+
};
334+
335+
/**
336+
* Fetches the name of all cookies beginning with a certain prefix
337+
*
338+
* @param cookiePrefix The prefix to check for
339+
* @return array The cookies that begin with the prefix
340+
*/
341+
object.getCookiesWithPrefix = function (cookiePrefix) {
342+
var cookies = document.cookie.split("; ");
343+
var cookieNames = [];
344+
for (var i = 0; i < cookies.length; i++) {
345+
if (cookies[i].startsWith(cookiePrefix)) {
346+
cookieNames.push(cookies[i]);
347+
}
348+
}
349+
return cookieNames;
350+
};
351+
293352
}());

src/js/tracker.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
* 16. bufferSize, 1
8383
* 17. crossDomainLinker, false
8484
* 18. maxPostBytes, 40000
85+
* 19. discoverRootDomain, false
8586
*/
8687
object.Tracker = function Tracker(functionName, namespace, version, pageViewId, mutSnowplowState, argmap) {
8788

@@ -258,6 +259,10 @@
258259
// Context to be added to every event
259260
commonContexts = [];
260261

262+
if (argmap.hasOwnProperty('discoverRootDomain') && argmap.discoverRootDomain) {
263+
configCookieDomain = helpers.findRootDomain();
264+
}
265+
261266
if (autoContexts.webPage) {
262267
commonContexts.push(getWebPageContext());
263268
}

0 commit comments

Comments
 (0)