2021-11-02 13:28:18 -04:00
|
|
|
const RefererParser = require('referer-parser')
|
|
|
|
const { URL } = require('url')
|
2021-06-21 10:02:43 -04:00
|
|
|
const AnalyticsManager = require('./AnalyticsManager')
|
|
|
|
|
|
|
|
function clearSource(session) {
|
|
|
|
if (session) {
|
2021-09-23 05:02:39 -04:00
|
|
|
delete session.required_login_from_product_medium
|
|
|
|
delete session.required_login_from_product_source
|
2021-06-21 10:02:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-02 13:28:18 -04:00
|
|
|
const UTM_KEYS = [
|
|
|
|
'utm_campaign',
|
|
|
|
'utm_source',
|
|
|
|
'utm_term',
|
|
|
|
'utm_medium',
|
|
|
|
'utm_count',
|
|
|
|
]
|
|
|
|
|
|
|
|
function parseUtm(query) {
|
|
|
|
const utmValues = {}
|
|
|
|
for (const utmKey of UTM_KEYS) {
|
|
|
|
if (query[utmKey]) {
|
|
|
|
utmValues[utmKey] = query[utmKey]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Object.keys(utmValues).length > 0 ? utmValues : null
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseReferrer(referrer, url) {
|
|
|
|
if (!referrer) {
|
|
|
|
return {
|
|
|
|
medium: 'direct',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const parsedReferrer = new RefererParser(referrer, url)
|
|
|
|
|
|
|
|
const referrerValues = {
|
|
|
|
medium: parsedReferrer.medium,
|
|
|
|
source: parsedReferrer.referer || 'other',
|
|
|
|
}
|
|
|
|
|
|
|
|
if (referrerValues.medium === 'unknown') {
|
|
|
|
try {
|
|
|
|
const referrerHostname = new URL(referrer).hostname
|
|
|
|
if (referrerHostname) {
|
|
|
|
referrerValues.medium = 'link'
|
|
|
|
referrerValues.source = referrerHostname
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
// ignore referrer parsing errors
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return referrerValues
|
|
|
|
}
|
|
|
|
|
2021-06-21 10:02:43 -04:00
|
|
|
function setInbound(session, url, query, referrer) {
|
|
|
|
const inboundSession = {
|
2021-11-02 13:28:18 -04:00
|
|
|
referrer: parseReferrer(referrer, url),
|
|
|
|
utm: parseUtm(query),
|
2021-06-21 10:02:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (inboundSession.referrer || inboundSession.utm) {
|
|
|
|
session.inbound = inboundSession
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function clearInbound(session) {
|
|
|
|
if (session) {
|
|
|
|
delete session.inbound
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function addUserProperties(userId, session) {
|
|
|
|
if (!session) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-23 05:02:39 -04:00
|
|
|
if (session.required_login_from_product_medium) {
|
|
|
|
AnalyticsManager.setUserPropertyForUser(
|
|
|
|
userId,
|
|
|
|
`registered-from-product-medium`,
|
|
|
|
session.required_login_from_product_medium
|
|
|
|
)
|
|
|
|
if (session.required_login_from_product_source) {
|
|
|
|
AnalyticsManager.setUserPropertyForUser(
|
|
|
|
userId,
|
|
|
|
`registered-from-product-source`,
|
|
|
|
session.required_login_from_product_source
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} else if (session.referal_id) {
|
2021-09-10 04:30:01 -04:00
|
|
|
AnalyticsManager.setUserPropertyForUser(
|
2021-06-21 10:02:43 -04:00
|
|
|
userId,
|
|
|
|
`registered-from-bonus-scheme`,
|
|
|
|
true
|
|
|
|
)
|
2021-09-23 05:02:39 -04:00
|
|
|
AnalyticsManager.setUserPropertyForUser(
|
|
|
|
userId,
|
|
|
|
`registered-from-product-medium`,
|
|
|
|
'bonus-scheme'
|
|
|
|
)
|
2021-06-21 10:02:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (session.inbound) {
|
2021-09-23 05:02:39 -04:00
|
|
|
if (session.inbound.referrer && session.inbound.referrer.medium) {
|
2021-09-10 04:30:01 -04:00
|
|
|
AnalyticsManager.setUserPropertyForUser(
|
2021-06-21 10:02:43 -04:00
|
|
|
userId,
|
2021-09-23 05:02:39 -04:00
|
|
|
`registered-from-referrer-medium`,
|
|
|
|
`${session.inbound.referrer.medium
|
|
|
|
.charAt(0)
|
|
|
|
.toUpperCase()}${session.inbound.referrer.medium.slice(1)}`
|
2021-06-21 10:02:43 -04:00
|
|
|
)
|
2021-09-23 05:02:39 -04:00
|
|
|
if (session.inbound.referrer.source) {
|
|
|
|
AnalyticsManager.setUserPropertyForUser(
|
|
|
|
userId,
|
|
|
|
`registered-from-referrer-source`,
|
|
|
|
session.inbound.referrer.source
|
|
|
|
)
|
|
|
|
}
|
2021-06-21 10:02:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (session.inbound.utm) {
|
2021-11-02 13:28:18 -04:00
|
|
|
for (const utmKey of UTM_KEYS) {
|
2021-06-21 10:02:43 -04:00
|
|
|
if (session.inbound.utm[utmKey]) {
|
2021-09-10 04:30:01 -04:00
|
|
|
AnalyticsManager.setUserPropertyForUser(
|
2021-06-21 10:02:43 -04:00
|
|
|
userId,
|
|
|
|
`registered-from-${utmKey.replace('_', '-')}`,
|
|
|
|
session.inbound.utm[utmKey]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
clearSource,
|
|
|
|
setInbound,
|
|
|
|
clearInbound,
|
|
|
|
addUserProperties,
|
|
|
|
}
|