Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"atcs",
"testdata",
"Bytespider",
"Timespans"
"Timespans",
"googlequicksearchbox"
]
}
74 changes: 74 additions & 0 deletions spec/src/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const {
isNil,
getWindowLocation,
getCanonicalUrl,
getDocumentReferrer,
dispatchEvent,
createCustomEvent,
hasOrderIdRecord,
Expand Down Expand Up @@ -215,6 +216,18 @@ describe('ConstructorIO - Utils - Helpers', () => {
});

describe('getCanonicalUrl', () => {
it('Should return android app referrers in a valid url structure', () => {
const cleanup = jsdom();

const canonicalUrl = 'android-app://com.google.android.googlequicksearchbox/';
const canonicalEle = document.querySelector('[rel=canonical]');
canonicalEle.setAttribute('href', canonicalUrl);

expect(getCanonicalUrl()).to.equal('https://com.google.android.googlequicksearchbox/');

cleanup();
});

it('Should return the canonical URL from the DOM link element', () => {
const cleanup = jsdom();

Expand Down Expand Up @@ -264,6 +277,67 @@ describe('ConstructorIO - Utils - Helpers', () => {
});
});

describe('getDocumentReferrer', () => {
it('Should return android app referrers in a valid url structure', () => {
const cleanup = jsdom();

const referrerUrl = 'android-app://com.google.android.googlequicksearchbox/';
Object.defineProperty(document, 'referrer', {
value: referrerUrl,
configurable: true,
});

expect(getDocumentReferrer()).to.equal('https://com.google.android.googlequicksearchbox/');

cleanup();
});

it('Should return the referrer URL from the document', () => {
const cleanup = jsdom();

const referrerUrl = 'https://constructor.io/products/item';
Object.defineProperty(document, 'referrer', {
value: referrerUrl,
configurable: true,
});

expect(getDocumentReferrer()).to.equal(referrerUrl);

cleanup();
});

it('Should return null for a relative url', () => {
const cleanup = jsdom();

const relativeUrl = '/products/item';
Object.defineProperty(document, 'referrer', {
value: relativeUrl,
configurable: true,
});

const result = getDocumentReferrer();
expect(result).to.be.null;

cleanup();
});

it('Should return null when referrer is empty', () => {
const cleanup = jsdom();

Object.defineProperty(document, 'referrer', {
value: '',
configurable: true,
});

expect(getDocumentReferrer()).to.be.null;
cleanup();
});

it('Should return null when not in a DOM context', () => {
expect(getDocumentReferrer()).to.be.null;
});
});

describe('dispatchEvent', () => {
it('Should dispatch an event if in a DOM context', () => {
const cleanup = jsdom();
Expand Down
32 changes: 27 additions & 5 deletions src/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@ const utils = {
return cleanedParams;
},

cleanAndValidateUrl: (url, baseUrl = undefined) => {
let validatedUrl = null;

try {
// Handle android app referrers
if (url?.startsWith('android-app')) {
url = url?.replace('android-app', 'https');
}

validatedUrl = (new URL(url, baseUrl)).toString();
} catch (e) {
// do nothing
}

return validatedUrl;
},

throwHttpErrorFromResponse: (error, response) => response.json().then((json) => {
error.message = json.message;
error.status = response.status;
Expand Down Expand Up @@ -92,11 +109,17 @@ const utils = {
},

getDocumentReferrer: () => {
if (utils.canUseDOM()) {
return document?.referrer;
let documentReferrer = null;

try {
if (utils.canUseDOM()) {
documentReferrer = utils.cleanAndValidateUrl(document.referrer);
}
} catch (e) {
// do nothing
}

return null;
return documentReferrer;
},

getCanonicalUrl: () => {
Expand All @@ -108,8 +131,7 @@ const utils = {
const href = linkEle?.getAttribute('href');

if (href) {
const url = new URL(href, document.location.href);
canonicalURL = url.toString();
canonicalURL = utils.cleanAndValidateUrl(href, document.location.href);
}
}
} catch (e) {
Expand Down
Loading