Skip to content

Commit 5716304

Browse files
appboy-web-sdk v3 support (#542)
Co-authored-by: Marcus Ericsson <36717+mericsson@users.noreply.github.com> Co-authored-by: David Bielik <davidbielik@users.noreply.github.com>
1 parent 30cd992 commit 5716304

File tree

5 files changed

+68
-6
lines changed

5 files changed

+68
-6
lines changed

integrations/appboy/HISTORY.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
1.16.0 / 2020-12-16
2+
===================
3+
4+
* Adds support for Braze 3.1 via version option
5+
16
1.15.1 / 2020-12-14
27
===================
38

integrations/appboy/lib/appboyUtil.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,22 @@ var appboyUtil = {
2222
return customEndpoint;
2323
},
2424

25+
getMajorVersion: function(options) {
26+
return Math.floor(Number(options.version));
27+
},
2528
isMajorVersionTwo: function(options) {
26-
return Math.floor(Number(options.version)) === 2;
29+
return appboyUtil.getMajorVersion(options) === 2;
30+
},
31+
isMajorVersionThree: function(options) {
32+
return appboyUtil.getMajorVersion(options) === 3;
2733
},
28-
2934
getConfig: function(options) {
3035
var config = {};
31-
if (appboyUtil.isMajorVersionTwo(options)) {
32-
// https://js.appboycdn.com/web-sdk/2.0/doc/module-appboy.html#.initialize
36+
if (
37+
appboyUtil.isMajorVersionTwo(options) ||
38+
appboyUtil.isMajorVersionThree(options)
39+
) {
40+
// https://js.appboycdn.com/web-sdk/3.1/doc/module-appboy.html#.initialize
3341
config = {
3442
safariWebsitePushId: options.safariWebsitePushId,
3543
enableHtmlInAppMessages: options.enableHtmlInAppMessages,

integrations/appboy/lib/index.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ var Appboy = (module.exports = integration('Appboy')
4747
.tag(
4848
'v2.7',
4949
'<script src="https://js.appboycdn.com/web-sdk/2.7/appboy.min.js">'
50+
)
51+
.tag(
52+
'v3.1',
53+
'<script src="https://js.appboycdn.com/web-sdk/3.1/appboy.min.js">'
5054
));
5155

5256
Appboy.prototype.appboyInitialize = function(userId, options, config) {
@@ -60,7 +64,9 @@ Appboy.prototype.appboyInitialize = function(userId, options, config) {
6064
};
6165

6266
Appboy.prototype.initialize = function() {
63-
if (appboyUtil.isMajorVersionTwo(this.options)) {
67+
if (appboyUtil.isMajorVersionThree(this.options)) {
68+
this.initializeV3();
69+
} else if (appboyUtil.isMajorVersionTwo(this.options)) {
6470
this.initializeV2();
6571
} else {
6672
this.initializeV1();
@@ -168,6 +174,31 @@ Appboy.prototype.initializeV2 = function() {
168174
this.load(versionTag, this.ready);
169175
};
170176

177+
Appboy.prototype.initializeV3 = function() {
178+
var options = this.options;
179+
var userId = this.analytics.user().id();
180+
181+
/* eslint-disable */
182+
+function(a,p,P,b,y){
183+
a.appboy={};
184+
a.appboyQueue=[];
185+
for(var s="DeviceProperties Card Card.prototype.dismissCard Card.prototype.removeAllSubscriptions Card.prototype.removeSubscription Card.prototype.subscribeToClickedEvent Card.prototype.subscribeToDismissedEvent Banner CaptionedImage ClassicCard ControlCard ContentCards ContentCards.prototype.getUnviewedCardCount Feed Feed.prototype.getUnreadCardCount ControlMessage InAppMessage InAppMessage.SlideFrom InAppMessage.ClickAction InAppMessage.DismissType InAppMessage.OpenTarget InAppMessage.ImageStyle InAppMessage.Orientation InAppMessage.TextAlignment InAppMessage.CropType InAppMessage.prototype.closeMessage InAppMessage.prototype.removeAllSubscriptions InAppMessage.prototype.removeSubscription InAppMessage.prototype.subscribeToClickedEvent InAppMessage.prototype.subscribeToDismissedEvent FullScreenMessage ModalMessage HtmlMessage SlideUpMessage User User.Genders User.NotificationSubscriptionTypes User.prototype.addAlias User.prototype.addToCustomAttributeArray User.prototype.getUserId User.prototype.incrementCustomUserAttribute User.prototype.removeFromCustomAttributeArray User.prototype.setAvatarImageUrl User.prototype.setCountry User.prototype.setCustomLocationAttribute User.prototype.setCustomUserAttribute User.prototype.setDateOfBirth User.prototype.setEmail User.prototype.setEmailNotificationSubscriptionType User.prototype.setFirstName User.prototype.setGender User.prototype.setHomeCity User.prototype.setLanguage User.prototype.setLastKnownLocation User.prototype.setLastName User.prototype.setPhoneNumber User.prototype.setPushNotificationSubscriptionType InAppMessageButton InAppMessageButton.prototype.removeAllSubscriptions InAppMessageButton.prototype.removeSubscription InAppMessageButton.prototype.subscribeToClickedEvent display display.automaticallyShowNewInAppMessages display.destroyFeed display.hideContentCards display.showContentCards display.showFeed display.showInAppMessage display.toggleContentCards display.toggleFeed changeUser destroy getDeviceId initialize isPushBlocked isPushGranted isPushPermissionGranted isPushSupported logCardClick logCardDismissal logCardImpressions logContentCardsDisplayed logCustomEvent logFeedDisplayed logInAppMessageButtonClick logInAppMessageClick logInAppMessageHtmlClick logInAppMessageImpression logPurchase openSession registerAppboyPushMessages removeAllSubscriptions removeSubscription requestContentCardsRefresh requestFeedRefresh requestImmediateDataFlush resumeWebTracking setLogger stopWebTracking subscribeToContentCardsUpdates subscribeToFeedUpdates subscribeToInAppMessage subscribeToNewInAppMessages toggleAppboyLogging trackLocation unregisterAppboyPushMessages wipeData".split(" "),i=0;i<s.length;i++){for(var m=s[i],k=a.appboy,l=m.split("."),j=0;j<l.length-1;j++)k=k[l[j]];k[l[j]]=(new Function("return function "+m.replace(/\./g,"_")+"(){window.appboyQueue.push(arguments); return true}"))()}
186+
window.appboy.getCachedContentCards=function(){return new window.appboy.ContentCards};
187+
window.appboy.getCachedFeed=function(){return new window.appboy.Feed};
188+
window.appboy.getUser=function(){return new window.appboy.User};
189+
}(window,document,'script');
190+
/* eslint-enable */
191+
192+
if (appboyUtil.shouldOpenSession(userId, options)) {
193+
this.hasBeenInitialized = true;
194+
var config = appboyUtil.getConfig(options);
195+
this.initializeTester(options.apiKey, config);
196+
this.appboyInitialize(userId, options, config);
197+
}
198+
199+
this.load('v3.1', this.ready);
200+
};
201+
171202
/**
172203
* @returns {boolean} true if integration should handle event.
173204
*/

integrations/appboy/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@segment/analytics.js-integration-appboy",
33
"description": "The Appboy analytics.js integration.",
4-
"version": "1.15.1",
4+
"version": "1.16.0",
55
"keywords": [
66
"analytics.js",
77
"analytics.js-integration",

integrations/appboy/test/index.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,24 @@ describe('Appboy', function() {
145145
analytics.initialize();
146146
});
147147

148+
it('should use initializeV3 if version is set to 3.1', function(done) {
149+
var V1spy = sinon.spy(appboy, 'initializeV1');
150+
var V2spy = sinon.spy(appboy, 'initializeV2');
151+
var V3spy = sinon.spy(appboy, 'initializeV3');
152+
appboy.options.version = 3.1;
153+
analytics.once('ready', function() {
154+
try {
155+
assert(V3spy.called);
156+
assert(!V2spy.called);
157+
assert(!V1spy.called);
158+
done();
159+
} catch (e) {
160+
done(e);
161+
}
162+
});
163+
analytics.initialize();
164+
});
165+
148166
it('should set the sdk endpoint to a custom URI if one is provided', function(done) {
149167
appboy.options.customEndpoint = 'https://my.custom.endpoint.com';
150168
var spy = sinon.spy(appboy, 'initializeTester');

0 commit comments

Comments
 (0)