Merge pull request #5679 from overleaf/ab-utm-tracking-fix
UTM Tracking GitOrigin-RevId: 8e9f8372affce72b35f47786de40def77c08a514
This commit is contained in:
committed by
Copybot
parent
7836743754
commit
05c1ecdde0
@@ -0,0 +1,61 @@
|
||||
const _ = require('lodash')
|
||||
const RequestHelper = require('./RequestHelper')
|
||||
const AnalyticsManager = require('./AnalyticsManager')
|
||||
const querystring = require('querystring')
|
||||
const { URL } = require('url')
|
||||
const Settings = require('@overleaf/settings')
|
||||
const OError = require('@overleaf/o-error')
|
||||
const logger = require('logger-sharelatex')
|
||||
|
||||
function recordUTMTags() {
|
||||
return function (req, res, next) {
|
||||
const query = req.query
|
||||
|
||||
try {
|
||||
const utmValues = RequestHelper.parseUtm(query)
|
||||
|
||||
if (utmValues) {
|
||||
const path = new URL(req.url, Settings.siteUrl).pathname
|
||||
|
||||
AnalyticsManager.recordEventForSession(req.session, 'page-view', {
|
||||
path,
|
||||
...utmValues,
|
||||
})
|
||||
|
||||
const propertyValue = [
|
||||
'utm_source',
|
||||
'utm_medium',
|
||||
'utm_campaign',
|
||||
'utm_term',
|
||||
]
|
||||
.map(tag => utmValues[tag] || 'N/A')
|
||||
.join(';')
|
||||
AnalyticsManager.setUserPropertyForSession(
|
||||
req.session,
|
||||
'utm-tags',
|
||||
propertyValue
|
||||
)
|
||||
|
||||
// redirect to URL without UTM query params
|
||||
const queryWithoutUtm = _.omit(query, RequestHelper.UTM_KEYS)
|
||||
const queryString =
|
||||
Object.keys(queryWithoutUtm).length > 0
|
||||
? '?' + querystring.stringify(queryWithoutUtm)
|
||||
: ''
|
||||
return res.redirect(path + queryString)
|
||||
}
|
||||
} catch (error) {
|
||||
// log errors and fail silently
|
||||
OError.tag(error, 'failed to track UTM tags', {
|
||||
query,
|
||||
})
|
||||
logger.warn({ error }, error.message)
|
||||
}
|
||||
|
||||
next()
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
recordUTMTags,
|
||||
}
|
||||
Reference in New Issue
Block a user