Skip to content

Commit b09fd36

Browse files
committed
google analytics tracking
1 parent 678c50c commit b09fd36

File tree

7 files changed

+85
-31
lines changed

7 files changed

+85
-31
lines changed

config.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ module.exports = {
2525
SOCKET_API_URL: '"https://socket.tutorcruncher.com"',
2626
GRECAPTCHA_KEY: '"6LdyXRgUAAAAADUNhMVKJDXiRr6DUN8TGOgllqbt"',
2727
RAVEN_DSN: '"https://[email protected]/128441"',
28+
GA_ID: '"UA-41117087-3"',
2829
},
2930
build_dir: prod_build_dir,
3031
public_path: prod_public_path,
@@ -48,8 +49,9 @@ module.exports = {
4849
// SOCKET_API_URL: '"api/"',
4950
SOCKET_API_URL: '"https://socket.tutorcruncher.com"',
5051
GRECAPTCHA_KEY: '"6LdyXRgUAAAAADUNhMVKJDXiRr6DUN8TGOgllqbt"',
51-
// RAVEN_DSN: 'false',
52-
RAVEN_DSN: '"https://[email protected]/128441"',
52+
RAVEN_DSN: 'null',
53+
// GA_ID: 'null',
54+
GA_ID: '"UA-41117087-3"',
5355
},
5456
port: 5000,
5557
build_dir: path.resolve(__dirname, 'dev'),
@@ -62,7 +64,8 @@ module.exports = {
6264
RELEASE: '"testing"',
6365
SOCKET_API_URL: '""',
6466
GRECAPTCHA_KEY: 'null',
65-
RAVEN_DSN: 'false',
67+
RAVEN_DSN: 'null',
68+
GA_ID: 'null',
6669
},
6770
build_dir: path.resolve(__dirname, 'dist-test'),
6871
css_source_map: true,

src/components/con-modal.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
<div class="tcs-scroll">
3333
<transition name="tcs-squeeze" mode="out-in">
34-
<enquiry v-if="show_enquiry" :contractor="contractor" :ismodal="true"></enquiry>
34+
<enquiry v-if="show_enquiry" :contractor="contractor" mode="con-modal"></enquiry>
3535
<con-details v-else :contractor="contractor"></con-details>
3636
</transition>
3737
</div>

src/components/enquiry-modal.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<modal title="Enquiry">
3-
<enquiry :ismodal="true"></enquiry>
3+
<enquiry mode="enquiry-modal"></enquiry>
44
</modal>
55
</template>
66

src/components/enquiry.vue

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ export default {
4242
type: Object,
4343
default: null,
4444
},
45-
ismodal: {
46-
type: Boolean,
47-
default: false,
45+
mode: {
46+
type: String,
47+
default: 'vanilla',
4848
},
4949
},
5050
components: {
@@ -56,6 +56,9 @@ export default {
5656
},
5757
attribute_fields: function () {
5858
return this.$root.enquiry_form_info.attributes || []
59+
},
60+
ismodal: function () {
61+
return this.mode.indexOf('modal') > -1
5962
}
6063
},
6164
data: () => ({
@@ -76,6 +79,7 @@ export default {
7679
this.$set(this.$root.enquiry_data, 'contractor', this.contractor.id)
7780
}
7881
this.$root.submit_enquiry(this.submission_complete)
82+
this.$root.ga_event('enquiry-form', 'submitted', this.mode)
7983
},
8084
submission_complete: function () {
8185
this.submitted = true
@@ -97,6 +101,9 @@ export default {
97101
}
98102
}
99103
this.$root.get_enquiry()
104+
if (this.mode !== 'vanilla') {
105+
this.$root.ga_event('enquiry-form', 'loaded', this.mode)
106+
}
100107
},
101108
}
102109
</script>

src/main.js

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ import enquiry_button from './components/enquiry-button'
88
import enquiry_modal from './components/enquiry-modal'
99
import grid from './components/grid'
1010
import con_modal from './components/con-modal'
11-
import {to_markdown, clean, auto_url_root} from './utils'
11+
import {to_markdown, clean, auto_url_root, init_ga} from './utils'
1212

