|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Module dependencies. |
| 5 | + */ |
| 6 | + |
| 7 | +var integration = require('@segment/analytics.js-integration'); |
| 8 | +var when = require('do-when'); |
| 9 | + |
| 10 | +/** |
| 11 | + * Expose `AdWords`. |
| 12 | + */ |
| 13 | + |
| 14 | +var AdWords = module.exports = integration('AdWords') |
| 15 | + .option('conversionId', '') |
| 16 | + .option('pageRemarketing', false) |
| 17 | + .option('eventMappings', []) |
| 18 | + .tag('<script src="//www.googleadservices.com/pagead/conversion_async.js">'); |
| 19 | + |
| 20 | +/** |
| 21 | + * Initialize. |
| 22 | + * |
| 23 | + * @api public |
| 24 | + */ |
| 25 | + |
| 26 | +AdWords.prototype.initialize = function() { |
| 27 | + var loaded = this.loaded; |
| 28 | + var ready = this.ready; |
| 29 | + this.load(function() { |
| 30 | + when(loaded, ready); |
| 31 | + }); |
| 32 | +}; |
| 33 | + |
| 34 | +/** |
| 35 | + * Loaded. |
| 36 | + * |
| 37 | + * @api private |
| 38 | + * @return {boolean} |
| 39 | + */ |
| 40 | + |
| 41 | +AdWords.prototype.loaded = function() { |
| 42 | + return !!(document.body && window.google_trackConversion); |
| 43 | +}; |
| 44 | + |
| 45 | +/** |
| 46 | + * Page. |
| 47 | + * |
| 48 | + * https://support.google.com/adwords/answer/3111920#standard_parameters |
| 49 | + * https://support.google.com/adwords/answer/3103357 |
| 50 | + * https://developers.google.com/adwords-remarketing-tag/asynchronous/ |
| 51 | + * https://developers.google.com/adwords-remarketing-tag/parameters |
| 52 | + * |
| 53 | + * @api public |
| 54 | + * @param {Page} page |
| 55 | + */ |
| 56 | + |
| 57 | +AdWords.prototype.page = function(page) { |
| 58 | + // Remarketing option can support both Adwords' "static" or "dynamic" remarketing tags |
| 59 | + // Difference is static you don't need to send props under `google_custom_params` |
| 60 | + var remarketing = this.options.pageRemarketing; |
| 61 | + var id = this.options.conversionId; |
| 62 | + var props = page.properties(); |
| 63 | + |
| 64 | + // Conversion tag |
| 65 | + window.google_trackConversion({ |
| 66 | + google_conversion_id: id, |
| 67 | + google_custom_params: {}, |
| 68 | + google_remarketing_only: false // this ensures that this is a conversion tag |
| 69 | + }); |
| 70 | + |
| 71 | + // Remarketing tag (must be sent in _addition_ to any conversion tags) |
| 72 | + // https://developers.google.com/adwords-remarketing-tag/ |
| 73 | + if (remarketing) { |
| 74 | + window.google_trackConversion({ |
| 75 | + google_conversion_id: id, |
| 76 | + google_custom_params: props, |
| 77 | + google_remarketing_only: true // this ensures that this is a remarketing tag |
| 78 | + }); |
| 79 | + } |
| 80 | +}; |
| 81 | + |
| 82 | +/** |
| 83 | + * Track. |
| 84 | + * |
| 85 | + * @api public |
| 86 | + * @param {Track} |
| 87 | + */ |
| 88 | + |
| 89 | +AdWords.prototype.track = function(track) { |
| 90 | + var self = this; |
| 91 | + var props = track.properties(); |
| 92 | + var eventMappings = this.options.eventMappings; |
| 93 | + var revenue = track.revenue() || 0; |
| 94 | + |
| 95 | + eventMappings.forEach(function(mapping) { |
| 96 | + if (mapping.value) { |
| 97 | + if (mapping.value.eventName.toLowerCase() !== track.event().toLowerCase()) return; |
| 98 | + var id = mapping.value.conversionId || self.options.conversionId; // customer can either specify one global conversion id or one per mapping |
| 99 | + |
| 100 | + // Fire conversion tag |
| 101 | + if (mapping.value.label !== '') { |
| 102 | + delete props.revenue; |
| 103 | + |
| 104 | + window.google_trackConversion({ |
| 105 | + google_conversion_id: id, |
| 106 | + google_custom_params: props, |
| 107 | + google_conversion_language: 'en', |
| 108 | + google_conversion_format: '3', |
| 109 | + google_conversion_color: 'ffffff', |
| 110 | + google_conversion_label: mapping.value.label, |
| 111 | + google_conversion_value: revenue, |
| 112 | + google_remarketing_only: false // ensure this is a conversion tag |
| 113 | + }); |
| 114 | + } |
| 115 | + |
| 116 | + // Fire remarketing tag |
| 117 | + if (mapping.value.remarketing) { |
| 118 | + window.google_trackConversion({ |
| 119 | + google_conversion_id: id, |
| 120 | + google_custom_params: props, // do not send PII here! |
| 121 | + google_remarketing_only: true // ensure this is a remarketing tag |
| 122 | + }); |
| 123 | + } |
| 124 | + } else { |
| 125 | + if (mapping.eventName.toLowerCase() !== track.event().toLowerCase()) return; |
| 126 | + id = mapping.conversionId || self.options.conversionId; // customer can either specify one global conversion id or one per mapping |
| 127 | + |
| 128 | + // Fire conversion tag |
| 129 | + if (mapping.label !== '') { |
| 130 | + delete props.revenue; |
| 131 | + |
| 132 | + window.google_trackConversion({ |
| 133 | + google_conversion_id: id, |
| 134 | + google_custom_params: props, |
| 135 | + google_conversion_language: 'en', |
| 136 | + google_conversion_format: '3', |
| 137 | + google_conversion_color: 'ffffff', |
| 138 | + google_conversion_label: mapping.label, |
| 139 | + google_conversion_value: revenue, |
| 140 | + google_remarketing_only: false // ensure this is a conversion tag |
| 141 | + }); |
| 142 | + } |
| 143 | + |
| 144 | + // Fire remarketing tag |
| 145 | + if (mapping.remarketing) { |
| 146 | + window.google_trackConversion({ |
| 147 | + google_conversion_id: id, |
| 148 | + google_custom_params: props, // do not send PII here! |
| 149 | + google_remarketing_only: true // ensure this is a remarketing tag |
| 150 | + }); |
| 151 | + } |
| 152 | + } |
| 153 | + }); |
| 154 | +}; |
0 commit comments