2022-09-19 09:45:52 +03:00
|
|
|
/**
|
|
|
|
* @typedef {Object} ReferrerData
|
2022-09-27 22:28:06 +03:00
|
|
|
* @prop {string|null} [referrerSource]
|
|
|
|
* @prop {string|null} [referrerMedium]
|
|
|
|
* @prop {string|null} [referrerUrl]
|
2022-09-19 09:45:52 +03:00
|
|
|
*/
|
|
|
|
|
2022-09-19 17:52:19 +03:00
|
|
|
const knownReferrers = require('@tryghost/referrers');
|
|
|
|
|
2022-09-19 09:45:52 +03:00
|
|
|
/**
|
|
|
|
* Translates referrer info into Source and Medium
|
|
|
|
*/
|
|
|
|
class ReferrerTranslator {
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {Object} deps
|
|
|
|
* @param {string} deps.siteUrl
|
|
|
|
* @param {string} deps.adminUrl
|
|
|
|
*/
|
|
|
|
constructor({adminUrl, siteUrl}) {
|
|
|
|
this.adminUrl = this.getUrlFromStr(adminUrl);
|
|
|
|
this.siteUrl = this.getUrlFromStr(siteUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate referrer details from history
|
|
|
|
* @param {import('./history').UrlHistoryArray} history
|
|
|
|
* @returns {ReferrerData|null}
|
|
|
|
*/
|
|
|
|
getReferrerDetails(history) {
|
2022-09-22 20:43:36 +03:00
|
|
|
// Empty history will return null as it means script is not loaded
|
2022-09-19 09:45:52 +03:00
|
|
|
if (history.length === 0) {
|
2022-09-21 12:29:55 +03:00
|
|
|
return {
|
2022-09-27 22:28:06 +03:00
|
|
|
referrerSource: null,
|
|
|
|
referrerMedium: null,
|
|
|
|
referrerUrl: null
|
2022-09-21 12:29:55 +03:00
|
|
|
};
|
2022-09-19 09:45:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const item of history) {
|
2022-09-27 22:28:06 +03:00
|
|
|
const referrerUrl = this.getUrlFromStr(item.referrerUrl);
|
|
|
|
const referrerSource = item.referrerSource;
|
|
|
|
const referrerMedium = item.referrerMedium;
|
2022-09-19 09:45:52 +03:00
|
|
|
|
|
|
|
// If referrer is Ghost Explore
|
2022-09-27 22:28:06 +03:00
|
|
|
if (this.isGhostExploreRef({referrerUrl, referrerSource})) {
|
2022-09-19 09:45:52 +03:00
|
|
|
return {
|
2022-09-27 22:28:06 +03:00
|
|
|
referrerSource: 'Ghost Explore',
|
|
|
|
referrerMedium: 'Ghost Network',
|
|
|
|
referrerUrl: referrerUrl?.hostname ?? null
|
2022-09-19 09:45:52 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// If referrer is Ghost.org
|
2022-09-27 22:28:06 +03:00
|
|
|
if (this.isGhostOrgUrl(referrerUrl)) {
|
2022-09-19 09:45:52 +03:00
|
|
|
return {
|
2022-09-27 22:28:06 +03:00
|
|
|
referrerSource: 'Ghost.org',
|
|
|
|
referrerMedium: 'Ghost Network',
|
|
|
|
referrerUrl: referrerUrl?.hostname
|
2022-09-19 09:45:52 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// If referrer is Ghost Newsletter
|
2022-09-27 22:28:06 +03:00
|
|
|
if (this.isGhostNewsletter({referrerSource})) {
|
2022-09-19 09:45:52 +03:00
|
|
|
return {
|
2022-09-27 22:28:06 +03:00
|
|
|
referrerSource: referrerSource.replace(/-/g, ' '),
|
|
|
|
referrerMedium: 'Email',
|
|
|
|
referrerUrl: referrerUrl?.hostname ?? null
|
2022-09-19 09:45:52 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// If referrer is from query params
|
2022-09-27 22:28:06 +03:00
|
|
|
if (referrerSource) {
|
|
|
|
const urlData = referrerUrl ? this.getDataFromUrl(referrerUrl) : null;
|
2022-09-19 09:45:52 +03:00
|
|
|
return {
|
2022-09-27 22:28:06 +03:00
|
|
|
referrerSource: referrerSource,
|
|
|
|
referrerMedium: referrerMedium || urlData?.medium || null,
|
|
|
|
referrerUrl: referrerUrl?.hostname ?? null
|
2022-09-19 09:45:52 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// If referrer is known external URL
|
2022-09-27 22:28:06 +03:00
|
|
|
if (referrerUrl && !this.isSiteDomain(referrerUrl)) {
|
|
|
|
const urlData = this.getDataFromUrl(referrerUrl);
|
2022-09-19 09:45:52 +03:00
|
|
|
|
2022-09-21 12:29:55 +03:00
|
|
|
// Use known source/medium if available
|
2022-09-19 09:45:52 +03:00
|
|
|
if (urlData) {
|
|
|
|
return {
|
2022-09-27 22:28:06 +03:00
|
|
|
referrerSource: urlData?.source ?? null,
|
|
|
|
referrerMedium: urlData?.medium ?? null,
|
|
|
|
referrerUrl: referrerUrl?.hostname ?? null
|
2022-09-19 09:45:52 +03:00
|
|
|
};
|
|
|
|
}
|
2022-09-21 12:29:55 +03:00
|
|
|
// Use the hostname as a source
|
2022-09-19 17:52:19 +03:00
|
|
|
return {
|
2022-09-27 22:28:06 +03:00
|
|
|
referrerSource: referrerUrl?.hostname ?? null,
|
|
|
|
referrerMedium: null,
|
|
|
|
referrerUrl: referrerUrl?.hostname ?? null
|
2022-09-19 17:52:19 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-21 12:29:55 +03:00
|
|
|
return {
|
2022-09-27 22:28:06 +03:00
|
|
|
referrerSource: 'Direct',
|
|
|
|
referrerMedium: null,
|
|
|
|
referrerUrl: null
|
2022-09-21 12:29:55 +03:00
|
|
|
};
|
2022-09-19 09:45:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetches referrer data from known external URLs
|
2022-09-19 17:52:19 +03:00
|
|
|
getDataFromUrl(url) {
|
|
|
|
// Allow matching both "google.ac/products" and "google.ac" as a source
|
|
|
|
const urlHostPath = url?.host + url?.pathname;
|
|
|
|
const urlDataKey = Object.keys(knownReferrers).sort((a, b) => {
|
|
|
|
// The longer key has higher the priority so google.ac/products is selected before google.ac
|
|
|
|
return b.length - a.length;
|
|
|
|
}).find((source) => {
|
|
|
|
return urlHostPath?.startsWith(source);
|
|
|
|
});
|
|
|
|
|
|
|
|
return urlDataKey ? knownReferrers[urlDataKey] : null;
|
2022-09-19 09:45:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* Return URL object for provided URL string
|
|
|
|
* @param {string} url
|
|
|
|
* @returns {URL|null}
|
|
|
|
*/
|
|
|
|
getUrlFromStr(url) {
|
|
|
|
try {
|
|
|
|
return new URL(url);
|
|
|
|
} catch (e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* Return whether the provided URL is a link to the site
|
|
|
|
* @param {URL} url
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
isSiteDomain(url) {
|
|
|
|
try {
|
|
|
|
if (this.siteUrl && this.siteUrl?.hostname === url?.hostname) {
|
|
|
|
if (url?.pathname?.startsWith(this.siteUrl?.pathname)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* Return whether provided ref is a Ghost newsletter
|
|
|
|
* @param {Object} deps
|
2022-09-27 22:28:06 +03:00
|
|
|
* @param {string|null} deps.referrerSource
|
2022-09-19 09:45:52 +03:00
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2022-09-27 22:28:06 +03:00
|
|
|
isGhostNewsletter({referrerSource}) {
|
2022-09-19 09:45:52 +03:00
|
|
|
// if refferer source ends with -newsletter
|
2022-09-27 22:28:06 +03:00
|
|
|
return referrerSource?.endsWith('-newsletter');
|
2022-09-19 09:45:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* Return whether provided ref is a Ghost.org URL
|
2022-09-27 22:28:06 +03:00
|
|
|
* @param {URL|null} referrerUrl
|
2022-09-19 09:45:52 +03:00
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2022-09-27 22:28:06 +03:00
|
|
|
isGhostOrgUrl(referrerUrl) {
|
|
|
|
return referrerUrl?.hostname === 'ghost.org';
|
2022-09-19 09:45:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* Return whether provided ref is Ghost Explore
|
|
|
|
* @param {Object} deps
|
2022-09-27 22:28:06 +03:00
|
|
|
* @param {URL|null} deps.referrerUrl
|
|
|
|
* @param {string|null} deps.referrerSource
|
2022-09-19 09:45:52 +03:00
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2022-09-27 22:28:06 +03:00
|
|
|
isGhostExploreRef({referrerUrl, referrerSource}) {
|
|
|
|
if (referrerSource === 'ghost-explore') {
|
2022-09-19 09:45:52 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-09-27 22:28:06 +03:00
|
|
|
if (referrerUrl?.hostname
|
|
|
|
&& this.adminUrl?.hostname === referrerUrl?.hostname
|
|
|
|
&& referrerUrl?.pathname?.startsWith(this.adminUrl?.pathname)
|
2022-09-19 09:45:52 +03:00
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-09-27 22:28:06 +03:00
|
|
|
if (referrerUrl?.hostname === 'ghost.org' && referrerUrl?.pathname?.startsWith('/explore')) {
|
2022-09-19 09:45:52 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ReferrerTranslator;
|