Skip to content

Commit 44f7a51

Browse files
committed
fix ga integration
1 parent 66ac912 commit 44f7a51

File tree

5 files changed

+32
-25
lines changed

5 files changed

+32
-25
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tutorcruncher-socket",
3-
"version": "0.0.9",
3+
"version": "0.0.10",
44
"description": "TutorCruncher socket",
55
"author": "Samuel Colvin <[email protected]>",
66
"private": false,

src/components/enquiry.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export default {
8585
},
8686
/* istanbul ignore next */
8787
prepare_grecaptcha: function () {
88-
const grecaptcha_callback = (response) => this.$set(this.$root.enquiry_data, 'grecaptcha_response', response)
88+
const grecaptcha_callback = response => this.$set(this.$root.enquiry_data, 'grecaptcha_response', response)
8989
9090
if (this.$root.grecaptcha_key === null) {
9191
grecaptcha_callback('-')
@@ -99,8 +99,8 @@ export default {
9999
})
100100
}
101101
if (window.grecaptcha === undefined) {
102-
window._grecaptcha_loaded = render_grecaptcha
103-
add_script('https://www.google.com/recaptcha/api.js?onload=_grecaptcha_loaded&render=explicit')
102+
window._tcs_grecaptcha_loaded = render_grecaptcha
103+
add_script('https://www.google.com/recaptcha/api.js?onload=_tcs_grecaptcha_loaded&render=explicit')
104104
} else {
105105
render_grecaptcha()
106106
}

src/main.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const raven_config = {
1515
tags: {
1616
host: window.location.host,
1717
},
18-
shouldSendCallback: (data) => {
18+
shouldSendCallback: data => {
1919
// if no culprit this a message and came from socket
2020
const culprit = data.culprit || '/socket.js'
2121
return culprit.indexOf('/socket.js') > 0
@@ -120,7 +120,8 @@ module.exports = function (public_key, config) {
120120
}
121121

122122
if (!config.router_mode) {
123-
config.router_mode = 'hash'
123+
// use history mode with enquiry so it doesn't add the hash
124+
config.router_mode = config.mode === 'enquiry' ? 'history' : 'hash'
124125
} else if (ROUTER_MODES.indexOf(config.router_mode) === -1) {
125126
error = `invalid router mode "${config.router_mode}", options are: ${ROUTER_MODES.join(', ')}`
126127
config.router_mode = 'hash'

src/utils.js

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ marked.setOptions({
66
smartLists: true,
77
})
88

9-
const to_markdown = (t) => {
9+
const to_markdown = t => {
1010
if (t === null || t === undefined) {
1111
return ''
1212
} else {
1313
return marked(t)
1414
}
1515
}
1616

17-
const clean = (obj) => {
17+
const clean = obj => {
1818
let new_obj = {}
1919
for (let [k, v] of Object.entries(obj)) {
2020
if (typeof v === 'object') {
@@ -26,15 +26,15 @@ const clean = (obj) => {
2626
return new_obj
2727
}
2828

29-
const auto_url_root = (path) => {
29+
const auto_url_root = path => {
3030
// remove :
3131
// * contractor slug
3232
// * /enquiry
3333
path = path.replace(/\/\d+-[\w-]+$/, '/').replace(/\/enquiry$/, '/')
3434
return path
3535
}
3636

37-
const add_script = (url) => {
37+
const add_script = url => {
3838
const s = document.createElement('script')
3939
s.async = true
4040
s.src = url
@@ -44,11 +44,11 @@ const add_script = (url) => {
4444
/* istanbul ignore next */
4545
const init_ga = (router, config) => {
4646
const ga_prefixes = []
47-
if (process.env.GA_ID === null || window._tcs_ga !== undefined) {
47+
if (process.env.GA_ID === null) {
4848
return ga_prefixes
4949
}
50-
window._tcs_ga = true
51-
ga_prefixes.push('tcs.')
50+
const tcs_ga_name = `${config.mode}-tcs`
51+
ga_prefixes.push(tcs_ga_name + '.')
5252
if (window.ga === undefined) {
5353
// taken from https://developers.google.com/analytics/devguides/collection/analyticsjs/
5454
add_script('https://www.google-analytics.com/analytics.js')
@@ -59,18 +59,24 @@ const init_ga = (router, config) => {
5959
} else {
6060
ga_prefixes.push('')
6161
}
62-
window.ga('create', 'UA-41117087-3', 'auto', 'tcs')
63-
window.ga('tcs.set', 'dimension1', config.mode)
64-
window.ga('tcs.set', 'dimension2', config.router_mode)
65-
let initial_load = true
62+
window.ga('create', 'UA-41117087-3', 'auto', tcs_ga_name)
63+
window.ga(tcs_ga_name + '.set', 'dimension1', config.mode)
64+
window.ga(tcs_ga_name + '.set', 'dimension2', config.router_mode)
65+
66+
if (!window._tcs_ga) {
67+
// we only submit router changes for one socket instance to avoid duplicate page loads
68+
let initial_load = true
69+
router.afterEach(to => {
70+
const _prefixes = initial_load ? ['tcs.'] : ga_prefixes
71+
initial_load = false
72+
for (let prefix of _prefixes) {
73+
window.ga(prefix + 'set', 'page', to.fullPath)
74+
window.ga(prefix + 'send', 'pageview')
75+
}
76+
})
77+
}
78+
window._tcs_ga = true
6679

67-
router.afterEach(to => {
68-
const _prefixes = initial_load ? ['tcs.'] : ga_prefixes
69-
for (let prefix of _prefixes) {
70-
window.ga(prefix + 'set', 'page', to.fullPath)
71-
window.ga(prefix + 'send', 'pageview')
72-
}
73-
})
7480
return ga_prefixes
7581
}
7682

test/unit/specs/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe('main.js', done => {
4949
let el = document.createElement('div')
5050
el.setAttribute('id', 'socket')
5151
document.body.appendChild(el)
52-
const vm = socket('public_key', {mode: 'enquiry', url_root: '/'})
52+
const vm = socket('public_key', {mode: 'enquiry', url_root: '/', router_mode: 'hash'})
5353
vm.enquiry_form_info = 'foobar'
5454
vm.contractors = [{}]
5555
expect(vm.$el.querySelectorAll('.tcs-grid')).to.have.lengthOf(0)

0 commit comments

Comments
 (0)