Merge pull request #5153 from overleaf/ta-registration-properties

Rework Referrer-From User Property

GitOrigin-RevId: f1b91616e2a9b93f3c3944890d96e735c563c237
This commit is contained in:
Timothée Alby 2021-09-23 11:02:39 +02:00 committed by Copybot
parent 1e9f37652a
commit 9e5dae3443
5 changed files with 62 additions and 14 deletions

View file

@ -5,6 +5,8 @@ const AnalyticsManager = require('./AnalyticsManager')
function clearSource(session) {
if (session) {
delete session.required_login_for
delete session.required_login_from_product_medium
delete session.required_login_from_product_source
}
}
@ -30,7 +32,6 @@ function parseReferrer(referrer, url) {
if (!referrer) {
return {
medium: 'direct',
detail: 'none',
}
}
@ -38,7 +39,7 @@ function parseReferrer(referrer, url) {
const referrerValues = {
medium: parsedReferrer.medium,
detail: parsedReferrer.referer,
source: parsedReferrer.referer || 'other',
}
if (referrerValues.medium === 'unknown') {
@ -46,7 +47,7 @@ function parseReferrer(referrer, url) {
const referrerHostname = new URL(referrer).hostname
if (referrerHostname) {
referrerValues.medium = 'link'
referrerValues.detail = referrerHostname
referrerValues.source = referrerHostname
}
} catch (error) {
// ignore referrer parsing errors
@ -78,12 +79,30 @@ function addUserProperties(userId, session) {
return
}
if (session.referal_id) {
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) {
AnalyticsManager.setUserPropertyForUser(
userId,
`registered-from-bonus-scheme`,
true
)
AnalyticsManager.setUserPropertyForUser(
userId,
`registered-from-product-medium`,
'bonus-scheme'
)
}
if (session.required_login_for) {
@ -95,12 +114,21 @@ function addUserProperties(userId, session) {
}
if (session.inbound) {
if (session.inbound.referrer) {
if (session.inbound.referrer && session.inbound.referrer.medium) {
AnalyticsManager.setUserPropertyForUser(
userId,
`registered-from-referrer-${session.inbound.referrer.medium}`,
session.inbound.referrer.detail || 'other'
`registered-from-referrer-medium`,
`${session.inbound.referrer.medium
.charAt(0)
.toUpperCase()}${session.inbound.referrer.medium.slice(1)}`
)
if (session.inbound.referrer.source) {
AnalyticsManager.setUserPropertyForUser(
userId,
`registered-from-referrer-source`,
session.inbound.referrer.source
)
}
}
if (session.inbound.utm) {

View file

@ -3,10 +3,14 @@ const OError = require('@overleaf/o-error')
const AnalyticsRegistrationSourceHelper = require('./AnalyticsRegistrationSourceHelper')
const SessionManager = require('../../Features/Authentication/SessionManager')
function setSource(source) {
function setSource(medium, source) {
return function (req, res, next) {
if (req.session) {
req.session.required_login_for = source
req.session.required_login_for = medium
req.session.required_login_from_product_medium = medium
if (source) {
req.session.required_login_from_product_source = source
}
}
next()
}

View file

@ -113,7 +113,10 @@ module.exports = {
webRouter.get(
'/project/:Project_id/invite/token/:token',
AnalyticsRegistrationSourceMiddleware.setSource('project-invite'),
AnalyticsRegistrationSourceMiddleware.setSource(
'collaboration',
'project-invite'
),
AuthenticationController.requireLogin(),
CollaboratorsInviteController.viewInvite,
AnalyticsRegistrationSourceMiddleware.clearSource()
@ -121,7 +124,10 @@ module.exports = {
webRouter.post(
'/project/:Project_id/invite/token/:token/accept',
AnalyticsRegistrationSourceMiddleware.setSource('project-invite'),
AnalyticsRegistrationSourceMiddleware.setSource(
'collaboration',
'project-invite'
),
AuthenticationController.requireLogin(),
CollaboratorsInviteController.acceptInvite,
AnalyticsRegistrationSourceMiddleware.clearSource()

View file

@ -8,7 +8,11 @@ module.exports = {
apply(app) {
app.get(
'/project/new/template/:Template_version_id',
AnalyticsRegistrationSourceMiddleware.setSource('template'),
(req, res, next) =>
AnalyticsRegistrationSourceMiddleware.setSource(
'template',
req.params.Template_version_id
)(req, res, next),
TemplatesMiddleware.saveTemplateDataInSession,
AuthenticationController.requireLogin(),
TemplatesController.getV1Template,

View file

@ -1137,7 +1137,10 @@ function initialize(webRouter, privateApiRouter, publicApiRouter) {
maxRequests: 15,
timeInterval: 60,
}),
AnalyticsRegistrationSourceMiddleware.setSource('link-sharing'),
AnalyticsRegistrationSourceMiddleware.setSource(
'collaboration',
'link-sharing'
),
TokenAccessController.tokenAccessPage,
AnalyticsRegistrationSourceMiddleware.clearSource()
)
@ -1149,7 +1152,10 @@ function initialize(webRouter, privateApiRouter, publicApiRouter) {
maxRequests: 15,
timeInterval: 60,
}),
AnalyticsRegistrationSourceMiddleware.setSource('link-sharing'),
AnalyticsRegistrationSourceMiddleware.setSource(
'collaboration',
'link-sharing'
),
TokenAccessController.tokenAccessPage,
AnalyticsRegistrationSourceMiddleware.clearSource()
)