|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Module Dependencies |
| 5 | + */ |
| 6 | + |
| 7 | +var integration = require('@segment/analytics.js-integration'); |
| 8 | +var each = require('@ndhoule/each'); |
| 9 | +var foldl = require('@ndhoule/foldl'); |
| 10 | +var qs = require('component-querystring'); |
| 11 | +var find = require('obj-case').find; |
| 12 | +var toNoCase = require('to-no-case'); |
| 13 | + |
| 14 | +/** |
| 15 | + * Expose `DoubleClick Floodlight` integration. |
| 16 | + */ |
| 17 | + |
| 18 | +var Floodlight = (module.exports = integration('DoubleClick Floodlight') |
| 19 | + .option('source', '') |
| 20 | + .tag( |
| 21 | + 'counter', |
| 22 | + '<iframe src="https://{{ src }}.fls.doubleclick.net/activityi;src={{ src }};type={{ type }};cat={{ cat }};dc_lat=;dc_rdid=;tag_for_child_directed_treatment=;ord={{ ord }}{{ customVariables }}?">' |
| 23 | + ) |
| 24 | + .tag( |
| 25 | + 'sales', |
| 26 | + '<iframe src="https://{{ src }}.fls.doubleclick.net/activityi;src={{ src }};type={{ type }};cat={{ cat }};qty={{ qty }};cost={{ cost }};dc_lat=;dc_rdid=;tag_for_child_directed_treatment=;ord={{ ord }}{{ customVariables }}?">' |
| 27 | + )); |
| 28 | + |
| 29 | +/** |
| 30 | + * Initialize. |
| 31 | + * |
| 32 | + * https://support.google.com/dcm/partner/answer/4293719?hl=en&ref_topic=4241548 |
| 33 | + * @api public |
| 34 | + */ |
| 35 | + |
| 36 | +Floodlight.prototype.initialize = function() { |
| 37 | + this.ready(); |
| 38 | +}; |
| 39 | + |
| 40 | +/** |
| 41 | + * Track |
| 42 | + * Fire Floodlight image tags per Segment event |
| 43 | + * |
| 44 | + * https://support.google.com/dcm/partner/answer/2823425?hl=en&ref_topic=4241548 |
| 45 | + * https://support.google.com/dcm/partner/answer/2823363 |
| 46 | + * @api public |
| 47 | + * @param {Track} |
| 48 | + */ |
| 49 | + |
| 50 | +Floodlight.prototype.track = function(track) { |
| 51 | + var mappedEvents = []; |
| 52 | + if (!this.options.events || !this.options.events.length) return; |
| 53 | + |
| 54 | + // retrieve event mappings that match the current event |
| 55 | + for (var i = 0; i < this.options.events.length; i += 1) { |
| 56 | + var item = this.options.events[i]; |
| 57 | + if (item.value) { |
| 58 | + if (toNoCase(item.key) === toNoCase(track.event())) |
| 59 | + mappedEvents.push(item.value); |
| 60 | + } else if (toNoCase(item.event) === toNoCase(track.event())) { |
| 61 | + mappedEvents.push(item); |
| 62 | + } |
| 63 | + } |
| 64 | + var settings = this.options; |
| 65 | + |
| 66 | + // Must have events mapped and DoubleClick Advertiser ID |
| 67 | + if (!mappedEvents.length || !settings.source) return; |
| 68 | + var properties = track.properties(); |
| 69 | + var self = this; |
| 70 | + |
| 71 | + // Prepare tag params for each mapped Floodlight Activity |
| 72 | + var tags = foldl( |
| 73 | + function(conversions, tag) { |
| 74 | + var type = tag.type || settings.groupTag; |
| 75 | + var event = tag.event; |
| 76 | + var cat = tag.cat || settings.activityTag; |
| 77 | + |
| 78 | + if (!event || !cat || !type) return conversions; |
| 79 | + |
| 80 | + // Find matching properties if any |
| 81 | + var matchedVariables = {}; |
| 82 | + each(function(variable) { |
| 83 | + var floodlightProp = variable.value; |
| 84 | + var segmentProp = variable.key.match(/{{(.*)}}/) || variable.key; |
| 85 | + var segmentPropValue; |
| 86 | + |
| 87 | + if (Array.isArray(segmentProp)) { |
| 88 | + segmentProp = segmentProp.pop(); |
| 89 | + segmentPropValue = find(track.json(), segmentProp); |
| 90 | + } else { |
| 91 | + segmentPropValue = properties[segmentProp]; |
| 92 | + } |
| 93 | + |
| 94 | + if (segmentPropValue) { |
| 95 | + matchedVariables[floodlightProp] = segmentPropValue; |
| 96 | + } |
| 97 | + }, tag.customVariable); |
| 98 | + |
| 99 | + var customVariables = qs.stringify(matchedVariables).replace(/&/g, ';'); |
| 100 | + if (tag.customVariable.length) customVariables = ';' + customVariables; |
| 101 | + |
| 102 | + var tagParams = { |
| 103 | + src: settings.source, |
| 104 | + type: type, |
| 105 | + cat: cat, |
| 106 | + customVariables: customVariables |
| 107 | + }; |
| 108 | + |
| 109 | + // there are two types of tags: counter and sales |
| 110 | + // counter basically are used to increment conversions |
| 111 | + // sales tags are used to collect revenue/cost data |
| 112 | + // ord for counter tags are cachebuster but for sales tag it is whatever you specified in your settings |
| 113 | + // sales tags takes some additional semantic ecommerce params |
| 114 | + if (tag.isSalesTag) { |
| 115 | + tagParams._type = 'sales'; |
| 116 | + // you need to enable order ID reporting for sales tag inside dbl click UI if you want this to work properly |
| 117 | + var quantity = 0; |
| 118 | + if (track.products().length) { |
| 119 | + each(function(product) { |
| 120 | + quantity += product.quantity || 0; |
| 121 | + }, track.products()); |
| 122 | + } else if (properties.quantity) { |
| 123 | + quantity = properties.quantity; |
| 124 | + } |
| 125 | + if (quantity) tagParams.qty = quantity; |
| 126 | + // doubleclick wants revenue under this cost param, yes |
| 127 | + if (track.revenue()) tagParams.cost = track.revenue(); |
| 128 | + tagParams.ord = track.proxy(tag.ordKey); |
| 129 | + } else { |
| 130 | + tagParams.ord = Math.random() * 10000000000000000000; |
| 131 | + } |
| 132 | + |
| 133 | + conversions.push(tagParams); |
| 134 | + |
| 135 | + return conversions; |
| 136 | + }, |
| 137 | + [], |
| 138 | + mappedEvents |
| 139 | + ); |
| 140 | + |
| 141 | + // Fire each tag |
| 142 | + each(function(tagParams) { |
| 143 | + if (tagParams._type === 'sales') return self.load('sales', tagParams); |
| 144 | + return self.load('counter', tagParams); |
| 145 | + }, tags); |
| 146 | +}; |
| 147 | + |
| 148 | +/** |
| 149 | + * Page |
| 150 | + * Fire Floodlight image tags per Segment event |
| 151 | + * |
| 152 | + * https://support.google.com/dcm/partner/answer/2823425?hl=en&ref_topic=4241548 |
| 153 | + * https://support.google.com/dcm/partner/answer/2823363 |
| 154 | + * @api public |
| 155 | + * @param {Page} |
| 156 | + */ |
| 157 | + |
| 158 | +Floodlight.prototype.page = function(page) { |
| 159 | + var name = page.fullName(); |
| 160 | + |
| 161 | + if (name) this.track(page.track(name)); |
| 162 | +}; |
0 commit comments