Skip to content

[DEST-698][CC-4865] Add product properties to Order Completed for Braze #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 92 additions & 13 deletions integrations/appboy/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,19 +407,9 @@ Appboy.prototype.orderCompleted = function(track) {
del(purchaseProperties, 'currency');

// we have to make a separate call to appboy for each product
each(function(product) {
var track = new Track({ properties: product });
var productId = track.productId();
var price = track.price();
var quantity = track.quantity();
window.appboy.logPurchase(
productId,
price,
currencyCode,
quantity,
purchaseProperties
);
}, products);
for (var i = 0; i < products.length; i++) {
logProduct(products[i], currencyCode, purchaseProperties);
}
};

/**
Expand Down Expand Up @@ -449,3 +439,92 @@ function getGender(gender) {
if (otherGenders.indexOf(gender.toLowerCase()) > -1)
return window.appboy.ab.User.Genders.OTHER;
}

/**
* Logs a Purchase containing a product as described in Braze's documentation:
* https://js.appboycdn.com/web-sdk/latest/doc/module-appboy.html#.logPurchase
*
* @param {Object} product Product from the Order Completed call
* @param {String} currencyCode Currency code
* @param {Object} extraProperties Root properties from the track call
*/
function logProduct(product, currencyCode, extraProperties) {
var track = new Track({ properties: product });
var productId = track.productId();
var price = track.price();
var quantity = track.quantity();
var productProperties = track.properties();
var properties = {};

del(productProperties, 'productId');
del(productProperties, 'price');
del(productProperties, 'quantity');

for (var productProperty in productProperties) {
if (!productProperties.hasOwnProperty(productProperty)) {
continue;
}

var value = productProperties[productProperty];
if (isValidProperty(productProperty, value)) {
properties[productProperty] = value;
}
}

for (var property in extraProperties) {
if (!extraProperties.hasOwnProperty(property)) {
continue;
}

var val = extraProperties[property];
if (
!productProperties.hasOwnProperty(property) &&
isValidProperty(property, val)
) {
properties[property] = val;
}
}

window.appboy.logPurchase(
productId,
price,
currencyCode,
quantity,
properties
);
}

/**
* Validates a name and value of a property, following Braze's restrictions:
*
* Names are limited to 255 characters in length, cannot begin with a $, and
* can only contain alphanumeric characters and punctuation. Values can be
* numeric, boolean, Date objects, or strings 255 characters or shorter.
*
* @param {String} name Name of the property.
* @param {*} value Value of the property.
*
* @return {boolean} <code>true</code> if the name and value are valid, <code>false</code> otherwise.
*/
function isValidProperty(name, value) {
if (name.length > 255 || name.startsWith('$')) {
return false;
}

if (typeof value === 'number' || typeof value === 'boolean') {
return true;
}

if (typeof value === 'object' && value instanceof Date) {
return true;
}

if (
(typeof value === 'string' || value instanceof String) &&
value.length <= 255
) {
return true;
}

return false;
}
2 changes: 1 addition & 1 deletion integrations/appboy/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@segment/analytics.js-integration-appboy",
"description": "The Appboy analytics.js integration.",
"version": "1.9.0",
"version": "1.10.0",
"keywords": [
"analytics.js",
"analytics.js-integration",
Expand Down
24 changes: 20 additions & 4 deletions integrations/appboy/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,27 +440,34 @@ describe('Appboy', function() {
});

it('should call logPurchase for each product in a Completed Order event', function() {
var today = new Date();

analytics.track('Order Completed', {
total: 30,
revenue: 25,
shipping: 3,
currency: 'USD',
date: today,
invalidPropertyLength: 'a'.repeat(500),
products: [
{
product_id: '507f1f77bcf86cd799439011',
sku: '45790-32',
name: 'Monopoly: 3rd Edition',
price: 19.23,
quantity: 1,
category: 'Games'
category: 'Games',
$invalidPropertyName: 3,
invalidPropertyValue: ['red', 'blue']
},
{
product_id: '505bd76785ebb509fc183733',
sku: '46493-32',
name: 'Uno Card Game',
price: 3,
quantity: 2,
category: 'Games'
category: 'Games',
size: 6
}
]
});
Expand All @@ -473,7 +480,11 @@ describe('Appboy', function() {
{
total: 30,
revenue: 25,
shipping: 3
shipping: 3,
sku: '45790-32',
name: 'Monopoly: 3rd Edition',
category: 'Games',
date: today
}
);
analytics.called(
Expand All @@ -485,7 +496,12 @@ describe('Appboy', function() {
{
total: 30,
revenue: 25,
shipping: 3
shipping: 3,
sku: '46493-32',
name: 'Uno Card Game',
category: 'Games',
size: 6,
date: today
}
);
});
Expand Down