13-
let raven_config = {
13+
const raven_config = {
1414
release: process.env.RELEASE,
1515
tags: {
1616
host: window.location.host,
1717
},
1818
shouldSendCallback: (data) => {
1919
// if no culprit this a message and came from socket
20-
let culprit = data.culprit || '/socket.js'
20+
const culprit = data.culprit || '/socket.js'
2121
return culprit.indexOf('/socket.js') > 0
2222
}
2323
}
@@ -26,9 +26,9 @@ Raven.config(process.env.RAVEN_DSN, raven_config).addPlugin(RavenVue, Vue).insta
2626
Vue.use(VueRouter)
2727

2828
const ConfiguredVueRouter = config => {
29-
let routes = []
29+
const routes = []
3030
if (config.mode === 'grid') {
31-
routes = [
31+
routes.push(
3232
{
3333
path: config.url_root,
3434
name: 'index',
@@ -41,17 +41,17 @@ const ConfiguredVueRouter = config => {
4141
}
4242
]
4343
}
44-
]
44+
)
4545
} else if (config.mode === 'enquiry') {
46-
routes = [
46+
routes.push(
4747
{
4848
path: config.url_root,
4949
name: 'index',
5050
component: enquiry,
5151
},
52-
]
52+
)
5353
} else if (config.mode === 'enquiry-modal') {
54-
routes = [
54+
routes.push(
5555
{
5656
path: config.url_root,
5757
name: 'index',
@@ -64,7 +64,7 @@ const ConfiguredVueRouter = config => {
6464
}
6565
]
6666
}
67-
]
67+
)
6868
}
6969
return new VueRouter({
7070
mode: config.router_mode,
@@ -144,10 +144,13 @@ module.exports = function (public_key, config) {
144144
config[k] = STRINGS[k]
145145
}
146146
}
147+
const router = ConfiguredVueRouter(config)
148+
149+
const ga_prefixes = init_ga(router, config)
147150

148151
return new Vue({
149152
el: config.element,
150-
router: ConfiguredVueRouter(config),
153+
router: router,
151154
render: h => h(app),
152155
data: {
153156
grecaptcha_key: process.env.GRECAPTCHA_KEY,
@@ -182,8 +185,8 @@ module.exports = function (public_key, config) {
182185
if (error !== null) {
183186
return
184187
}
185-
let xhr = new window.XMLHttpRequest()
186-
let url = `${config.api_root}/${public_key}/contractors`
188+
const xhr = new window.XMLHttpRequest()
189+
const url = `${config.api_root}/${public_key}/contractors`
187190
xhr.open('GET', url)
188191
xhr.onload = () => {
189192
let contractors
@@ -217,13 +220,13 @@ response text: "${xhr.responseText}"`)
217220
if (this.contractors_extra[link] !== undefined) {
218221
return false
219222
}
220-
let xhr = new window.XMLHttpRequest()
223+
const xhr = new window.XMLHttpRequest()
221224
xhr.open('GET', url)
222225
xhr.onload = () => {
223226
if (xhr.status !== 200) {
224227
this.handle_error(`bad response ${xhr.status} at "${url}"`)
225228
} else {
226-
let con = JSON.parse(xhr.responseText)
229+
const con = JSON.parse(xhr.responseText)
227230
Vue.set(this.contractors_extra, link, con)
228231
}
229232
}
@@ -234,8 +237,8 @@ response text: "${xhr.responseText}"`)
234237
if (Object.keys(this.enquiry_form_info).length !== 0 || this._getting_enquiry_info) {
235238
return
236239
}
237-
let xhr = new window.XMLHttpRequest()
238-
let url = `${config.api_root}/${public_key}/enquiry`
240+
const xhr = new window.XMLHttpRequest()
241+
const url = `${config.api_root}/${public_key}/enquiry`
239242
xhr.open('GET', url)
240243
xhr.onload = () => {
241244
if (xhr.status !== 200) {
@@ -251,9 +254,9 @@ response text: "${xhr.responseText}"`)
251254
xhr.send()
252255
},
253256
submit_enquiry: function (callback) {
254-
let data = JSON.stringify(clean(this.enquiry_data))
255-
let xhr = new window.XMLHttpRequest()
256-
let url = `${config.api_root}/${public_key}/enquiry`
257+
const data = JSON.stringify(clean(this.enquiry_data))
258+
const xhr = new window.XMLHttpRequest()
259+
const url = `${config.api_root}/${public_key}/enquiry`
257260
xhr.open('POST', url)
258261
xhr.onload = () => {
259262
if (xhr.status !== 201) {
@@ -280,6 +283,12 @@ response text: "${xhr.responseText}"`)
280283
return s
281284
}
282285
},
286+
ga_event: function (category, action, label) {
287+
/* istanbul ignore next */
288+
for (let prefix of ga_prefixes) {
289+
window.ga(prefix + 'send', 'event', category, action, label)
290+
}
291+
},
283292
goto: function (name, params) {
284293
this.$router.push({'name': name, params: params})
285294
}

src/utils.js

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,43 @@ const auto_url_root = (path) => {
3535
}
3636

3737
const add_script = (url) => {
38-
let s = document.createElement('script')
38+
const s = document.createElement('script')
39+
s.async = true
3940
s.src = url
4041
document.body.appendChild(s)
4142
}
4243

43-
export {to_markdown, clean, auto_url_root, add_script}
44+
/* istanbul ignore next */
45+
const init_ga = (router, config) => {
46+
const ga_prefixes = []
47+
if (process.env.GA_ID === null || window._tcs_ga !== undefined) {
48+
return ga_prefixes
49+
}
50+
window._tcs_ga = true
51+
ga_prefixes.push('tcs.')
52+
if (window.ga === undefined) {
53+
// taken from https://developers.google.com/analytics/devguides/collection/analyticsjs/
54+
add_script('https://www.google-analytics.com/analytics.js')
55+
window.ga = function () {
56+
(window.ga.q = window.ga.q || []).push(arguments)
57+
}
58+
window.ga.l = Number(new Date())
59+
} else {
60+
ga_prefixes.push('')
61+
}
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
66+
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+
})
74+
return ga_prefixes
75+
}
76+
77+
export {to_markdown, clean, auto_url_root, add_script, init_ga}

test/unit/specs/_shared.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,13 @@ function generate_vm (router, vm_data_) {
7272
},
7373
get_details: function () { this.__record_call('get_details') },
7474
get_enquiry: function () { this.__record_call('get_enquiry') },
75-
get_text: function (name, replacements) { this.__record_call('get_text') },
75+
get_text: function () { this.__record_call('get_text') },
7676
submit_enquiry: function (callback) {
7777
this.__record_call('submit_enquiry')
7878
this.enquiry_data = {}
7979
callback()
8080
},
81+
ga_event: function () { this.__record_call('ga_event') },
8182
}
8283
})
8384
}

0 commit comments

Comments
 (0)