Migrate Features to ES modules
GitOrigin-RevId: 4e9d3176b4b5a5504afc102e569a27d7788864a3
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
import _ from 'lodash'
|
||||
import RequestHelper from './RequestHelper.js'
|
||||
import AnalyticsManager from './AnalyticsManager.js'
|
||||
import querystring from 'querystring'
|
||||
import { URL } from 'url'
|
||||
import Settings from '@overleaf/settings'
|
||||
import OError from '@overleaf/o-error'
|
||||
import logger from '@overleaf/logger'
|
||||
|
||||
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 = `${utmValues.utm_source || 'N/A'};${
|
||||
utmValues.utm_medium || 'N/A'
|
||||
};${utmValues.utm_campaign || 'N/A'};${
|
||||
utmValues.utm_content || utmValues.utm_term || 'N/A'
|
||||
}`
|
||||
AnalyticsManager.setUserPropertyForSessionInBackground(
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
||||
export default {
|
||||
recordUTMTags,
|
||||
}
|
||||
Reference in New Issue
Block a user