Skip to content

Commit 72e9538

Browse files
update klaviyo integration to support exchange ID (#618)
1 parent 310353f commit 72e9538

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

integrations/klaviyo/lib/index.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var Klaviyo = (module.exports = integration('Klaviyo')
2121
.global('_learnq')
2222
.option('apiKey', '')
2323
.option('enforceEmail', false)
24-
.tag('<script src="//a.klaviyo.com/media/js/analytics/analytics.js">'));
24+
.tag('<script src="//static.klaviyo.com/onsite/js/klaviyo.js">'));
2525

2626
/**
2727
* Initialize.
@@ -58,23 +58,31 @@ Klaviyo.prototype.loaded = function() {
5858
*/
5959

6060
Klaviyo.prototype.identify = function(identify) {
61-
// if you don't send either userId or email, noop
62-
if (!identify.userId() && !identify.email()) return;
6361
// TODO: should map/alias the rest of the reserved props
6462
var traitAliases = {
6563
email: '$email',
6664
id: '$id',
6765
firstName: '$first_name',
6866
lastName: '$last_name',
6967
phone: '$phone_number',
70-
title: '$title'
68+
title: '$title',
69+
exchangeId: '$exchange_id'
7170
};
72-
// don't add $id or id if email only option is enforced to prevent some edge case dupe profile issues with Klaviyo API
71+
7372
var traits = identify.traits(traitAliases);
73+
74+
// If there is not a user ID, email address, or an exchange ID, noop
75+
//
76+
// NOTE: An exchange ID is an encrypted identifier that Klaviyo
77+
// consumes server-side. It is considered a valid identifier.
78+
if (!identify.userId() && !identify.email() && !traits.$exchange_id) return;
79+
80+
// don't add $id or id if email only option is enforced to prevent some edge case dupe profile issues with Klaviyo API
7481
if (this.options.enforceEmail) {
7582
remove(traits, 'id');
7683
remove(traits, '$id');
7784
}
85+
7886
// if you enforce email but you don't send email, noop
7987
if (this.options.enforceEmail && !traits.$email) return;
8088
push('identify', traits);

integrations/klaviyo/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-klaviyo",
33
"description": "The Klaviyo analytics.js integration.",
4-
"version": "2.2.3",
4+
"version": "2.2.4",
55
"keywords": [
66
"analytics.js",
77
"analytics.js-integration",

integrations/klaviyo/test/index.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,17 @@ describe('Klaviyo', function() {
114114
]);
115115
});
116116

117+
it('should send an $exchange_id and traits', function() {
118+
analytics.identify(undefined, {
119+
$exchange_id: 'exchange-id',
120+
foo: true
121+
});
122+
analytics.called(window._learnq.push, [
123+
'identify',
124+
{ $exchange_id: 'exchange-id', foo: true }
125+
]);
126+
});
127+
117128
it('should alias traits', function() {
118129
analytics.identify('id', {
119130
email: 'name@example.com',

0 commit comments

Comments
 (0)