MCPcopy Create free account
hub / github.com/DavidWells/analytics / parseReferrer

Function parseReferrer

packages/analytics-utils/src/parseReferrer.js:21–65  ·  view source on GitHub ↗
(referrer, currentUrl)

Source from the content-addressed store, hash-verified

19 * @return {ReferrerObject} [description]
20 */
21export function parseReferrer(referrer, currentUrl) {
22 if (!isBrowser) return false
23 // default referral data
24 let refData = {
25 'source': '(direct)',
26 'medium': '(none)',
27 'campaign': '(not set)'
28 }
29 // Add raw ref url if external
30 if (referrer && isExternalReferrer(referrer)) {
31 refData.referrer = referrer
32 }
33
34 const domainInfo = parseDomain(referrer)
35 // Read referrer URI and infer source
36 if (domainInfo && Object.keys(domainInfo).length) {
37 refData = Object.assign({}, refData, domainInfo)
38 }
39
40 // Read URI params and use set utm params
41 const params = paramsParse(currentUrl)
42 const paramKeys = Object.keys(params)
43 if (!paramKeys.length) {
44 return refData
45 }
46
47 // set campaign params off GA matches
48 const gaParams = paramKeys.reduce((acc, key) => {
49 // match utm params & dclid (display) & gclid (cpc)
50 if (key.match(/^utm_/)) {
51 acc[`${key.replace(/^utm_/, '')}`] = params[key]
52 }
53 // https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters
54 // dclid - cpc Cost-Per-Thousand Impressions
55 // gclid - cpc Cost per Click
56 if (key.match(/^(d|g)clid/)) {
57 acc['source'] = googleKey
58 acc['medium'] = (params.gclid) ? 'cpc' : 'cpm'
59 acc[key] = params[key]
60 }
61 return acc
62 }, {})
63
64 return Object.assign({}, refData, gaParams)
65}
66
67/**
68 * @typedef {{

Callers 1

getOriginalSourceFunction · 0.90

Calls 3

isExternalReferrerFunction · 0.90
paramsParseFunction · 0.90
parseDomainFunction · 0.85

Tested by

no test coverage detected