@@ -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 ( / \/ e n q u i r y $ / , '/' )
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 */
4545const 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
0 commit comments