-
-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathbrowser.js
More file actions
189 lines (179 loc) · 6.58 KB
/
Copy pathbrowser.js
File metadata and controls
189 lines (179 loc) · 6.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* global analytics */
const config = {
/* Your segment writeKey */
writeKey: null,
/* Disable anonymous MTU */
disableAnonymousTraffic: false,
/* Sync segment Anonymous id with `analytics` Anon id */
syncAnonymousId: false,
/* Enable/disable segment destinations https://bit.ly/38nRBj3 */
integrations: {}
/* Override the Segment snippet url, for loading via custom CDN proxy */
}
/**
* Segment analytics plugin
* @link https://getanalytics.io/plugins/segment/
* @link https://segment.com/docs/sources/website/analytics.js/
* @param {object} pluginConfig - Plugin settings
* @param {string} pluginConfig.writeKey - Your segment writeKey
* @param {boolean} [pluginConfig.disableAnonymousTraffic] - Disable loading segment for anonymous visitors
* @param {boolean} [pluginConfig.customScriptSrc] - Override the Segment snippet url, for loading via custom CDN proxy
* @param {object} [pluginConfig.integrations] - Enable/disable segment destinations https://bit.ly/38nRBj3
* @param {object} [pluginConfig.loadIntegrations] - Enable/disable loading of code for segment destinations http://bit.ly/4lWjAee
* @return {object} Analytics plugin
* @example
*
* segmentPlugin({
* writeKey: '123-xyz'
* })
*/
function segmentPlugin(pluginConfig = {}) {
return {
name: 'segment',
config: {
...config,
...pluginConfig
},
/* Custom methods to add .group call */
methods: {
/* Group https://segment.com/docs/connections/sources/catalog/libraries/website/javascript/#group */
group(groupId, traits = {}, options = {}, callback) {
// const analyticsInstance = this.instance
// If no segment, return early
if (typeof window.analytics === 'undefined') {
return
}
// Make group call to segment
window.analytics.group(groupId, traits, options, callback)
},
},
bootstrap: ({ config, instance }) => {
/* Load segment script after userId exists */
if (config.disableAnonymousTraffic && !instance.user('userId')) {
instance.once('identifyStart', ({ plugins }) => {
const self = plugins['segment']
if (!self.loaded()) {
// instance.loadPlugin('segment')
initialize({ config, instance })
}
})
}
},
/* Load Segment analytics.js on page */
initialize: initialize,
/* Trigger Segment page view http://bit.ly/2LSPFr1 */
page: ({ payload, config }) => {
if (typeof analytics === 'undefined') return
const properties = payload.properties || {}
const name = properties.name || properties.title
const category = properties.category
analytics.page(category, name, properties, {
integrations: config.integrations,
...payload.options,
})
},
/* Track Segment event http://bit.ly/2WLnYkK */
track: ({ payload, config }) => {
if (typeof analytics === 'undefined') return
analytics.track(payload.event, payload.properties, {
integrations: config.integrations,
...payload.options,
})
},
/* Identify Segment user http://bit.ly/2VL45xD */
identify: ({ payload, config }) => {
if (typeof analytics === 'undefined') return
const { userId, traits, options } = payload
if (typeof userId === 'string') {
analytics.identify(userId, traits, {
integrations: config.integrations,
...options,
})
} else {
analytics.identify(traits, {
integrations: config.integrations,
...options,
})
}
},
/* Remove segment cookies on analytics.reset */
reset: () => {
if (typeof analytics === 'undefined') return
analytics.reset();
},
/* Sync id when ready */
ready: ({ instance, config }) => {
if (!config.syncAnonymousId || typeof analytics === 'undefined') return
const segmentUser = analytics.user()
if (segmentUser) {
const segmentAnonId = segmentUser.anonymousId()
const analyticsAnonId = instance.user('anonymousId')
// If has segment anonymous ID && doesnt match analytics anon id, update
if (segmentAnonId && segmentAnonId !== analyticsAnonId) {
instance.setAnonymousId(segmentAnonId)
}
}
},
/* Check if segment loaded */
loaded: () => {
return window.analytics && !!analytics.initialized
}
}
}
function initialize({ config, instance }) {
const { disableAnonymousTraffic, writeKey, customScriptSrc } = config
if (!writeKey) {
throw new Error('No segment writeKey')
}
/* Disable segment.com if user is not yet identified. Save on Monthly MTU bill $$$ */
const userID = instance.user('userId')
if (!userID && disableAnonymousTraffic) {
return false
}
/* eslint-disable */
!function() {
var analytics = window.analytics = window.analytics || []
function isScriptLoaded() {
const scripts = document.getElementsByTagName('script')
const scriptMatch = customScriptSrc || 'cdn.segment.com/analytics.js/v1/'
return !!Object.keys(scripts).filter((key) => {
const scriptInfo = scripts[key] || {}
const src = scriptInfo.src || ''
return src.indexOf(scriptMatch) > -1
}).length
}
if (!analytics.initialize) {
if (!isScriptLoaded()) {
analytics.invoked = !0;
analytics.methods = ["trackSubmit", "trackClick", "trackLink", "trackForm", "pageview", "identify", "reset", "group", "track", "ready", "alias", "debug", "page", "once", "off", "on"];
analytics.factory = function(t) {
return function() {
var e = Array.prototype.slice.call(arguments);
e.unshift(t);
analytics.push(e);
return analytics
}
};
for (var t = 0; t < analytics.methods.length; t++) {
var e = analytics.methods[t];
analytics[e] = analytics.factory(e)
}
analytics.load = function(t, e) {
var n = document.createElement("script");
n.type = "text/javascript";
n.async = !0;
n.src = customScriptSrc || "https://cdn.segment.com/analytics.js/v1/" + t + "/analytics.min.js";
n.id = 'segment-io'
var a = document.getElementsByTagName("script")[0];
a.parentNode.insertBefore(n, a);
analytics._loadOptions = e
};
analytics.SNIPPET_VERSION = "4.1.0";
const loadIntegrations = config.loadIntegrations && { integrations: config.loadIntegrations } || undefined;
analytics.load(writeKey, loadIntegrations);
}
}
}();
/* eslint-enable */
}
export default segmentPlugin