Skip to content

Commit

Permalink
fix: cross domain tracking cases
Browse files Browse the repository at this point in the history
  • Loading branch information
seeratawan01 committed Oct 17, 2024
1 parent 9a568e6 commit cfbac76
Showing 1 changed file with 56 additions and 24 deletions.
80 changes: 56 additions & 24 deletions packages/javascript-sdk/src/utils/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,19 @@ export class CookieManager {
}

get(name: string): string | null {
const cookieName = `${name}=`;
const decodedCookie = decodeURIComponent(document.cookie);
const cookieArray = decodedCookie.split(';');
for (let i = 0; i < cookieArray.length; i++) {
let cookie = cookieArray[i].trim();
if (cookie.indexOf(cookieName) === 0) {
return cookie.substring(cookieName.length);
const nameEQ = name + '=';
const ca = document.cookie.split(';');
for (let i = 0; i < ca.length; i++) {
let c = ca[i].trim();
if (c.indexOf(nameEQ) === 0) {
return decodeURIComponent(c.substring(nameEQ.length));
}
}
return null;
}

delete(name: string): void {
document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;domain=${this.cookieDomain}`;
delete(name: string, path: string = '/'): void {
document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=${path};domain=${this.cookieDomain}`;
}

private getCookieDomain(): string {
Expand All @@ -41,31 +40,64 @@ export class CookieManager {
}

private extractRoot(url: string): string {
const domainParts = url.split(".");
const domainLength = domainParts.length;

// Check if it's an IP address
if (domainLength === 4 && domainParts.every(part => !isNaN(Number(part)))) {
if (this.isIpAddress(url)) {
return url;
}

// Handle localhost
if (url === 'localhost') {
return "." + url;
return url;
}

let rootDomain = this.extractTopLevelDomain(url);
if (!rootDomain) {
rootDomain = this.extractRootDomain(url);
}

// Handle subdomains
if (domainLength > 2) {
// Check for country code top-level domains (ccTLDs)
const knownCcTLDs = ['co.uk', 'com.au', 'co.jp']; // Add more as needed
const lastTwoParts = domainParts.slice(-2).join('.');
if (knownCcTLDs.includes(lastTwoParts)) {
return '.' + domainParts.slice(-3).join('.');
return '.' + rootDomain;
}

private isIpAddress(url: string): boolean {
const parts = url.split('.');
return parts.length === 4 && parts.every(part => !isNaN(Number(part)));
}

private extractHostname(url: string): string {
let hostname: string;
if (url.indexOf("//") > -1) {
hostname = url.split('/')[2];
} else {
hostname = url.split('/')[0];
}
hostname = hostname.split(':')[0];
hostname = hostname.split('?')[0];
return hostname;
}

private extractRootDomain(url: string): string {
let domain = this.extractHostname(url);
const splitArr = domain.split('.');
const arrLen = splitArr.length;

if (arrLen > 2) {
if (splitArr[arrLen - 1].length == 2) {
// likely a ccTLD
domain = splitArr[arrLen - 2] + '.' + splitArr[arrLen - 1];
if (splitArr[arrLen - 2].length == 2) {
domain = splitArr[arrLen - 3] + '.' + domain;
}
} else {
// likely a gTLD
domain = splitArr[arrLen - 2] + '.' + splitArr[arrLen - 1];
}
return '.' + domainParts.slice(-2).join('.');
}
return domain;
}

// Handle simple domains
return '.' + url;
private extractTopLevelDomain(url: string): string {
const DOMAIN_MATCH_REGEX = /[a-z0-9][a-z0-9-]+\.[a-z.]{2,6}$/i;
const matches = url.match(DOMAIN_MATCH_REGEX);
return matches ? matches[0] : '';
}
}

0 comments on commit cfbac76

Please sign in to comment.