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