|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/* global JSON */ |
| 4 | +/* eslint no-restricted-globals: [0] */ |
| 5 | + |
| 6 | +/** |
| 7 | + * Module dependencies. |
| 8 | + */ |
| 9 | + |
| 10 | +var integration = require('@segment/analytics.js-integration'); |
| 11 | +var each = require('component-each'); |
| 12 | +var is = require('is'); |
| 13 | +var extend = require('@ndhoule/extend'); |
| 14 | +var toISOString = require('@segment/to-iso-string'); |
| 15 | +var toString = Object.prototype.toString; // in case this method has been overridden by the user |
| 16 | + |
| 17 | +/** |
| 18 | + * Expose `Heap` integration. |
| 19 | + */ |
| 20 | + |
| 21 | +var Heap = (module.exports = integration('Heap') |
| 22 | + .global('heap') |
| 23 | + .option('appId', '') |
| 24 | + .tag('<script src="//cdn.heapanalytics.com/js/heap-{{ appId }}.js">')); |
| 25 | + |
| 26 | +/** |
| 27 | + * Initialize. |
| 28 | + * |
| 29 | + * https://heapanalytics.com/docs/installation#web |
| 30 | + * |
| 31 | + * @api public |
| 32 | + */ |
| 33 | + |
| 34 | +Heap.prototype.initialize = function() { |
| 35 | + window.heap = window.heap || []; |
| 36 | + window.heap.load = function(appid, config) { |
| 37 | + window.heap.appid = appid; |
| 38 | + window.heap.config = config; |
| 39 | + |
| 40 | + var methodFactory = function(type) { |
| 41 | + return function() { |
| 42 | + window.heap.push( |
| 43 | + [type].concat(Array.prototype.slice.call(arguments, 0)) |
| 44 | + ); |
| 45 | + }; |
| 46 | + }; |
| 47 | + |
| 48 | + var heapMethods = [ |
| 49 | + 'addEventProperties', |
| 50 | + 'addUserProperties', |
| 51 | + 'clearEventProperties', |
| 52 | + 'identify', |
| 53 | + 'removeEventProperty', |
| 54 | + 'setEventProperties', |
| 55 | + 'track', |
| 56 | + 'unsetEventProperty', |
| 57 | + 'resetIdentity' |
| 58 | + ]; |
| 59 | + each(heapMethods, function(method) { |
| 60 | + window.heap[method] = methodFactory(method); |
| 61 | + }); |
| 62 | + }; |
| 63 | + |
| 64 | + window.heap.load(this.options.appId); |
| 65 | + this.load(this.ready); |
| 66 | +}; |
| 67 | + |
| 68 | +/** |
| 69 | + * Loaded? |
| 70 | + * |
| 71 | + * @api private |
| 72 | + * @return {boolean} |
| 73 | + */ |
| 74 | + |
| 75 | +Heap.prototype.loaded = function() { |
| 76 | + return !!(window.heap && window.heap.appid); |
| 77 | +}; |
| 78 | + |
| 79 | +/** |
| 80 | + * Identify. |
| 81 | + * |
| 82 | + * https://heapanalytics.com/docs#identify |
| 83 | + * |
| 84 | + * @api public |
| 85 | + * @param {Identify} identify |
| 86 | + */ |
| 87 | + |
| 88 | +Heap.prototype.identify = function(identify) { |
| 89 | + var traits = identify.traits({ email: '_email' }); |
| 90 | + var id = identify.userId(); |
| 91 | + if (id) window.heap.identify(id); |
| 92 | + window.heap.addUserProperties(clean(traits)); |
| 93 | +}; |
| 94 | + |
| 95 | +/** |
| 96 | + * Track. |
| 97 | + * |
| 98 | + * https://heapanalytics.com/docs#track |
| 99 | + * |
| 100 | + * @api public |
| 101 | + * @param {Track} track |
| 102 | + */ |
| 103 | + |
| 104 | +Heap.prototype.track = function(track) { |
| 105 | + window.heap.track(track.event(), clean(track.properties())); |
| 106 | +}; |
| 107 | + |
| 108 | +/** |
| 109 | + * Clean all nested objects and arrays. |
| 110 | + * |
| 111 | + * @param {Object} obj |
| 112 | + * @return {Object} |
| 113 | + * @api private |
| 114 | + */ |
| 115 | + |
| 116 | +function clean(obj) { |
| 117 | + var ret = {}; |
| 118 | + |
| 119 | + for (var k in obj) { |
| 120 | + if (obj.hasOwnProperty(k)) { |
| 121 | + var value = obj[k]; |
| 122 | + // Heap's natively library will drop null and undefined properties anyway |
| 123 | + // so no need to send these |
| 124 | + // also prevents uncaught errors since we call .toString() on non objects |
| 125 | + if (value === null || value === undefined) continue; |
| 126 | + |
| 127 | + // date |
| 128 | + if (is.date(value)) { |
| 129 | + ret[k] = toISOString(value); |
| 130 | + continue; |
| 131 | + } |
| 132 | + |
| 133 | + // leave boolean as is |
| 134 | + if (is.bool(value)) { |
| 135 | + ret[k] = value; |
| 136 | + continue; |
| 137 | + } |
| 138 | + |
| 139 | + // leave numbers as is |
| 140 | + if (is.number(value)) { |
| 141 | + ret[k] = value; |
| 142 | + continue; |
| 143 | + } |
| 144 | + |
| 145 | + // arrays of objects (eg. `products` array) |
| 146 | + if (toString.call(value) === '[object Array]') { |
| 147 | + ret = extend(ret, trample(k, value)); |
| 148 | + continue; |
| 149 | + } |
| 150 | + |
| 151 | + // non objects |
| 152 | + if (toString.call(value) !== '[object Object]') { |
| 153 | + ret[k] = value.toString(); |
| 154 | + continue; |
| 155 | + } |
| 156 | + |
| 157 | + ret = extend(ret, trample(k, value)); |
| 158 | + } |
| 159 | + } |
| 160 | + // json |
| 161 | + // must flatten including the name of the original trait/property |
| 162 | + function trample(key, value) { |
| 163 | + var nestedObj = {}; |
| 164 | + nestedObj[key] = value; |
| 165 | + var flattenedObj = flatten(nestedObj, { safe: true }); |
| 166 | + |
| 167 | + // stringify arrays inside nested object to be consistent with top level behavior of arrays |
| 168 | + for (var k in flattenedObj) { |
| 169 | + if (is.array(flattenedObj[k])) |
| 170 | + flattenedObj[k] = JSON.stringify(flattenedObj[k]); |
| 171 | + } |
| 172 | + |
| 173 | + return flattenedObj; |
| 174 | + } |
| 175 | + |
| 176 | + return ret; |
| 177 | +} |
| 178 | + |
| 179 | +/** |
| 180 | + * Flatten nested objects |
| 181 | + * taken from https://www.npmjs.com/package/flat |
| 182 | + * @param {Object} obj |
| 183 | + * @return {Object} obj |
| 184 | + * @api public |
| 185 | + */ |
| 186 | + |
| 187 | +function flatten(target, opts) { |
| 188 | + var options = opts || {}; |
| 189 | + |
| 190 | + var delimiter = options.delimiter || '.'; |
| 191 | + var maxDepth = options.maxDepth; |
| 192 | + var currentDepth = 1; |
| 193 | + var output = {}; |
| 194 | + |
| 195 | + function step(object, prev) { |
| 196 | + Object.keys(object).forEach(function(key) { |
| 197 | + var value = object[key]; |
| 198 | + var isarray = options.safe && Array.isArray(value); |
| 199 | + var type = Object.prototype.toString.call(value); |
| 200 | + var isobject = type === '[object Object]' || type === '[object Array]'; |
| 201 | + |
| 202 | + var newKey = prev ? prev + delimiter + key : key; |
| 203 | + |
| 204 | + if (!options.maxDepth) { |
| 205 | + maxDepth = currentDepth + 1; |
| 206 | + } |
| 207 | + |
| 208 | + if ( |
| 209 | + !isarray && |
| 210 | + isobject && |
| 211 | + Object.keys(value).length && |
| 212 | + currentDepth < maxDepth |
| 213 | + ) { |
| 214 | + ++currentDepth; |
| 215 | + return step(value, newKey); |
| 216 | + } |
| 217 | + |
| 218 | + output[newKey] = value; |
| 219 | + }); |
| 220 | + } |
| 221 | + |
| 222 | + step(target); |
| 223 | + |
| 224 | + return output; |
| 225 | +} |
| 226 | + |
| 227 | +/** |
| 228 | + * Polyfill Object.keys |
| 229 | + * // From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys |
| 230 | + * Note: Had to do this because for some reason, the above will not work properly without using Object.keys |
| 231 | + */ |
| 232 | + |
| 233 | +if (!Object.keys) { |
| 234 | + Object.keys = function(o) { |
| 235 | + if (o !== Object(o)) { |
| 236 | + throw new TypeError('Object.keys called on a non-object'); |
| 237 | + } |
| 238 | + var k = []; |
| 239 | + var p; |
| 240 | + for (p in o) if (Object.prototype.hasOwnProperty.call(o, p)) k.push(p); |
| 241 | + return k; |
| 242 | + }; |
| 243 | +} |
0 commit comments