Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@segment/analytics.js-integration-pinterest-tag

Package Overview
Dependencies
Maintainers
111
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@segment/analytics.js-integration-pinterest-tag - npm Package Compare versions

Comparing version 1.0.2 to 1.1.0

5

HISTORY.md

@@ -0,1 +1,6 @@

1.1.0 / 2019-03-20
==================
* Enables Pinterest Enhanced Match feature by sending an email address to the pinterest load function
1.0.2 / 2017-12-04

@@ -2,0 +7,0 @@ ==================

166

lib/index.js

@@ -1,2 +0,2 @@

'use strict'
'use strict';

@@ -7,4 +7,4 @@ /**

var integration = require('@segment/analytics.js-integration')
var analyticsEvents = require('analytics-events')
var integration = require('@segment/analytics.js-integration');
var analyticsEvents = require('analytics-events');

@@ -15,3 +15,3 @@ /**

var Pinterest = module.exports = integration('Pinterest Tag')
var Pinterest = (module.exports = integration('Pinterest Tag')
.global('pintrk')

@@ -21,7 +21,7 @@ .option('tid', '')

.mapping('pinterestEventMapping')
.tag('<script src="https://s.pinimg.com/ct/core.js"></script>')
.tag('<script src="https://s.pinimg.com/ct/core.js"></script>'));
Pinterest.prototype.initialize = function () {
Pinterest.prototype.initialize = function() {
// We require a Tag ID to run this integration.
if (!this.options.tid) return
if (!this.options.tid) return;

@@ -31,40 +31,52 @@ // Preparation for loading the Pinterest script.

this.load(this.ready)
pintrk('load', this.options.tid)
pintrk('page') // This is treated semantically different than our own page implementation.
this.load(this.ready);
window.pintrk('load', this.options.tid);
window.pintrk('page'); // This is treated semantically different than our own page implementation.
this.createPropertyMapping()
}
this.createPropertyMapping();
};
Pinterest.prototype.loaded = function () {
return !!(window.pintrk && window.pintrk.queue && window.pintrk.queue.push !== Array.prototype.push)
}
Pinterest.prototype.loaded = function() {
return !!(window.pintrk && window.pintrk.queue);
};
Pinterest.prototype.page = function (page) {
Pinterest.prototype.identify = function(identify) {
// If we have an email then enable Enhanced Match feature by reloading the Pinterest library.
// TODO: We may want to add a toggle in the Pinterest integration UI as a configuration to enable enhanced match
if (identify.email()) {
window.pintrk('load', this.options.tid, {
em: identify.email()
});
}
};
Pinterest.prototype.page = function(page) {
// If we have a category, the use ViewCategory. Otherwise, use a normal PageVisit.
if (page.category()) {
pintrk('track', 'ViewCategory', {
window.pintrk('track', 'ViewCategory', {
category: page.category(),
name: page.name() || ''
})
});
} else {
pintrk('track', 'PageVisit', {
window.pintrk('track', 'PageVisit', {
name: page.name() || ''
})
});
}
}
};
Pinterest.prototype.track = function (track) {
Pinterest.prototype.track = function(track) {
// Send a Pinterest Event if mapped. Otherwise, send the call as-is.
var segmentEvent = track.event()
var pinterestEvent = this.getPinterestEvent(segmentEvent)
var pinterestObject = this.generatePropertiesObject(track)
var segmentEvent = track.event();
var pinterestEvent = this.getPinterestEvent(segmentEvent);
var pinterestObject = this.generatePropertiesObject(track);
pinterestEvent ? pintrk('track', pinterestEvent, pinterestObject) : pintrk('track', segmentEvent, pinterestObject)
}
pinterestEvent
? window.pintrk('track', pinterestEvent, pinterestObject)
: window.pintrk('track', segmentEvent, pinterestObject);
};
Pinterest.prototype.getPinterestEvent = function (segmentEvent) {
Pinterest.prototype.getPinterestEvent = function(segmentEvent) {
for (var mappedEvent in this.options.pinterestEventMapping) {
if (mappedEvent.toLowerCase() === segmentEvent.toLowerCase()) {
return this.options.pinterestEventMapping[mappedEvent]
return this.options.pinterestEventMapping[mappedEvent];
}

@@ -80,13 +92,14 @@ }

[analyticsEvents.videoPlaybackStarted, 'WatchVideo']
]
];
for (var index in eventMap) {
var eventRegex = eventMap[index][0]
var pinterestEvent = eventMap[index][1]
if (!eventMap.hasOwnProperty(index)) continue;
var eventRegex = eventMap[index][0];
var pinterestEvent = eventMap[index][1];
if (eventRegex.test(segmentEvent)) {
return pinterestEvent
return pinterestEvent;
}
}
}
};

@@ -97,11 +110,11 @@ /**

Pinterest.prototype.createPropertyMapping = function () {
Pinterest.prototype.createPropertyMapping = function() {
this.propertyMap = {
// Segment Property: Pinterest Property
'query': 'search_query',
'order_id': 'order_id',
'coupon': 'coupon',
'value': 'value',
'currency': 'currency'
}
query: 'search_query',
order_id: 'order_id',
coupon: 'coupon',
value: 'value',
currency: 'currency'
};

@@ -111,12 +124,12 @@ // This is a second map to allow us to loop over specific potentially-nested properties.

// Segment Property: Pinterest Property
'name': 'product_name',
'product_id': 'product_id',
'sku': 'product_id',
'category': 'product_category',
'variant': 'product_variant',
'price': 'product_price',
'quantity': 'product_quantity',
'brand': 'product_brand'
}
}
name: 'product_name',
product_id: 'product_id',
sku: 'product_id',
category: 'product_category',
variant: 'product_variant',
price: 'product_price',
quantity: 'product_quantity',
brand: 'product_brand'
};
};

@@ -127,50 +140,53 @@ /**

Pinterest.prototype.generatePropertiesObject = function (track) {
Pinterest.prototype.generatePropertiesObject = function(track) {
// Generate the properties object to send with the call.
var pinterestProps = {}
var trackValue
var pinterestProps = {};
var trackValue;
for (var prop in this.propertyMap) {
trackValue = track.proxy('properties.' + prop)
if (trackValue) pinterestProps[this.propertyMap[prop]] = trackValue
if (!this.propertyMap.hasOwnProperty(prop)) continue;
trackValue = track.proxy('properties.' + prop);
if (trackValue) pinterestProps[this.propertyMap[prop]] = trackValue;
}
// Determine if there's a 'products' Array, then add in the specific features on that decision.
var products = track.proxy('properties.products')
var lineItemsArray
var products = track.proxy('properties.products');
var lineItemsArray;
if (Array.isArray(products)) {
lineItemsArray = []
lineItemsArray = [];
for (var i = 0; i < products.length; i++) {
for (prop in this.productPropertyMap) {
trackValue = products[i][prop]
if (!this.productPropertyMap.hasOwnProperty(prop)) continue;
trackValue = products[i][prop];
if (trackValue) {
// Product values are added into a `line_items` array, with a nested object. If that doesn't exist, make it first.
if (lineItemsArray[i] === undefined) lineItemsArray[i] = {}
lineItemsArray[i][this.productPropertyMap[prop]] = trackValue
if (lineItemsArray[i] === undefined) lineItemsArray[i] = {};
lineItemsArray[i][this.productPropertyMap[prop]] = trackValue;
}
}
}
if (lineItemsArray.length) pinterestProps['line_items'] = lineItemsArray
if (lineItemsArray.length) pinterestProps.line_items = lineItemsArray;
} else {
// There will only be a single layer, since we have, at most, one product.
lineItemsArray = [{}]
var propAdded = false
lineItemsArray = [{}];
var propAdded = false;
for (prop in this.productPropertyMap) {
trackValue = track.proxy('properties.' + prop)
if (!this.productPropertyMap.hasOwnProperty(prop)) continue;
trackValue = track.proxy('properties.' + prop);
if (trackValue) {
lineItemsArray[0][this.productPropertyMap[prop]] = trackValue
propAdded = true
lineItemsArray[0][this.productPropertyMap[prop]] = trackValue;
propAdded = true;
}
}
if (propAdded) pinterestProps['line_items'] = lineItemsArray
if (propAdded) pinterestProps.line_items = lineItemsArray;
}
// Finally, add in any custom properties defined by the user.
var customProps = this.options.pinterestCustomProperties
var customProps = this.options.pinterestCustomProperties;
for (var j = 0; j < customProps.length; j++) {
prop = customProps[j]
trackValue = track.proxy('properties.' + prop)
if (trackValue) pinterestProps[prop] = trackValue
prop = customProps[j];
trackValue = track.proxy('properties.' + prop);
if (trackValue) pinterestProps[prop] = trackValue;
}
return pinterestProps
}
return pinterestProps;
};
{
"name": "@segment/analytics.js-integration-pinterest-tag",
"description": "The Pinterest Tag analytics.js integration.",
"version": "1.0.2",
"version": "1.1.0",
"keywords": [

@@ -15,15 +15,15 @@ "analytics.js",

},
"author": "Segment \u003cfriends@segment.com\u003e",
"license": "SEE LICENSE IN LICENSE",
"homepage": "https://github.com/segmentio/analytics.js-integrations/blob/master/integrations/pinterest-tag#readme",
"bugs": {
"url": "https://github.com/segmentio/analytics.js-integrations/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/segment-integrations/analytics.js-integration-pinterest-tag.git"
"url": "git+https://github.com/segmentio/analytics.js-integrations.git"
},
"author": "Segment <friends@segment.com>",
"license": "SEE LICENSE IN LICENSE",
"bugs": {
"url": "https://github.com/segment-integrations/analytics.js-integration-pinterest-tag/issues"
},
"homepage": "https://github.com/segment-integrations/analytics.js-integration-pinterest-tag#readme",
"dependencies": {
"@segment/analytics.js-integration": "^2.1.0",
"analytics-events": "^2.1.0",
"@segment/analytics.js-integration": "^2.1.0",
"global-queue": "^1.0.1"

@@ -57,11 +57,3 @@ },

"watchify": "^3.7.0"
},
"standard": {
"env": [
"mocha"
],
"globals": [
"pintrk"
]
}
}

@@ -11,3 +11,3 @@ # analytics.js-integration-pinterest-tag [![Build Status][ci-badge]][ci-link]

[Analytics.js]: https://segment.com/docs/libraries/analytics.js/
[ci-link]: https://circleci.com/gh/segment-integrations/analytics.js-integration-pinterest-tag
[ci-badge]: https://circleci.com/gh/segment-integrations/analytics.js-integration-pinterest-tag.svg?style=svg
[ci-link]: https://ci.segment.com/gh/segment-integrations/analytics.js-integration-pinterest-tag
[ci-badge]: https://ci.segment.com/gh/segment-integrations/analytics.js-integration-pinterest-tag.svg?style=svg

@@ -1,12 +0,12 @@

'use strict'
'use strict';
var Analytics = require('@segment/analytics.js-core').constructor
var integration = require('@segment/analytics.js-integration')
var tester = require('@segment/analytics.js-integration-tester')
var sandbox = require('@segment/clear-env')
var Pinterest = require('../lib/')
var Analytics = require('@segment/analytics.js-core').constructor;
var integration = require('@segment/analytics.js-integration');
var tester = require('@segment/analytics.js-integration-tester');
var sandbox = require('@segment/clear-env');
var Pinterest = require('../lib/');
describe('Pinterest', function () {
var analytics
var pinterest
describe('Pinterest', function() {
var analytics;
var pinterest;
var options = {

@@ -19,49 +19,75 @@ tid: '2620795819800',

},
pinterestCustomProperties: [
'custom_prop'
]
}
pinterestCustomProperties: ['custom_prop']
};
beforeEach(function () {
analytics = new Analytics()
pinterest = new Pinterest(options)
analytics.use(Pinterest)
analytics.use(tester)
analytics.add(pinterest)
})
beforeEach(function() {
analytics = new Analytics();
pinterest = new Pinterest(options);
analytics.use(Pinterest);
analytics.use(tester);
analytics.add(pinterest);
});
afterEach(function () {
analytics.restore()
analytics.reset()
pinterest.reset()
sandbox()
})
afterEach(function() {
analytics.restore();
analytics.reset();
pinterest.reset();
sandbox();
});
it('should have the right settings', function () {
analytics.compare(Pinterest, integration('Pinterest Tag')
.global('pintrk')
.mapping('pinterestEventMapping')
.option('pinterestCustomProperties', [])
.option('tid', ''))
})
it('should have the right settings', function() {
analytics.compare(
Pinterest,
integration('Pinterest Tag')
.global('pintrk')
.mapping('pinterestEventMapping')
.option('pinterestCustomProperties', [])
.option('tid', '')
);
});
describe('before loading', function () {
beforeEach(function () {
analytics.stub(pinterest, 'load')
})
describe('before loading', function() {
beforeEach(function() {
analytics.stub(pinterest, 'load');
});
describe('#initialize', function () {
it('should call #load', function () {
analytics.initialize()
analytics.page()
analytics.called(pinterest.load)
})
})
})
describe('#initialize', function() {
it('should call #load', function() {
analytics.initialize();
analytics.page();
analytics.called(pinterest.load);
});
});
});
describe('loading', function () {
it('should load', function (done) {
analytics.load(pinterest, done)
})
})
})
describe('loading', function() {
it('should load', function(done) {
analytics.load(pinterest, done);
});
});
describe('after loading', function() {
beforeEach(function(done) {
analytics.once('ready', done);
analytics.initialize();
analytics.stub(pinterest, 'load');
});
describe('#identify', function() {
beforeEach(function() {
analytics.spy(window, 'pintrk');
});
it('should not fire the Pinterest pixel tag', function() {
analytics.identify();
analytics.didNotCall(window.pintrk);
});
it('should push Segment email to Pinterest Enhanced Match', function() {
analytics.identify('123', { email: 'prakash@segment.com' });
analytics.called(window.pintrk, 'load', '2620795819800', {
em: 'prakash@segment.com'
});
});
});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc