2022-09-19 09:45:52 +03:00
|
|
|
/**
|
|
|
|
* @typedef {Object} ReferrerData
|
|
|
|
* @prop {string|null} [refSource]
|
|
|
|
* @prop {string|null} [refMedium]
|
2022-09-21 12:29:55 +03:00
|
|
|
* @prop {string|null} [refUrl]
|
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-22 20:43:36 +03:00
|
|
|
refSource: null,
|
2022-09-21 12:29:55 +03:00
|
|
|
refMedium: null,
|
|
|
|
refUrl: null
|
|
|
|
};
|
2022-09-19 09:45:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const item of history) {
|
|
|
|
const refUrl = this.getUrlFromStr(item.refUrl);
|
|
|
|
const refSource = item.refSource;
|
|
|
|
const refMedium = item.refMedium;
|
|
|
|
|
|
|
|
// If referrer is Ghost Explore
|
|
|
|
if (this.isGhostExploreRef({refUrl, refSource})) {
|
|
|
|
return {
|
|
|
|
refSource: 'Ghost Explore',
|
|
|
|
refMedium: 'Ghost Network',
|
2022-09-21 12:29:55 +03:00
|
|
|
refUrl: refUrl?.hostname ?? null
|
2022-09-19 09:45:52 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// If referrer is Ghost.org
|
|
|
|
if (this.isGhostOrgUrl(refUrl)) {
|
|
|
|
return {
|
|
|
|
refSource: 'Ghost.org',
|
|
|
|
refMedium: 'Ghost Network',
|
2022-09-21 12:29:55 +03:00
|
|
|
refUrl: refUrl?.hostname
|
2022-09-19 09:45:52 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// If referrer is Ghost Newsletter
|
|
|
|
if (this.isGhostNewsletter({refSource})) {
|
|
|
|
return {
|
|
|
|
refSource: refSource.replace(/-/g, ' '),
|
|
|
|
refMedium: 'Email',
|
2022-09-21 12:29:55 +03:00
|
|
|
refUrl: refUrl?.hostname ?? null
|
2022-09-19 09:45:52 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// If referrer is from query params
|
|
|
|
if (refSource) {
|
2022-09-19 17:52:19 +03:00
|
|
|
const urlData = refUrl ? this.getDataFromUrl(refUrl) : null;
|
2022-09-19 09:45:52 +03:00
|
|
|
return {
|
|
|
|
refSource: refSource,
|
|
|
|
refMedium: refMedium || urlData?.medium || null,
|
2022-09-21 12:29:55 +03:00
|
|
|
refUrl: refUrl?.hostname ?? null
|
2022-09-19 09:45:52 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// If referrer is known external URL
|
|
|
|
if (refUrl && !this.isSiteDomain(refUrl)) {
|
2022-09-19 17:52:19 +03:00
|
|
|
const urlData = this.getDataFromUrl(refUrl);
|
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-21 12:29:55 +03:00
|
|
|
refSource: urlData?.source ?? null,
|
|
|
|
refMedium: urlData?.medium ?? null,
|
|
|
|
refUrl: refUrl?.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-21 12:29:55 +03:00
|
|
|
refSource: refUrl?.hostname ?? null,
|
2022-09-19 17:52:19 +03:00
|
|
|
refMedium: null,
|
2022-09-21 12:29:55 +03:00
|
|
|
refUrl: refUrl?.hostname ?? null
|
2022-09-19 17:52:19 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-21 12:29:55 +03:00
|
|
|
return {
|
|
|
|
refSource: 'Direct',
|
|
|
|
refMedium: null,
|
|
|
|
refUrl: null
|
|
|
|
};
|
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
|
|
|
|
* @param {string|null} deps.refSource
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
isGhostNewsletter({refSource}) {
|
|
|
|
// if refferer source ends with -newsletter
|
|
|
|
return refSource?.endsWith('-newsletter');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* Return whether provided ref is a Ghost.org URL
|
|
|
|
* @param {URL|null} refUrl
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
isGhostOrgUrl(refUrl) {
|
|
|
|
return refUrl?.hostname === 'ghost.org';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* Return whether provided ref is Ghost Explore
|
|
|
|
* @param {Object} deps
|
|
|
|
* @param {URL|null} deps.refUrl
|
|
|
|
* @param {string|null} deps.refSource
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
isGhostExploreRef({refUrl, refSource}) {
|
|
|
|
if (refSource === 'ghost-explore') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (refUrl?.hostname
|
|
|
|
&& this.adminUrl?.hostname === refUrl?.hostname
|
|
|
|
&& refUrl?.pathname?.startsWith(this.adminUrl?.pathname)
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (refUrl?.hostname === 'ghost.org' && refUrl?.pathname?.startsWith('/explore')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ReferrerTranslator;
|