1
1
/* global, window */
2
- let loadedInstances = { }
2
+ let loadedInstances = { } ;
3
3
/* Location of gtag script */
4
- const gtagScriptSource = ' https://www.googletagmanager.com/gtag/js'
4
+ const gtagScriptSource = " https://www.googletagmanager.com/gtag/js" ;
5
5
// See https://developers.google.com/analytics/devguides/collection/ga4/reference/config
6
6
const defaultGtagConf = {
7
7
// https://support.google.com/analytics/answer/7201382?hl=en#zippy=%2Cglobal-site-tag-websites
@@ -35,15 +35,15 @@ const defaultGtagConf = {
35
35
* Cookie Flags
36
36
* https://developers.google.com/analytics/devguides/collection/ga4/cookies-user-id#cookie_flags
37
37
*/
38
- cookie_flags : ''
39
- }
38
+ cookie_flags : "" ,
39
+ } ;
40
40
41
41
const defaultConfig = {
42
- gtagName : ' gtag' ,
43
- dataLayerName : ' ga4DataLayer' ,
42
+ gtagName : " gtag" ,
43
+ dataLayerName : " ga4DataLayer" ,
44
44
measurementIds : [ ] ,
45
45
gtagConfig : defaultGtagConf ,
46
- }
46
+ } ;
47
47
48
48
/**
49
49
* Google analytics plugin
@@ -70,83 +70,93 @@ const defaultConfig = {
70
70
* })
71
71
*/
72
72
function googleAnalytics ( pluginConfig = { } ) {
73
- let pageCallCount = 0
74
- let measurementIds = getIds ( pluginConfig . measurementIds )
73
+ let pageCallCount = 0 ;
74
+ let measurementIds = getIds ( pluginConfig . measurementIds ) ;
75
75
const initConfig = {
76
76
...defaultConfig ,
77
77
...pluginConfig ,
78
- }
79
-
78
+ } ;
79
+
80
80
return {
81
- name : ' google-analytics' ,
81
+ name : " google-analytics" ,
82
82
config : initConfig ,
83
83
// Load gtag.js and define gtag
84
84
initialize : ( { config, instance } ) => {
85
- const { dataLayerName, customScriptSrc, gtagName, gtagConfig, debug } = config
85
+ const { dataLayerName, customScriptSrc, gtagName, gtagConfig, debug } =
86
+ config ;
86
87
/* Inject google gtag.js script if not found */
87
88
if ( ! scriptLoaded ( customScriptSrc || gtagScriptSource ) ) {
88
- const customLayerName = ( dataLayerName ) ? `&l=${ dataLayerName } ` : ''
89
- const script = document . createElement ( 'script' )
90
- script . async = true
91
- script . src = customScriptSrc || `${ gtagScriptSource } ?id=${ measurementIds [ 0 ] } ${ customLayerName } `
92
- document . body . appendChild ( script )
89
+ const customLayerName = dataLayerName ? `&l=${ dataLayerName } ` : "" ;
90
+ const script = document . createElement ( "script" ) ;
91
+ script . async = true ;
92
+ script . src =
93
+ customScriptSrc ||
94
+ `${ gtagScriptSource } ?id=${ measurementIds [ 0 ] } ${ customLayerName } ` ;
95
+ document . body . appendChild ( script ) ;
93
96
}
94
97
/* Set gtag and datalayer */
95
98
if ( ! window [ dataLayerName ] ) {
96
- window [ dataLayerName ] = window [ dataLayerName ] || [ ]
97
- window [ gtagName ] = function ( ) {
98
- window [ dataLayerName ] . push ( arguments )
99
- }
100
- window [ gtagName ] ( 'js' , new Date ( ) )
99
+ window [ dataLayerName ] = window [ dataLayerName ] || [ ] ;
100
+ window [ gtagName ] = function ( ) {
101
+ window [ dataLayerName ] . push ( arguments ) ;
102
+ } ;
103
+ window [ gtagName ] ( "js" , new Date ( ) ) ;
101
104
}
102
105
// Initialize tracker instances on page
103
106
let gtagConf = {
104
107
...defaultGtagConf ,
105
- ...( gtagConfig ) ? gtagConfig : { } ,
106
- ...( debug ) ? { debug_mode : true } : { } ,
108
+ ...( gtagConfig ? gtagConfig : { } ) ,
109
+ } ;
110
+ // You must explicitly delete the debug_mode parameter or all sessions will fire in debug more. Setting it false is not enough.
111
+ // https://support.google.com/analytics/answer/7201382?hl=en&ref_topic=9303319#zippy=%2Cgoogle-tag-websites:~:text=To%20disable%20debug%20mode%2C%20exclude%20the%20%27debug_mode%27%20parameter%3B%20setting%20the%20parameter%20to%20false%20doesn%27t%20disable%20debug%20mode.
112
+ if ( debug === true ) {
113
+ gtagConf . debug_mode = true ;
114
+ } else {
115
+ delete gtagConf . debug_mode ;
107
116
}
108
117
/* set custom dimensions from user traits */
109
- const user = instance . user ( ) || { }
110
- const traits = user . traits || { }
118
+ const user = instance . user ( ) || { } ;
119
+ const traits = user . traits || { } ;
111
120
if ( Object . keys ( traits ) . length ) {
112
- window [ gtagName ] ( ' set' , ' user_properties' , traits )
121
+ window [ gtagName ] ( " set" , " user_properties" , traits ) ;
113
122
}
114
123
/* Initialize all measurementIds */
115
124
for ( var i = 0 ; i < measurementIds . length ; i ++ ) {
116
125
if ( ! loadedInstances [ measurementIds [ i ] ] ) {
117
- window [ gtagName ] ( 'config' , measurementIds [ i ] , gtagConf )
118
- loadedInstances [ measurementIds [ i ] ] = true
126
+ console . log ( "a" , gtagConf ) ;
127
+ window [ gtagName ] ( "config" , measurementIds [ i ] , gtagConf ) ;
128
+ loadedInstances [ measurementIds [ i ] ] = true ;
119
129
}
120
130
}
121
131
} ,
122
132
// Set parameter scope at user level with 'set' method
123
133
identify : ( { payload, config } ) => {
124
- const { gtagName } = config
125
- if ( ! window [ gtagName ] || ! measurementIds . length ) return
134
+ const { gtagName } = config ;
135
+ if ( ! window [ gtagName ] || ! measurementIds . length ) return ;
126
136
if ( payload . userId ) {
127
137
// https://developers.google.com/analytics/devguides/collection/ga4/user-id?platform=websites#send_user_ids
128
- window [ gtagName ] ( ' set' , { user_id : payload . userId } )
138
+ window [ gtagName ] ( " set" , { user_id : payload . userId } ) ;
129
139
// console.log('Set userid', payload.userId)
130
140
}
131
- // TODO verify this
141
+ // TODO verify this
132
142
// https://developers.google.com/analytics/devguides/collection/ga4/user-properties?technology=websites
133
143
if ( Object . keys ( payload . traits ) . length ) {
134
144
/* gtag('set', 'user_properties', {
135
145
favorite_composer: 'Mahler',
136
146
favorite_instrument: 'double bass',
137
147
season_ticketholder: 'true'
138
148
}) */
139
- window [ gtagName ] ( ' set' , ' user_properties' , payload . traits )
149
+ window [ gtagName ] ( " set" , " user_properties" , payload . traits ) ;
140
150
// console.log('Set userprops', payload.traits)
141
151
}
142
152
} ,
143
153
// Set parameter scope at page level with 'config' method
144
154
page : ( { payload, config, instance } ) => {
145
- const { gtagName, gtagConfig } = config
146
- if ( ! window [ gtagName ] || ! measurementIds . length ) return
147
- const { properties } = payload
148
- const { send_to } = properties
149
- const campaign = instance . getState ( ' context.campaign' )
155
+ const { gtagName, gtagConfig } = config ;
156
+ if ( ! window [ gtagName ] || ! measurementIds . length ) return ;
157
+ const { properties } = payload ;
158
+ const { send_to } = properties ;
159
+ const campaign = instance . getState ( " context.campaign" ) ;
150
160
// console.log('ga page properties', properties)
151
161
/* Create pageview-related properties */
152
162
const pageView = {
@@ -156,38 +166,38 @@ function googleAnalytics(pluginConfig = {}) {
156
166
page_hash : properties . hash ,
157
167
page_search : properties . page_search ,
158
168
page_referrer : properties . referrer ,
159
- }
160
- const campaignData = addCampaignData ( campaign )
169
+ } ;
170
+ const campaignData = addCampaignData ( campaign ) ;
161
171
const finalPayload = {
162
- ...( send_to ) ? { send_to } : { } ,
172
+ ...( send_to ? { send_to } : { } ) ,
163
173
...pageView ,
164
174
...campaignData ,
165
- }
175
+ } ;
166
176
/* If send_page_view true, ignore first analytics.page call */
167
177
if ( gtagConfig && gtagConfig . send_page_view && pageCallCount === 0 ) {
168
- pageCallCount ++
178
+ pageCallCount ++ ;
169
179
// console.log('ignore first pageCallCount', pageCallCount)
170
- return
180
+ return ;
171
181
}
172
182
// console.log('Send page_view payload', finalPayload)
173
- window [ gtagName ] ( ' event' , ' page_view' , finalPayload )
183
+ window [ gtagName ] ( " event" , " page_view" , finalPayload ) ;
174
184
// Set after initial page view
175
- pageCallCount ++
185
+ pageCallCount ++ ;
176
186
} ,
177
187
// Set parameter scope at event level with 'event' method
178
188
track : ( { payload, config, instance } ) => {
179
- const { properties, event } = payload
180
- const campaign = instance . getState ( ' context.campaign' )
181
- const { gtagName } = config
182
- if ( ! window [ gtagName ] || ! measurementIds . length ) return
189
+ const { properties, event } = payload ;
190
+ const campaign = instance . getState ( " context.campaign" ) ;
191
+ const { gtagName } = config ;
192
+ if ( ! window [ gtagName ] || ! measurementIds . length ) return ;
183
193
/* Attach campaign data */
184
- const campaignData = addCampaignData ( campaign )
194
+ const campaignData = addCampaignData ( campaign ) ;
185
195
// Limits https://support.google.com/analytics/answer/9267744
186
196
const finalPayload = {
187
197
...properties ,
188
198
/* Attach campaign data, if exists */
189
199
...campaignData ,
190
- }
200
+ } ;
191
201
/*
192
202
console.log('finalPayload', finalPayload)
193
203
console.log('event', event)
@@ -197,61 +207,64 @@ function googleAnalytics(pluginConfig = {}) {
197
207
<event_params>key: value,
198
208
})
199
209
*/
200
- window [ gtagName ] ( ' event' , event , finalPayload )
210
+ window [ gtagName ] ( " event" , event , finalPayload ) ;
201
211
} ,
202
212
/* Verify gtag loaded and ready to use */
203
213
loaded : ( ) => {
204
- const { dataLayerName, customScriptSrc } = initConfig
205
- const hasDataLayer = dataLayerName && ( window [ dataLayerName ] && Array . prototype . push === window [ dataLayerName ] . push )
206
- return scriptLoaded ( customScriptSrc || gtagScriptSource ) && hasDataLayer
214
+ const { dataLayerName, customScriptSrc } = initConfig ;
215
+ const hasDataLayer =
216
+ dataLayerName &&
217
+ window [ dataLayerName ] &&
218
+ Array . prototype . push === window [ dataLayerName ] . push ;
219
+ return scriptLoaded ( customScriptSrc || gtagScriptSource ) && hasDataLayer ;
207
220
} ,
208
221
/* Custom methods */
209
222
methods : {
210
223
addTag ( tagId , settings = { } ) {
211
224
// https://developers.google.com/tag-platform/devguides/install-gtagjs#add_products_to_your_tag
212
225
if ( window [ initConfig . gtagName ] ) {
213
- window [ initConfig . gtagName ] ( ' config' , tagId , settings )
226
+ window [ initConfig . gtagName ] ( " config" , tagId , settings ) ;
214
227
// Add tag id
215
228
if ( measurementIds && ! measurementIds . includes ( tagId ) ) {
216
- measurementIds = measurementIds . concat ( tagId )
229
+ measurementIds = measurementIds . concat ( tagId ) ;
217
230
}
218
231
}
219
232
} ,
220
233
/* Disable gtag for user */
221
234
disable : ( ids ) => {
222
- const gaIds = ( ids ) ? getIds ( ids ) : measurementIds
235
+ const gaIds = ids ? getIds ( ids ) : measurementIds ;
223
236
for ( var i = 0 ; i < measurementIds . length ; i ++ ) {
224
- const gaId = measurementIds [ i ]
237
+ const gaId = measurementIds [ i ] ;
225
238
if ( gaIds . includes ( gaId ) ) {
226
239
// https://developers.google.com/analytics/devguides/collection/gtagjs/user-opt-out
227
- window [ `ga-disable-${ gaId } ` ] = true
240
+ window [ `ga-disable-${ gaId } ` ] = true ;
228
241
}
229
242
}
230
243
} ,
231
244
/* Enable gtag for user */
232
245
enable : ( ids ) => {
233
- const gaIds = ( ids ) ? getIds ( ids ) : measurementIds
246
+ const gaIds = ids ? getIds ( ids ) : measurementIds ;
234
247
for ( var i = 0 ; i < measurementIds . length ; i ++ ) {
235
- const gaId = measurementIds [ i ]
248
+ const gaId = measurementIds [ i ] ;
236
249
if ( gaIds . includes ( gaId ) ) {
237
250
// https://developers.google.com/analytics/devguides/collection/gtagjs/user-opt-out
238
- window [ `ga-disable-${ gaId } ` ] = false
251
+ window [ `ga-disable-${ gaId } ` ] = false ;
239
252
}
240
253
}
241
- }
254
+ } ,
242
255
} ,
243
- }
256
+ } ;
244
257
}
245
258
246
259
function getIds ( measurementIds ) {
247
- if ( ! measurementIds ) throw new Error ( ' No GA Measurement ID defined' )
260
+ if ( ! measurementIds ) throw new Error ( " No GA Measurement ID defined" ) ;
248
261
if ( Array . isArray ( measurementIds ) ) {
249
- return measurementIds
262
+ return measurementIds ;
250
263
}
251
- if ( typeof measurementIds === ' string' ) {
252
- return [ measurementIds ]
264
+ if ( typeof measurementIds === " string" ) {
265
+ return [ measurementIds ] ;
253
266
}
254
- throw new Error ( ' GA Measurement ID must be string or array of strings' )
267
+ throw new Error ( " GA Measurement ID must be string or array of strings" ) ;
255
268
}
256
269
257
270
/**
@@ -264,21 +277,23 @@ function getIds(measurementIds) {
264
277
* @param {String } [campaignData.campaignKeyword] - Keyword of campaign
265
278
*/
266
279
function addCampaignData ( campaignData = { } ) {
267
- let campaign = { }
268
- const { id, name, source, medium, content, keyword } = campaignData
269
- if ( id ) campaign . campaignId = id
270
- if ( name ) campaign . campaignName = name
271
- if ( source ) campaign . campaignSource = source
272
- if ( medium ) campaign . campaignMedium = medium
273
- if ( content ) campaign . campaignContent = content
274
- if ( keyword ) campaign . campaignKeyword = keyword
275
- return campaign
280
+ let campaign = { } ;
281
+ const { id, name, source, medium, content, keyword } = campaignData ;
282
+ if ( id ) campaign . campaignId = id ;
283
+ if ( name ) campaign . campaignName = name ;
284
+ if ( source ) campaign . campaignSource = source ;
285
+ if ( medium ) campaign . campaignMedium = medium ;
286
+ if ( content ) campaign . campaignContent = content ;
287
+ if ( keyword ) campaign . campaignKeyword = keyword ;
288
+ return campaign ;
276
289
}
277
290
278
291
function scriptLoaded ( scriptSrc ) {
279
- const scripts = document . querySelectorAll ( 'script[src]' )
280
- const regex = new RegExp ( `^${ scriptSrc } ` )
281
- return Boolean ( Object . values ( scripts ) . filter ( ( value ) => regex . test ( value . src ) ) . length )
292
+ const scripts = document . querySelectorAll ( "script[src]" ) ;
293
+ const regex = new RegExp ( `^${ scriptSrc } ` ) ;
294
+ return Boolean (
295
+ Object . values ( scripts ) . filter ( ( value ) => regex . test ( value . src ) ) . length
296
+ ) ;
282
297
}
283
298
284
- export default googleAnalytics
299
+ export default googleAnalytics ;
0 commit comments