@@ -9,6 +9,7 @@ var Track = require('segmentio-facade').Track;
9
9
var each = require ( '@ndhoule/each' ) ;
10
10
var del = require ( 'obj-case' ) . del ;
11
11
var clone = require ( '@ndhoule/clone' ) ;
12
+ var appboyUtil = require ( './appboyUtil' ) ;
12
13
13
14
/**
14
15
* Expose `Appboy` integration.
@@ -34,6 +35,7 @@ var Appboy = (module.exports = integration('Appboy')
34
35
. option ( 'customEndpoint' , '' )
35
36
. option ( 'version' , 1 )
36
37
. option ( 'logPurchaseWhenRevenuePresent' , false )
38
+ . option ( 'onlyTrackKnownUsersOnWeb' , false )
37
39
. tag (
38
40
'v1' ,
39
41
'<script src="https://js.appboycdn.com/web-sdk/1.6/appboy.min.js">'
@@ -43,32 +45,25 @@ var Appboy = (module.exports = integration('Appboy')
43
45
'<script src="https://js.appboycdn.com/web-sdk/2.4/appboy.min.js">'
44
46
) ) ;
45
47
46
- Appboy . prototype . initialize = function ( ) {
47
- var options = this . options ;
48
- var customEndpoint ;
49
- // Setup custom endpoints
50
- if ( options . customEndpoint ) {
51
- var endpoint = options . customEndpoint ;
52
- var regex = new RegExp ( '^(http|https)://' , 'i' ) ;
53
- customEndpoint =
54
- ( regex . test ( endpoint ) ? endpoint : 'https://' + endpoint ) + '/api/v3' ;
55
- } else if ( options . datacenter === 'eu' ) {
56
- customEndpoint = 'https://sdk.fra-01.braze.eu/api/v3' ;
57
- }
48
+ Appboy . prototype . appboyInitialize = function ( userId , options , config ) {
49
+ window . appboy . initialize ( options . apiKey , config ) ;
50
+
51
+ if ( options . automaticallyDisplayMessages )
52
+ window . appboy . display . automaticallyShowNewInAppMessages ( ) ;
53
+ if ( userId ) window . appboy . changeUser ( userId ) ;
54
+
55
+ window . appboy . openSession ( ) ;
56
+ } ;
58
57
59
- if ( Number ( options . version ) === 2 ) {
60
- this . initializeV2 ( customEndpoint ) ;
58
+ Appboy . prototype . initialize = function ( ) {
59
+ if ( appboyUtil . isVersionTwo ( this . options ) ) {
60
+ this . initializeV2 ( ) ;
61
61
} else {
62
- this . initializeV1 ( customEndpoint ) ;
62
+ this . initializeV1 ( ) ;
63
63
}
64
64
} ;
65
- /**
66
- * Initialize v1.
67
- *
68
- * @api public
69
- */
70
65
71
- Appboy . prototype . initializeV1 = function ( customEndpoint ) {
66
+ Appboy . prototype . initializeV1 = function ( ) {
72
67
var options = this . options ;
73
68
var self = this ;
74
69
var userId = this . analytics . user ( ) . id ( ) ;
@@ -79,7 +74,7 @@ Appboy.prototype.initializeV1 = function(customEndpoint) {
79
74
window . appboy = { } ;
80
75
for (
81
76
var s = 'destroy toggleAppboyLogging setLogger openSession changeUser requestImmediateDataFlush requestFeedRefresh subscribeToFeedUpdates logCardImpressions logCardClick logFeedDisplayed requestInAppMessageRefresh logInAppMessageImpression logInAppMessageClick logInAppMessageButtonClick subscribeToNewInAppMessages removeSubscription removeAllSubscriptions logCustomEvent logPurchase isPushSupported isPushBlocked isPushGranted isPushPermissionGranted registerAppboyPushMessages unregisterAppboyPushMessages submitFeedback ab ab.User ab.User.Genders ab.User.NotificationSubscriptionTypes ab.User.prototype.getUserId ab.User.prototype.setFirstName ab.User.prototype.setLastName ab.User.prototype.setEmail ab.User.prototype.setGender ab.User.prototype.setDateOfBirth ab.User.prototype.setCountry ab.User.prototype.setHomeCity ab.User.prototype.setEmailNotificationSubscriptionType ab.User.prototype.setPushNotificationSubscriptionType ab.User.prototype.setPhoneNumber ab.User.prototype.setAvatarImageUrl ab.User.prototype.setLastKnownLocation ab.User.prototype.setUserAttribute ab.User.prototype.setCustomUserAttribute ab.User.prototype.addToCustomAttributeArray ab.User.prototype.removeFromCustomAttributeArray ab.User.prototype.incrementCustomUserAttribute ab.InAppMessage ab.InAppMessage.SlideFrom ab.InAppMessage.ClickAction ab.InAppMessage.DismissType ab.InAppMessage.OpenTarget ab.InAppMessage.ImageStyle ab.InAppMessage.Orientation ab.InAppMessage.CropType ab.InAppMessage.prototype.subscribeToClickedEvent ab.InAppMessage.prototype.subscribeToDismissedEvent ab.InAppMessage.prototype.removeSubscription ab.InAppMessage.prototype.removeAllSubscriptions ab.InAppMessage.Button ab.InAppMessage.Button.prototype.subscribeToClickedEvent ab.InAppMessage.Button.prototype.removeSubscription ab.InAppMessage.Button.prototype.removeAllSubscriptions ab.SlideUpMessage ab.ModalMessage ab.FullScreenMessage ab.ControlMessage ab.Feed ab.Feed.prototype.getUnreadCardCount ab.Card ab.ClassicCard ab.CaptionedImage ab.Banner ab.WindowUtils display display.automaticallyShowNewInAppMessages display.showInAppMessage display.showFeed display.destroyFeed display.toggleFeed sharedLib' . split (
82
- ' '
77
+ ' '
83
78
) ,
84
79
i = 0 ;
85
80
i < s . length ;
@@ -93,9 +88,9 @@ Appboy.prototype.initializeV1 = function(customEndpoint) {
93
88
}
94
89
appboy . initialize = function ( ) {
95
90
console &&
96
- console . error (
97
- 'Appboy cannot be loaded - this is usually due to strict corporate firewalls or ad blockers.'
98
- ) ;
91
+ console . error (
92
+ 'Appboy cannot be loaded - this is usually due to strict corporate firewalls or ad blockers.'
93
+ ) ;
99
94
} ;
100
95
appboy . getUser = function ( ) {
101
96
return new appboy . ab . User ( ) ;
@@ -110,50 +105,18 @@ Appboy.prototype.initializeV1 = function(customEndpoint) {
110
105
this . _shim = window . appboy . initialize ;
111
106
112
107
this . load ( 'v1' , function ( ) {
113
- var config = { } ;
114
- var datacenterMappings = {
115
- us : 'https://sdk.iad-01.braze.com' ,
116
- us02 : 'https://sdk.iad-02.braze.com' ,
117
- us03 : 'https://sdk.iad-03.braze.com' ,
118
- eu : 'https://sdk.fra-01.braze.eu'
119
- } ;
120
- if ( options . safariWebsitePushId )
121
- config . safariWebsitePushId = options . safariWebsitePushId ;
122
- if ( options . enableHtmlInAppMessages ) config . enableHtmlInAppMessages = true ;
123
-
124
- // Setup custom endpoints
125
- if ( options . customEndpoint ) {
126
- var endpoint = options . customEndpoint ;
127
- var regex = new RegExp ( '^(http|https)://' , 'i' ) ;
128
- config . baseUrl =
129
- ( regex . test ( endpoint ) ? endpoint : 'https://' + endpoint ) + '/api/v3' ;
130
- } else {
131
- config . baseUrl =
132
- ( datacenterMappings [ options . datacenter ] ||
133
- 'https://sdk.iad-01.braze.com' ) + '/api/v3' ;
108
+ if ( appboyUtil . shouldOpenSession ( userId , options ) ) {
109
+ self . hasBeenInitialized = true ;
110
+ var config = appboyUtil . getConfig ( options ) ;
111
+ self . initializeTester ( options . apiKey , config ) ;
112
+ self . appboyInitialize ( userId , options , config ) ;
134
113
}
135
114
136
- if ( customEndpoint ) config . baseUrl = customEndpoint ;
137
-
138
- self . initializeTester ( options . apiKey , config ) ;
139
- window . appboy . initialize ( options . apiKey , config ) ;
140
-
141
- if ( options . automaticallyDisplayMessages )
142
- window . appboy . display . automaticallyShowNewInAppMessages ( ) ;
143
- if ( userId ) window . appboy . changeUser ( userId ) ;
144
-
145
- window . appboy . openSession ( ) ;
146
115
self . ready ( ) ;
147
116
} ) ;
148
117
} ;
149
118
150
- /**
151
- * Initialize v2.
152
- *
153
- * @api public
154
- */
155
-
156
- Appboy . prototype . initializeV2 = function ( customEndpoint ) {
119
+ Appboy . prototype . initializeV2 = function ( ) {
157
120
var options = this . options ;
158
121
var userId = this . analytics . user ( ) . id ( ) ;
159
122
@@ -163,7 +126,7 @@ Appboy.prototype.initializeV2 = function(customEndpoint) {
163
126
window . appboyQueue = [ ] ;
164
127
for (
165
128
var s = 'initialize destroy getDeviceId toggleAppboyLogging setLogger openSession changeUser requestImmediateDataFlush requestFeedRefresh subscribeToFeedUpdates logCardImpressions logCardClick logFeedDisplayed requestInAppMessageRefresh logInAppMessageImpression logInAppMessageClick logInAppMessageButtonClick logInAppMessageHtmlClick subscribeToNewInAppMessages removeSubscription removeAllSubscriptions logCustomEvent logPurchase isPushSupported isPushBlocked isPushGranted isPushPermissionGranted registerAppboyPushMessages unregisterAppboyPushMessages submitFeedback trackLocation stopWebTracking resumeWebTracking wipeData ab ab.User ab.User.Genders ab.User.NotificationSubscriptionTypes ab.User.prototype.getUserId ab.User.prototype.setFirstName ab.User.prototype.setLastName ab.User.prototype.setEmail ab.User.prototype.setGender ab.User.prototype.setDateOfBirth ab.User.prototype.setCountry ab.User.prototype.setHomeCity ab.User.prototype.setLanguage ab.User.prototype.setEmailNotificationSubscriptionType ab.User.prototype.setPushNotificationSubscriptionType ab.User.prototype.setPhoneNumber ab.User.prototype.setAvatarImageUrl ab.User.prototype.setLastKnownLocation ab.User.prototype.setUserAttribute ab.User.prototype.setCustomUserAttribute ab.User.prototype.addToCustomAttributeArray ab.User.prototype.removeFromCustomAttributeArray ab.User.prototype.incrementCustomUserAttribute ab.User.prototype.addAlias ab.InAppMessage ab.InAppMessage.SlideFrom ab.InAppMessage.ClickAction ab.InAppMessage.DismissType ab.InAppMessage.OpenTarget ab.InAppMessage.ImageStyle ab.InAppMessage.TextAlignment ab.InAppMessage.Orientation ab.InAppMessage.CropType ab.InAppMessage.prototype.subscribeToClickedEvent ab.InAppMessage.prototype.subscribeToDismissedEvent ab.InAppMessage.prototype.removeSubscription ab.InAppMessage.prototype.removeAllSubscriptions ab.InAppMessage.Button ab.InAppMessage.Button.prototype.subscribeToClickedEvent ab.InAppMessage.Button.prototype.removeSubscription ab.InAppMessage.Button.prototype.removeAllSubscriptions ab.SlideUpMessage ab.ModalMessage ab.FullScreenMessage ab.HtmlMessage ab.ControlMessage ab.Feed ab.Feed.prototype.getUnreadCardCount ab.Card ab.ClassicCard ab.CaptionedImage ab.Banner ab.WindowUtils display display.automaticallyShowNewInAppMessages display.showInAppMessage display.showFeed display.destroyFeed display.toggleFeed sharedLib' . split (
166
- ' '
129
+ ' '
167
130
) ,
168
131
i = 0 ;
169
132
i < s . length ;
@@ -177,8 +140,8 @@ Appboy.prototype.initializeV2 = function(customEndpoint) {
177
140
k = k [ l [ j ] ] ;
178
141
k [ l [ j ] ] = new Function (
179
142
'return function ' +
180
- m . replace ( / \. / g, '_' ) +
181
- '(){appboyQueue.push(arguments); return true}'
143
+ m . replace ( / \. / g, '_' ) +
144
+ '(){appboyQueue.push(arguments); return true}'
182
145
) ( ) ;
183
146
}
184
147
appboy . getUser = function ( ) {
@@ -190,38 +153,23 @@ Appboy.prototype.initializeV2 = function(customEndpoint) {
190
153
} ) ( window , document , 'script' ) ;
191
154
/* eslint-enable */
192
155
193
- // https://js.appboycdn.com/web-sdk/2.0/doc/module-appboy.html#.initialize
194
- var config = {
195
- safariWebsitePushId : options . safariWebsitePushId ,
196
- enableHtmlInAppMessages : options . enableHtmlInAppMessages ,
197
- allowCrawlerActivity : options . allowCrawlerActivity ,
198
- doNotLoadFontAwesome : options . doNotLoadFontAwesome ,
199
- enableLogging : options . enableLogging ,
200
- localization : options . localization ,
201
- minimumIntervalBetweenTriggerActionsInSeconds :
202
- Number ( options . minimumIntervalBetweenTriggerActionsInSeconds ) || 30 ,
203
- openInAppMessagesInNewTab : options . openInAppMessagesInNewTab ,
204
- openNewsFeedCardsInNewTab : options . openNewsFeedCardsInNewTab ,
205
- requireExplicitInAppMessageDismissal :
206
- options . requireExplicitInAppMessageDismissal ,
207
- serviceWorkerLocation : options . serviceWorkerLocation ,
208
- sessionTimeoutInSeconds : Number ( options . sessionTimeoutInSeconds ) || 30
209
- } ;
210
-
211
- if ( customEndpoint ) config . baseUrl = customEndpoint ;
212
-
213
- this . initializeTester ( options . apiKey , config ) ;
214
- window . appboy . initialize ( options . apiKey , config ) ;
215
-
216
- if ( options . automaticallyDisplayMessages )
217
- window . appboy . display . automaticallyShowNewInAppMessages ( ) ;
218
- if ( userId ) window . appboy . changeUser ( userId ) ;
219
-
220
- window . appboy . openSession ( ) ;
156
+ if ( appboyUtil . shouldOpenSession ( userId , options ) ) {
157
+ this . hasBeenInitialized = true ;
158
+ var config = appboyUtil . getConfig ( options ) ;
159
+ this . initializeTester ( options . apiKey , config ) ;
160
+ this . appboyInitialize ( userId , options , config ) ;
161
+ }
221
162
222
163
this . load ( 'v2' , this . ready ) ;
223
164
} ;
224
165
166
+ /**
167
+ * @returns {boolean } true if integration should handle event.
168
+ */
169
+ Appboy . prototype . shouldHandleEvent = function ( ) {
170
+ return ! this . options . onlyTrackKnownUsersOnWeb || this . hasBeenInitialized ;
171
+ } ;
172
+
225
173
// This is used to test window.appboy.initialize
226
174
Appboy . prototype . initializeTester = function ( ) { } ;
227
175
@@ -231,20 +179,12 @@ Appboy.prototype.initializeTester = function() {};
231
179
* @api private
232
180
* @return {boolean }
233
181
*/
234
-
235
182
Appboy . prototype . loaded = function ( ) {
236
183
var options = this . options ;
237
184
if ( Number ( options . version ) === 2 ) return window . appboyQueue === null ;
238
185
return window . appboy && window . appboy . initialize !== this . _shim ;
239
186
} ;
240
187
241
- /**
242
- * Identify.
243
- *
244
- * @api public
245
- * @param {Identify } identify
246
- */
247
-
248
188
Appboy . prototype . identify = function ( identify ) {
249
189
var userId = identify . userId ( ) ;
250
190
var address = identify . address ( ) ;
@@ -257,6 +197,24 @@ Appboy.prototype.identify = function(identify) {
257
197
var phone = identify . phone ( ) ;
258
198
var traits = clone ( identify . traits ( ) ) ;
259
199
200
+ var options = this . options ;
201
+
202
+ if (
203
+ this . options . onlyTrackKnownUsersOnWeb &&
204
+ userId &&
205
+ ! this . hasBeenInitialized // To avoid calling this more than once.
206
+ ) {
207
+ // Rerun initial initialization.
208
+ this . hasBeenInitialized = true ;
209
+ var config = appboyUtil . getConfig ( options ) ;
210
+ this . initializeTester ( options . apiKey , config ) ;
211
+ this . appboyInitialize ( userId , options , config ) ;
212
+ }
213
+
214
+ if ( this . options . onlyTrackKnownUsersOnWeb && ! this . hasBeenInitialized ) {
215
+ return ;
216
+ }
217
+
260
218
if ( userId ) {
261
219
window . appboy . changeUser ( userId ) ;
262
220
}
@@ -355,6 +313,9 @@ Appboy.prototype.identify = function(identify) {
355
313
*/
356
314
357
315
Appboy . prototype . group = function ( group ) {
316
+ if ( ! this . shouldHandleEvent ( ) ) {
317
+ return ;
318
+ }
358
319
var userId = group . userId ( ) ;
359
320
var groupIdKey = 'ab_segment_group_' + group . groupId ( ) ;
360
321
@@ -374,6 +335,9 @@ Appboy.prototype.group = function(group) {
374
335
*/
375
336
376
337
Appboy . prototype . track = function ( track ) {
338
+ if ( ! this . shouldHandleEvent ( ) ) {
339
+ return ;
340
+ }
377
341
var userId = track . userId ( ) ;
378
342
var eventName = track . event ( ) ;
379
343
var properties = track . properties ( ) ;
@@ -408,6 +372,9 @@ Appboy.prototype.track = function(track) {
408
372
*/
409
373
410
374
Appboy . prototype . page = function ( page ) {
375
+ if ( ! this . shouldHandleEvent ( ) ) {
376
+ return ;
377
+ }
411
378
var settings = this . options ;
412
379
if ( ! settings . trackAllPages && ! settings . trackNamedPages ) return ;
413
380
if ( settings . trackNamedPages && ! page . name ( ) ) return ;
@@ -435,6 +402,9 @@ Appboy.prototype.page = function(page) {
435
402
*/
436
403
437
404
Appboy . prototype . orderCompleted = function ( track ) {
405
+ if ( ! this . shouldHandleEvent ( ) ) {
406
+ return ;
407
+ }
438
408
var userId = track . userId ( ) ;
439
409
var products = track . products ( ) ;
440
410
var currencyCode = track . currency ( ) ;
0 commit comments