-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathindex.js
106 lines (91 loc) · 2.56 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
'use strict';
/**
* Module dependencies.
*/
var Track = require('segmentio-facade').Track;
var each = require('component-each');
var integration = require('@segment/analytics.js-integration');
/**
* Expose `ShareASale`.
*/
var ShareASale = (module.exports = integration('ShareASale')
.option('merchantId', '')
.option('currency', 'USD')
.option('createLeads', false)
.option('useTotalAsAmount', false)
.tag(
'orderCompleted',
'<img src="https://shareasale.com/sale.cfm?amount={{ orderTotal }}&tracking={{ orderId }}&transtype=sale&merchantID={{ merchantId }}{{ repeat }}&skulist={{ skulist }}&quantitylist={{ quantitylist }}&pricelist={{ pricelist }}¤cy={{ currency }}&couponcode={{ couponcode }}">'
)
.tag(
'leadCreated',
'<img src="https://shareasale.com/sale.cfm?amount=0.00&tracking={{ userId }}&transtype=lead&merchantID={{ merchantId }}">'
));
/**
* Track order completed.
*
* @param {Track} track
*/
ShareASale.prototype.orderCompleted = function(track) {
var orderId = track.orderId();
var isRepeat = track.proxy('properties.repeat');
var subtotal = (track.subtotal() || 0).toFixed(2);
var total= (track.total() || 0).toFixed(2)
var orderTotal =
this.options.useTotalAsAmount && track.total()
? total
: subtotal;
var products = track.products();
var currency = track.currency() || this.options.currency;
var coupon = track.coupon() || '';
var skus = [];
var quantities = [];
var prices = [];
if (!orderId) {
this.debug('must pass `orderId`');
return;
}
if (!subtotal) {
this.debug('must pass `subtotal`, `total`, or `revenue`');
return;
}
each(products, function(product) {
var track = new Track({ properties: product });
skus.push(track.sku());
quantities.push(track.quantity());
prices.push(track.price());
});
var repeat = '';
if (typeof isRepeat === 'boolean') {
repeat = isRepeat ? '&newcustomer=0' : '&newcustomer=1';
}
this.load('orderCompleted', {
orderTotal: orderTotal,
orderId: orderId,
skulist: skus.join(','),
quantitylist: quantities.join(','),
pricelist: prices.join(','),
currency: currency,
couponcode: coupon,
repeat: repeat
});
};
/**
* Identify Leads
*
* @param {Facade} identify
*/
ShareASale.prototype.identify = function(identify) {
var userId = identify.userId();
var opts = this.options;
// identify leads created
if (opts.createLeads) {
if (!userId) {
this.debug('must pass `userId`');
return;
}
this.load('leadCreated', {
userId: userId
});
}
};