2022-09-19 09:45:52 +03:00
|
|
|
/**
|
|
|
|
* @typedef {Object} ReferrerData
|
|
|
|
* @prop {string|null} [refSource]
|
|
|
|
* @prop {string|null} [refMedium]
|
|
|
|
* @prop {URL|null} [refUrl]
|
|
|
|
*/
|
|
|
|
|
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) {
|
|
|
|
if (history.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
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',
|
|
|
|
refUrl: refUrl
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// If referrer is Ghost.org
|
|
|
|
if (this.isGhostOrgUrl(refUrl)) {
|
|
|
|
return {
|
|
|
|
refSource: 'Ghost.org',
|
|
|
|
refMedium: 'Ghost Network',
|
|
|
|
refUrl: refUrl
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// If referrer is Ghost Newsletter
|
|
|
|
if (this.isGhostNewsletter({refSource})) {
|
|
|
|
return {
|
|
|
|
refSource: refSource.replace(/-/g, ' '),
|
|
|
|
refMedium: 'Email',
|
|
|
|
refUrl: refUrl
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
refUrl: refUrl
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
if (urlData) {
|
|
|
|
return {
|
|
|
|
refSource: urlData?.source,
|
|
|
|
refMedium: urlData?.medium,
|
|
|
|
refUrl: refUrl
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-19 17:52:19 +03:00
|
|
|
// Return any referrer URL in history that is not site domain
|
|
|
|
for (const item of history) {
|
|
|
|
const refUrl = this.getUrlFromStr(item.refUrl);
|
|
|
|
|
|
|
|
if (refUrl && !this.isSiteDomain(refUrl)) {
|
|
|
|
return {
|
|
|
|
refSource: null,
|
|
|
|
refMedium: null,
|
|
|
|
refUrl: refUrl
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-19 09:45:52 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|