Skip to content

Commit fba3c43

Browse files
chrisradekbartes
andauthored
upgrade castle integration (#671) (#700)
* merge * added user address * update history Co-authored-by: Bartosz <bartesrlz@gmail.com>
1 parent 495483d commit fba3c43

File tree

6 files changed

+452
-181
lines changed

6 files changed

+452
-181
lines changed

integrations/castle/HISTORY.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2.0.0 / 2022-09-06
2+
==================
3+
4+
* integration with 2.x version of the castle script
5+
16
1.0.4 / 2017-11-13
27
==================
38

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
'use strict';
2+
13
module.exports = require('../../karma.conf-ci.js');

integrations/castle/karma.conf.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
'use strict';
2+
13
module.exports = require('../../karma.conf');

integrations/castle/lib/index.js

Lines changed: 157 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,44 @@
55
*/
66

77
var integration = require('@segment/analytics.js-integration');
8+
var objCase = require('obj-case');
9+
var newDate = require('new-date');
10+
var isoDate = require('@segment/isodate');
11+
var toIsoString = require('@segment/to-iso-string');
12+
var isObject = require('isobject');
813

914
/**
1015
* Expose `Castle` integration.
1116
*/
1217

13-
var Castle = (module.exports = integration('Castle')
18+
var CastleIntegration = (module.exports = integration('Castle')
19+
.global('CastleSegment')
20+
.global('casData')
1421
.option('publishableKey', '')
15-
.option('autoPageview', false)
16-
.option('cookieDomain', false)
17-
.tag('<script src="//d2t77mnxyo7adj.cloudfront.net/v1/cs.js">'));
22+
.option('cookieDomain', '')
23+
.tag(
24+
'<script src="https://d355prp56x5ntt.cloudfront.net/v3/castle.segment.js">'
25+
));
1826

1927
/**
2028
* Initialize.
2129
*
2230
* @api public
2331
*/
24-
25-
Castle.prototype.initialize = function() {
26-
window._castle = window._castle || {};
27-
window._castle.q = window._castle.q || [];
28-
window._castle.q.push(['setKey', this.options.publishableKey]);
29-
30-
if (this.options.cookieDomain) {
31-
window._castle.q.push(['setCookieDomain', this.options.cookieDomain]);
32-
}
33-
34-
if (this.options.autoPageview === false) {
35-
window._castle.q.push(['autoTrack', this.options.autoPageview]);
36-
}
37-
38-
this._identifyFromCache();
39-
40-
this.load(this.ready);
32+
CastleIntegration.prototype.initialize = function() {
33+
var options = this.options;
34+
var self = this;
35+
// reset casData always
36+
window.casData = {};
37+
38+
this.load(function() {
39+
window.CastleSegment.configure({
40+
pk: options.publishableKey,
41+
cookieDomain: options.cookieDomain,
42+
verbose: false
43+
});
44+
self.ready();
45+
});
4146
};
4247

4348
/**
@@ -47,57 +52,164 @@ Castle.prototype.initialize = function() {
4752
* @return {boolean}
4853
*/
4954

50-
Castle.prototype.loaded = function() {
51-
return typeof window._castle === 'function';
55+
CastleIntegration.prototype.loaded = function() {
56+
return typeof window.CastleSegment !== 'undefined';
5257
};
5358

5459
/**
5560
* Identify
5661
*
5762
* @api public
5863
*/
59-
60-
Castle.prototype.identify = function(identify) {
61-
var traits = identify.traits();
64+
/* */
65+
CastleIntegration.prototype.identify = function(identify) {
6266
var castleOptions = identify.options(this.name);
63-
if (castleOptions && castleOptions.secure) {
64-
window._castle('secure', castleOptions.secure);
67+
68+
if (castleOptions && objCase.find(castleOptions, 'userJwt')) {
69+
window.casData.jwt = objCase.find(castleOptions, 'userJwt');
70+
} else {
71+
objCase.del(window.casData, 'jwt');
6572
}
66-
delete traits.id;
67-
window._castle('identify', identify.userId(), traits);
6873
};
6974

7075
/**
71-
* Track
76+
* reset
7277
*
7378
* @api public
7479
*/
80+
/* */
81+
CastleIntegration.prototype.reset = function() {
82+
if (window.casData) {
83+
objCase.del(window.casData, 'jwt');
84+
}
85+
};
86+
87+
var convertDateToIso = function(date) {
88+
if (isoDate.is(date)) {
89+
return date;
90+
}
91+
var convertedDate = toIsoString(newDate(date));
92+
if (isoDate.is(convertedDate)) {
93+
return convertedDate;
94+
}
95+
96+
return undefined;
97+
};
98+
99+
var generateUserObj = function(user) {
100+
var userObj = {};
101+
102+
if (!user || !user.id()) {
103+
return undefined;
104+
}
105+
106+
var traits = user.traits();
107+
userObj.id = user.id();
108+
109+
var userAddress = objCase.find(traits, 'address');
110+
if (userAddress && isObject(userAddress)) {
111+
var countryCode =
112+
objCase.find(userAddress, 'country_code') ||
113+
objCase.find(userAddress, 'country');
114+
115+
if (countryCode && countryCode.length < 3) {
116+
userObj.address = {};
117+
userObj.address.country_code = countryCode;
118+
userObj.address.city = objCase.find(userAddress, 'city');
119+
userObj.address.region_code =
120+
objCase.find(userAddress, 'regionCode') ||
121+
objCase.find(userAddress, 'state');
122+
userObj.address.postal_code = objCase.find(userAddress, 'postalCode');
123+
userObj.address.line1 =
124+
objCase.find(userAddress, 'line1') ||
125+
objCase.find(userAddress, 'street');
126+
userObj.address.line2 = objCase.find(userAddress, 'line2');
127+
objCase.del(traits, 'address');
128+
}
129+
}
75130

76-
Castle.prototype.page = function(page) {
77-
if (this.options.autoPageview) return;
78-
window._castle('page', page.url(), page.title());
131+
if (objCase.find(traits, 'city')) {
132+
userObj.email = objCase.find(traits, 'email');
133+
objCase.del(traits, 'email');
134+
}
135+
136+
if (objCase.find(traits, 'email')) {
137+
userObj.email = objCase.find(traits, 'email');
138+
objCase.del(traits, 'email');
139+
}
140+
141+
if (objCase.find(traits, 'phone')) {
142+
userObj.phone = objCase.find(traits, 'phone');
143+
objCase.del(traits, 'phone');
144+
}
145+
146+
if (objCase.find(traits, 'registeredAt')) {
147+
var convertedRegisteredAt = convertDateToIso(
148+
objCase.find(traits, 'registeredAt')
149+
);
150+
if (convertedRegisteredAt) {
151+
userObj.registered_at = convertedRegisteredAt;
152+
objCase.del(traits, 'registeredAt');
153+
}
154+
}
155+
if (objCase.find(traits, 'createdAt') && !userObj.registered_at) {
156+
var convertedCreatedAt = convertDateToIso(
157+
objCase.find(traits, 'createdAt')
158+
);
159+
if (convertedCreatedAt) {
160+
userObj.registered_at = convertedCreatedAt;
161+
objCase.del(traits, 'createdAt');
162+
}
163+
}
164+
if (objCase.find(traits, 'name')) {
165+
userObj.name = objCase.find(traits, 'name');
166+
objCase.del(traits, 'name');
167+
}
168+
userObj.traits = traits;
169+
170+
return userObj;
171+
};
172+
173+
var userOrJwt = function(jwtData, userData) {
174+
return jwtData ? { userJwt: jwtData } : { user: generateUserObj(userData) };
79175
};
80176

81177
/**
82178
* Page
83179
*
84180
* @api public
181+
* @param {Page} page
85182
*/
86183

87-
Castle.prototype.track = function(track) {
88-
window._castle('track', track.event(), track.properties());
89-
};
184+
CastleIntegration.prototype.page = function(page) {
185+
var data = userOrJwt(window.casData.jwt, this.analytics.user());
186+
if (page.url()) {
187+
data.url = page.url();
188+
}
189+
if (page.name()) {
190+
data.name = page.name();
191+
}
192+
if (!data.name && page.title()) {
193+
data.name = page.title();
194+
}
195+
if (page.referrer()) {
196+
data.referrer = page.referrer();
197+
}
90198

199+
window.CastleSegment.page(data);
200+
};
201+
//
91202
/**
92-
* Send user information to Castle from cached user
203+
* Track
93204
*
94-
* @api private
205+
* @api public
206+
* @param {Track} track
95207
*/
96208

97-
Castle.prototype._identifyFromCache = function() {
98-
// See if there is a cached user, and call identify if so
99-
var user = this.analytics.user();
100-
if (user.id()) {
101-
window._castle.q.push(['identify', user.id(), user.traits()]);
102-
}
209+
CastleIntegration.prototype.track = function(track) {
210+
var data = userOrJwt(window.casData.jwt, this.analytics.user());
211+
data.name = track.event();
212+
data.properties = track.properties();
213+
214+
window.CastleSegment.custom(data);
103215
};

integrations/castle/package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@segment/analytics.js-integration-castle",
33
"description": "The Castle analytics.js integration.",
4-
"version": "1.0.5",
4+
"version": "2.0.0",
55
"keywords": [
66
"analytics.js",
77
"analytics.js-integration",
@@ -24,7 +24,13 @@
2424
"url": "git+https://github.com/segmentio/analytics.js-integrations.git"
2525
},
2626
"dependencies": {
27-
"@segment/analytics.js-integration": "^3.2.0"
27+
"@segment/analytics.js-integration": "^3.2.0",
28+
"@segment/to-iso-string": "^1.0.1",
29+
"@segment/convert-dates": "^1.0.0",
30+
"@segment/isodate": "^1.0.2",
31+
"new-date": "^1.0.0",
32+
"obj-case": "^0.2.0",
33+
"isobject": "^2.1.0"
2834
},
2935
"devDependencies": {
3036
"@segment/analytics.js-core": "^3.0.0",

0 commit comments

Comments
 (0)