|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Module dependencies. |
| 5 | + */ |
| 6 | + |
| 7 | +var Track = require('segmentio-facade').Track; |
| 8 | +var each = require('component-each'); |
| 9 | +var integration = require('@segment/analytics.js-integration'); |
| 10 | +var push = require('global-queue')('_fxm'); |
| 11 | + |
| 12 | +/** |
| 13 | + * Expose `FoxMetrics` integration. |
| 14 | + */ |
| 15 | + |
| 16 | +var FoxMetrics = module.exports = integration('FoxMetrics') |
| 17 | + .assumesPageview() |
| 18 | + .global('_fxm') |
| 19 | + .option('appId', '') |
| 20 | + .tag('<script src="//d35tca7vmefkrc.cloudfront.net/scripts/{{ appId }}.js">'); |
| 21 | + |
| 22 | +/** |
| 23 | + * Initialize. |
| 24 | + * |
| 25 | + * http://foxmetrics.com/documentation/apijavascript |
| 26 | + * |
| 27 | + * @api public |
| 28 | + */ |
| 29 | + |
| 30 | +FoxMetrics.prototype.initialize = function() { |
| 31 | + window._fxm = window._fxm || []; |
| 32 | + this.load(this.ready); |
| 33 | +}; |
| 34 | + |
| 35 | +/** |
| 36 | + * Loaded? |
| 37 | + * |
| 38 | + * @return {Boolean} |
| 39 | + */ |
| 40 | + |
| 41 | +FoxMetrics.prototype.loaded = function() { |
| 42 | + return !!(window._fxm && window._fxm.appId); |
| 43 | +}; |
| 44 | + |
| 45 | +/** |
| 46 | + * Page. |
| 47 | + * |
| 48 | + * @api public |
| 49 | + * @param {Page} page |
| 50 | + */ |
| 51 | + |
| 52 | +FoxMetrics.prototype.page = function(page) { |
| 53 | + var properties = page.properties(); |
| 54 | + var category = page.category(); |
| 55 | + var name = page.name(); |
| 56 | + // store for later |
| 57 | + // TODO: Why? Document me |
| 58 | + this._category = category; |
| 59 | + |
| 60 | + push( |
| 61 | + '_fxm.pages.view', |
| 62 | + properties.title, |
| 63 | + name, |
| 64 | + category, |
| 65 | + properties.url, |
| 66 | + properties.referrer |
| 67 | + ); |
| 68 | +}; |
| 69 | + |
| 70 | +/** |
| 71 | + * Identify. |
| 72 | + * |
| 73 | + * @api public |
| 74 | + * @param {Identify} identify |
| 75 | + */ |
| 76 | + |
| 77 | +FoxMetrics.prototype.identify = function(identify) { |
| 78 | + var id = identify.userId(); |
| 79 | + |
| 80 | + if (!id) return; |
| 81 | + |
| 82 | + push( |
| 83 | + '_fxm.visitor.profile', |
| 84 | + id, |
| 85 | + identify.firstName(), |
| 86 | + identify.lastName(), |
| 87 | + identify.email(), |
| 88 | + identify.address(), |
| 89 | + // social |
| 90 | + // TODO: Why is this `undefined`? Document |
| 91 | + undefined, |
| 92 | + // partners |
| 93 | + // TODO: Why is this `undefined`? Document |
| 94 | + undefined, |
| 95 | + identify.traits() |
| 96 | + ); |
| 97 | +}; |
| 98 | + |
| 99 | +/** |
| 100 | + * Track. |
| 101 | + * |
| 102 | + * @api public |
| 103 | + * @param {Track} track |
| 104 | + */ |
| 105 | + |
| 106 | +FoxMetrics.prototype.track = function(track) { |
| 107 | + var props = track.properties(); |
| 108 | + var category = this._category || props.category; |
| 109 | + push(track.event(), category, props); |
| 110 | +}; |
| 111 | + |
| 112 | +/** |
| 113 | + * Product viewed. |
| 114 | + * |
| 115 | + * @api private |
| 116 | + * @param {Track} track |
| 117 | + */ |
| 118 | + |
| 119 | +FoxMetrics.prototype.productViewed = function(track) { |
| 120 | + ecommerce('productview', track); |
| 121 | +}; |
| 122 | + |
| 123 | +/** |
| 124 | + * Product Removed. |
| 125 | + * |
| 126 | + * @api private |
| 127 | + * @param {Track} track |
| 128 | + */ |
| 129 | + |
| 130 | +FoxMetrics.prototype.productRemoved = function(track) { |
| 131 | + ecommerce('removecartitem', track); |
| 132 | +}; |
| 133 | + |
| 134 | +/** |
| 135 | + * Product Added. |
| 136 | + * |
| 137 | + * @api private |
| 138 | + * @param {Track} track |
| 139 | + */ |
| 140 | + |
| 141 | +FoxMetrics.prototype.productAdded = function(track) { |
| 142 | + ecommerce('cartitem', track); |
| 143 | +}; |
| 144 | + |
| 145 | +/** |
| 146 | + * Order Completed. |
| 147 | + * |
| 148 | + * @api private |
| 149 | + * @param {Track} track |
| 150 | + */ |
| 151 | + |
| 152 | +FoxMetrics.prototype.orderCompleted = function(track) { |
| 153 | + var orderId = track.orderId(); |
| 154 | + |
| 155 | + // transaction |
| 156 | + push( |
| 157 | + '_fxm.ecommerce.order', |
| 158 | + orderId, |
| 159 | + track.subtotal(), |
| 160 | + track.shipping(), |
| 161 | + track.tax(), |
| 162 | + track.city(), |
| 163 | + track.state(), |
| 164 | + track.zip(), |
| 165 | + track.quantity() |
| 166 | + ); |
| 167 | + |
| 168 | + // items |
| 169 | + each(track.products(), function(product) { |
| 170 | + var track = new Track({ properties: product }); |
| 171 | + ecommerce('purchaseitem', track, [ |
| 172 | + track.quantity(), |
| 173 | + track.price(), |
| 174 | + orderId |
| 175 | + ]); |
| 176 | + }); |
| 177 | +}; |
| 178 | + |
| 179 | +/** |
| 180 | + * Track ecommerce `event` with `track` |
| 181 | + * with optional `arr` to append. |
| 182 | + * |
| 183 | + * @api private |
| 184 | + * @param {string} event |
| 185 | + * @param {Track} track |
| 186 | + * @param {Array} arr |
| 187 | + */ |
| 188 | + |
| 189 | +function ecommerce(event, track, arr) { |
| 190 | + push.apply(null, [ |
| 191 | + '_fxm.ecommerce.' + event, |
| 192 | + track.productId() || track.id() || track.sku(), |
| 193 | + track.name(), |
| 194 | + track.category() |
| 195 | + ].concat(arr || [])); |
| 196 | +} |
0 commit comments