Skip to content

Commit 6cc3cf5

Browse files
committed
fix: setCustomDimesions
1 parent 7c26a54 commit 6cc3cf5

1 file changed

Lines changed: 19 additions & 21 deletions

File tree

controllers/track.js

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,19 @@ import ua from 'universal-analytics'
22

33
const visitor = ua(process.env.GA_TRACKING_ID)
44

5-
const setCustomDimensions = () => {
6-
visitor.set('cd1', 'feature')
7-
visitor.set('cd2', 'subfeature')
8-
visitor.set('cd3', 'version')
9-
visitor.set('cd4', 'chapter')
10-
visitor.set('cd5', 'verse')
11-
visitor.set('cd6', 'ip')
12-
visitor.set('cd7', 'isHowToUse')
13-
visitor.set('cd8', 'book')
14-
visitor.set('cd9', 'period')
5+
const setCustomDimensions = (cd) => {
6+
visitor.set('cd1', cd.action)
7+
visitor.set('cd2', cd.subfeature)
8+
visitor.set('cd3', cd.version)
9+
visitor.set('cd4', cd.chapter)
10+
visitor.set('cd5', cd.verse)
11+
visitor.set('cd6', cd.ip)
12+
visitor.set('cd7', cd.isHowToUse)
13+
visitor.set('cd8', cd.book)
14+
visitor.set('cd9', cd.period)
15+
visitor.set('uid', cd.userId)
1516
}
1617

17-
setCustomDimensions()
18-
1918
const generateVersesEventData = (req) => {
2019
const { version, abbrev, chapter, number } = req.params
2120

@@ -61,8 +60,6 @@ const generateRequestsEventData = (req) => {
6160
export const trackEvent = async (req, res, next) => {
6261
try {
6362
const user = req.user
64-
visitor.set('uid', user ? user._id.toString() : '')
65-
6663
const url = req.originalUrl.replace(/\/$/g, '')
6764
const [, , action] = url.split('/')
6865

@@ -74,16 +71,17 @@ export const trackEvent = async (req, res, next) => {
7471
}
7572
const payload = actions[action] || {}
7673

77-
const data = {
78-
ec: action,
79-
ea: 'request',
74+
setCustomDimensions({
8075
...payload,
81-
feature: action,
76+
action,
8277
ip: req.headers['x-forwarded-for'] || req.connection.remoteAddress,
83-
isHowToUse: !!req.query.isHowToUse
84-
}
78+
isHowToUse: !!req.query.isHowToUse,
79+
userId: user ? user._id.toString() : ''
80+
})
8581

86-
return visitor.event(data).send()
82+
return visitor.event({ ec: action, ea: 'request', dp: url }, (err) => {
83+
if (err) throw Error(err)
84+
}).send()
8785
} catch (e) {
8886
console.log('error: trackEvent', e)
8987
} finally {

0 commit comments

Comments
 (0)