-
Notifications
You must be signed in to change notification settings - Fork 23
/
luna-scroll-tracking.json
252 lines (252 loc) · 24.1 KB
/
luna-scroll-tracking.json
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
{
"exportFormatVersion": 2,
"exportTime": "2018-08-09 22:08:17",
"containerVersion": {
"path": "accounts/263956808/containers/2187445/versions/0",
"accountId": "263956808",
"containerId": "2187445",
"containerVersionId": "0",
"container": {
"path": "accounts/263956808/containers/2187445",
"accountId": "263956808",
"containerId": "2187445",
"name": "Scroll Tracking",
"publicId": "GTM-NRXSRH",
"usageContext": [
"WEB"
],
"fingerprint": "1529436438613",
"tagManagerUrl": "https://tagmanager.google.com/#/container/accounts/263956808/containers/2187445/workspaces?apiLink=container"
},
"tag": [
{
"accountId": "263956808",
"containerId": "2187445",
"tagId": "8",
"name": "CU - Scroll Tracking - LunaMetrics Plugin",
"type": "html",
"parameter": [
{
"type": "TEMPLATE",
"key": "html",
"value": "<script type=\"text/javascript\" id=\"gtm-scroll-tracking\">\n/**\n * Emits events based on scrolling behavior in a given context. Shouldn't\n * be called until after DOMReady.\n *\n * @example\n * var scrollTracker = ScrollTracker({\n * context: '#content'\n * });\n *\n * scrollTracker.on({\n * percentage: {\n * every: [25]\n * }\n * }, function(evt) {\n *\n * // Will trigger when the user reaches 25, 50, 75, & 100% depth\n * notifySomeService(evt.data.scrollDepth);\n *\n * });\n *\n * Copyright(c) 2017 LunaMetrics, LLC.\n * Written by @notdanwilkerson\n * Licensed under the MIT License\n * For full license text, visit https://opensource.org/licenses/MIT\n */\n(function(window) {\n\n 'use strict';\n // Won't work on IE8, so we install a mock.\n if (window.navigator.userAgent.match(/MSIE [678]/gi)) return installMock();\n\n var document = window.document;\n\n /**\n * @constructor\n *\n * @param {object} [opts] options for the constructor\n * @param {HTMLElement} [opts.context] defaults to <body>\n * @param {number} [opts.minHeight] minimum height of context required to track\n *\n * @returns {ScrollTracker}\n */\n function ScrollTracker(opts) {\n\n if (!(this instanceof ScrollTracker)) return new ScrollTracker(opts);\n\n opts = opts || {};\n\n var context = opts.context || 'body';\n\n if (typeof context === 'string') context = document.querySelector(context);\n\n if (!context) throw new Error('Unable to find context ' + context);\n\n this._context = context;\n this.minHeight = opts.minHeight || 0;\n this._marks = {};\n this._tracked = {};\n this._config = {\n percentages: {\n each: {},\n every: {}\n },\n pixels: {\n each: {},\n every: {}\n },\n elements: {\n each: {},\n every: {}\n }\n };\n\n var boundAndThrottledDepthCheck = throttle(this._checkDepth.bind(this), 500);\n var boundUpdate = this._update.bind(this);\n var throttledUpdate = throttle(boundUpdate, 500);\n\n window.addEventListener('scroll', boundAndThrottledDepthCheck, true);\n window.addEventListener('resize', throttledUpdate);\n\n this._artifacts = {\n timer: onDocHeightChange(boundUpdate),\n resize: throttledUpdate,\n scroll: boundAndThrottledDepthCheck\n };\n\n }\n\n /**\n * Cleans up timer and event bindings\n */\n ScrollTracker.prototype.destroy = function() {\n\n clearInterval(this._artifacts._timer);\n window.removeEventListener('resize', this._artifacts.resize);\n window.removeEventListener('scroll', this._artifacts.scroll, true);\n\n };\n\n /**\n * Registers a handler for a given configuration\n *\n * @param {object} config\n * @param {object} [config.percentages]\n * @param {number[]} [config.percentages.each] tracks every 100 / n percentage\n * @param {number[]} [config.percentages.every] tracks each percentage once\n * @param {object} [config.pixels]\n * @param {number[]} [config.pixels.each] tracks every context.height() / n pixel depth\n * @param {number[]} [config.pixels.every] tracks each pixel depth once\n * @param {object} [config.elements]\n * @param {string[]} [config.elements.each] tracks every element that matches each selector\n * @param {string[]} [config.elements.every] tracks the first element that matches each selector\n * @param {function} handler\n */\n ScrollTracker.prototype.on = function(config, handler) {\n\n var _config = this._config;\n\n ['percentages', 'pixels', 'elements'].forEach(function(type) {\n\n if (!config[type]) return;\n\n ['each', 'every'].forEach(function(freq) {\n\n if (!config[type][freq]) return;\n\n config[type][freq].forEach(function(key) {\n\n _config[type][freq][key] = _config[type][freq][key] || [];\n _config[type][freq][key].push(handler);\n\n });\n\n });\n\n });\n\n this._update();\n\n };\n\n /**\n * Checks marks and depth\n */\n ScrollTracker.prototype._update = function() {\n\n this._calculateMarks();\n this._checkDepth();\n\n };\n\n /**\n * Calculates the pixels for all configs\n */\n ScrollTracker.prototype._calculateMarks = function() {\n\n delete this._marks;\n this._fromTop = getNodeDistanceFromTop(this._context);\n this._marks = {};\n\n var _config = this._config;\n var contextHeight = this._contextHeight();\n var addMark = this._addMark.bind(this);\n var self = this;\n var elements,\n element,\n depth,\n key;\n\n if (contextHeight < this.minHeight) return;\n\n for (key in _config.percentages.every) {\n\n forEachIn({\n n: Number(key),\n numerator: 100,\n callback: percentagesEveryCallback(_config.percentages.every[key])\n });\n\n }\n\n for (key in _config.pixels.every) {\n\n forEachIn({\n n: Number(key),\n numerator: contextHeight,\n callback: pixelsEveryCallback(_config.pixels.every[key])\n });\n\n }\n\n for (key in _config.percentages.each) {\n\n depth = Math.floor(contextHeight * Number(key) / 100);\n\n addMark({\n label: key + '%',\n depth: depth,\n handlers: _config.percentages.each[key]\n });\n\n }\n\n for (key in _config.pixels.each) {\n\n depth = Number(key);\n\n addMark({\n label: key + 'px',\n depth: depth,\n handlers: _config.pixels.each[key]\n });\n\n }\n\n for (key in _config.elements.every) {\n\n elements = [].slice.call(this._context.querySelectorAll(key));\n\n if (elements.length) {\n\n elements.forEach(elementsEveryCallback(key, _config.elements.every[key]));\n\n }\n\n }\n\n for (key in _config.elements.each) {\n\n element = this._context.querySelector(key);\n\n if (element) {\n\n depth = element.getBoundingClientRect().top -\n self._context.getBoundingClientRect().top;\n\n addMark({\n label: key,\n depth: depth,\n handlers: _config.elements.each[key]\n });\n\n }\n\n }\n\n /**\n * Callback for our everyElements iterations\n *\n * @param {string} key\n * @param {function[]} handlers\n *\n * @returns {everyElement~Callback}\n */\n function elementsEveryCallback(key, handlers) {\n\n /**\n * @callback everyElement~Callback\n *\n * @param {HTMLElement} element\n * @param {number} ind\n */\n return function(element, ind) {\n\n var depth = element.getBoundingClientRect().top -\n self._context.getBoundingClientRect().top;\n\n addMark({\n label: key + '[' + ind + ']',\n depth: depth,\n handlers: _config.elements.every[key]\n });\n\n };\n\n }\n\n\n /**\n * Builds a callback for our everyPercentages iterations\n *\n * @param {function[]} handlers\n *\n * @returns {everyPercentage~Callback}\n */\n function percentagesEveryCallback(handlers) {\n\n /**\n * @callback everyPercentage~Callback\n *\n * @param {number} n\n */\n return function(n) {\n\n var depth = Math.floor(n * contextHeight / 100);\n\n addMark({\n label: String(n) + '%',\n depth: depth,\n handlers: _config.percentages.every[key]\n });\n\n };\n\n }\n\n /**\n * Builds a callback for our everyPixels iterations\n *\n * @param {function[]} handlers\n *\n * @param {number} n\n */\n\n function pixelsEveryCallback(handlers) {\n\n /**\n * @callback everyPixel~Callback\n *\n * @param {function[]} handlers\n */\n return function(n) {\n\n var depth = n;\n\n addMark({\n label: String(depth) + 'px',\n depth: depth,\n handlers: handlers\n });\n\n };\n\n }\n\n };\n\n /**\n * Checks all marks and triggers appropriate handlers\n */\n ScrollTracker.prototype._checkDepth = function() {\n\n var marks = this._marks;\n var currentDepth = this._currentDepth();\n var key;\n\n for (key in marks) {\n\n if (currentDepth >= key && !this._tracked[key]) {\n\n marks[key].forEach(function(boundHandler) {\n boundHandler();\n });\n\n this._tracked[key] = true;\n\n }\n\n }\n\n };\n\n /**\n * Resets the internal cache of tracked marks\n */\n ScrollTracker.prototype.reset = function() {\n\n this._tracked = {};\n this._marks = {};\n\n\t\tthis._update();\n\n };\n\n /**\n * Returns the height of the scrolling context\n *\n * @returns {number}\n */\n ScrollTracker.prototype._contextHeight = function() {\n\n if (this._context !== document.body) return this._context.scrollHeight - 5;\n\n return this._context.clientHeight - 5;\n\n };\n\n /**\n * Returns the current depth we've scrolled into the context\n *\n * @returns {number}\n */\n ScrollTracker.prototype._currentDepth = function() {\n\n var isVisible = visibleInViewport(this._context);\n var depth;\n\n if (!this._context.scrollTop) {\n\n this._context.scrollTop = 1;\n\n if (!this._context.scrollTop) {\n\n depth = (window.pageYOffset ||\n document.documentElement.scrollTop ||\n document.body.scrollTop || 0) - this._fromTop;\n\n } else {\n\n this._context.scrollTop = 0;\n depth = this._context.scrollTop + isVisible;\n\n }\n\n } else {\n\n depth = this._context.scrollTop + isVisible;\n\n }\n\n if (!isVisible) {\n\n return depth >= this._fromTop ? depth : -1;\n\n }\n\n return depth + isVisible;\n\n };\n\n /**\n * Adds a mark to be tracked\n *\n * @param {object} config\n * @param {number} config.depth\n * @param {string} config.label\n * @param {function[]} config.handlers\n */\n ScrollTracker.prototype._addMark = function(config) {\n\n var depth = config.depth;\n\n this._marks[depth] = (this._marks[depth] || []).concat(Mark(config));\n\n };\n\n /**\n * @constructor\n * @private\n *\n * @param {object} config\n * @param {string} config.label\n * @param {number} config.depth\n * @param {function[]} config.handlers\n *\n * @returns {Mark}\n */\n function Mark(config) {\n\n /**\n * A Mark is an array of callbacks bound with their data payloads\n *\n * @name Mark\n *\n * @type {function[]}\n */\n return config.handlers.map(function(handler) {\n\n return handler.bind(this, {\n data: {\n depth: config.depth,\n label: config.label\n }\n });\n\n });\n\n }\n\n /**\n * Calls a callback function each time config.n goes into config.numerator\n *\n * @param {object} config\n * @param {number} config.n\n * @param {number} config.numerator\n * @param {function} config.callback\n */\n function forEachIn(config) {\n\n var len = Math.floor(config.numerator / config.n);\n var i;\n\n for (i = 1; i <= len; i++) {\n\n config.callback(i * config.n);\n\n }\n\n }\n\n /**\n * Helper that watches for changes in the height of the document\n */\n function onDocHeightChange(handler) {\n\n var documentHeight = docHeight();\n\n return setInterval(function() {\n\n if (docHeight() !== documentHeight) {\n\n handler();\n documentHeight = docHeight();\n\n }\n\n }, 500);\n\n }\n\n /**\n * Returns the height of the document\n *\n * @returns {number}\n */\n function docHeight() {\n\n var body = document.body;\n var html = document.documentElement;\n\n return Math.max(body.scrollHeight, body.offsetHeight,\n html.clientHeight, html.scrollHeight, html.offsetHeight);\n\n }\n\n /**\n * Returns the number of pixels of the element visible in the viewport\n * @param {HTMLElement} element\n *\n * @returns {number}\n * adapted from:\n * @link https://stackoverflow.com/questions/24768795/get-the-visible-height-of-a-div-with-jquery#answer-26831113\n */\n function visibleInViewport(element) {\n\n var height = element.offsetHeight;\n var windowHeight = viewportHeight();\n var rect = element.getBoundingClientRect();\n\n return Math.max(\n 0,\n rect.top > 0 ? Math.min(height, windowHeight - rect.top) :\n (rect.bottom < windowHeight ? rect.bottom : windowHeight)\n );\n\n }\n\n /**\n * Returns the height of the viewport\n *\n * @returns {number}\n */\n function viewportHeight() {\n\n var elem = (document.compatMode === \"CSS1Compat\") ?\n document.documentElement :\n document.body;\n\n return elem.clientHeight;\n\n }\n\n /**\n * Retrieves the distance of a node from the top of the document\n *\n * @param {HTMLElement} node\n *\n * @returns {number}\n */\n function getNodeDistanceFromTop(node) {\n\n var nodeTop = node.getBoundingClientRect().top - (node.scrollHeight - node.clientHeight) / 2;\n // @link https://developer.mozilla.org/en-US/docs/Web/API/Window/scrollX\n var docTop = (window.pageYOffset !== undefined) ?\n window.pageYOffset :\n (document.documentElement || document.body.parentNode || document.body).scrollTop;\n\n return nodeTop + docTop;\n\n }\n\n /**\n * Does nothing\n */\n function noop() {}\n\n /**\n * Throttle function borrowed from:\n * Underscore.js 1.5.2\n * http://underscorejs.org\n * (c) 2009-2013 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors\n * Underscore may be freely distributed under the MIT license.\n */\n function throttle(func, wait) {\n var context, args, result;\n var timeout = null;\n var previous = 0;\n var later = function() {\n previous = new Date;\n timeout = null;\n result = func.apply(context, args);\n };\n return function() {\n var now = new Date;\n if (!previous) previous = now;\n var remaining = wait - (now - previous);\n context = this;\n args = arguments;\n if (remaining <= 0) {\n clearTimeout(timeout);\n timeout = null;\n previous = now;\n result = func.apply(context, args);\n } else if (!timeout) {\n timeout = setTimeout(later, remaining);\n }\n return result;\n };\n }\n\n /**\n * Installs a noop'd version of ScrollTracker on the window\n */\n function installMock() {\n\n var fake = {};\n var key;\n\n for (key in ScrollTracker) {\n\n fake[key] = noop;\n\n }\n\n window.ScrollTracker = fake;\n\n }\n\n window.ScrollTracker = ScrollTracker;\n\n})(this);\n/*\n * v2.0.4\n * Created by the Google Analytics consultants at http://www.lunametrics.com/\n * Written by @notdanwilkerson\n * Documentation: https://github.com/lunametrics/gascroll/\n * Licensed under the MIT License\n */\n/*\n * This section of the code handles setting up scroll tracking for GTM\n */\n(function(window) {\n \n if (document.readyState !== 'loading') {\n\n init();\n\n } else {\n\n document.addEventListener('DOMContentLoaded', init);\n\n }\n\n function init() {\n\n var tracker = window.ScrollTracker();\n\n tracker.on({\n percentages: {\n \teach: [10 ,90],\n every: [25]\n }\n }, function(evt) {\n \n dataLayer.push({\n 'event': 'scrollTracking',\n 'attributes': {\n 'pixels': evt.data.depth,\n 'distance': evt.data.label,\n 'label': {{Page Path}}\n }\n });\n\n });\n \n delete window.ScrollTracker;\n \n }\n \n})(window);\n</script>"
},
{
"type": "BOOLEAN",
"key": "supportDocumentWrite",
"value": "false"
}
],
"fingerprint": "1533852458468",
"firingTriggerId": [
"2147479553"
],
"parentFolderId": "6",
"tagFiringOption": "ONCE_PER_EVENT"
},
{
"accountId": "263956808",
"containerId": "2187445",
"tagId": "9",
"name": "GA - Event - Scroll Tracking",
"type": "ua",
"parameter": [
{
"type": "BOOLEAN",
"key": "nonInteraction",
"value": "true"
},
{
"type": "TEMPLATE",
"key": "useDebugVersion",
"value": "{{Debug Mode}}"
},
{
"type": "TEMPLATE",
"key": "eventCategory",
"value": "Scroll Tracking"
},
{
"type": "TEMPLATE",
"key": "trackType",
"value": "TRACK_EVENT"
},
{
"type": "TEMPLATE",
"key": "eventAction",
"value": "{{DLV - attributes.distance}}"
},
{
"type": "TEMPLATE",
"key": "eventLabel",
"value": "{{DLV - attributes.label}}"
},
{
"type": "BOOLEAN",
"key": "overrideGaSettings",
"value": "true"
},
{
"type": "BOOLEAN",
"key": "setTrackerName",
"value": "false"
},
{
"type": "BOOLEAN",
"key": "doubleClick",
"value": "false"
},
{
"type": "LIST",
"key": "fieldsToSet",
"list": [
{
"type": "MAP",
"map": [
{
"type": "TEMPLATE",
"key": "fieldName",
"value": "cookieDomain"
},
{
"type": "TEMPLATE",
"key": "value",
"value": "auto"
}
]
}
]
},
{
"type": "BOOLEAN",
"key": "enableLinkId",
"value": "false"
},
{
"type": "BOOLEAN",
"key": "enableEcommerce",
"value": "false"
},
{
"type": "TEMPLATE",
"key": "trackingId",
"value": "{{YOUR_GA_TRACKING_ID}}"
}
],
"fingerprint": "1525465934066",
"firingTriggerId": [
"7"
],
"parentFolderId": "6",
"tagFiringOption": "ONCE_PER_EVENT"
}
],
"trigger": [
{
"accountId": "263956808",
"containerId": "2187445",
"triggerId": "7",
"name": "Event - Scroll Tracking",
"type": "CUSTOM_EVENT",
"customEventFilter": [
{
"type": "EQUALS",
"parameter": [
{
"type": "TEMPLATE",
"key": "arg0",
"value": "{{_event}}"
},
{
"type": "TEMPLATE",
"key": "arg1",
"value": "scrollTracking"
}
]
}
],
"fingerprint": "1462238385247",
"parentFolderId": "6"
}
],
"variable": [
{
"accountId": "263956808",
"containerId": "2187445",
"variableId": "5",
"name": "DLV - attributes.distance",
"type": "v",
"parameter": [
{
"type": "BOOLEAN",
"key": "setDefaultValue",
"value": "false"
},
{
"type": "INTEGER",
"key": "dataLayerVersion",
"value": "2"
},
{
"type": "TEMPLATE",
"key": "name",
"value": "attributes.distance"
}
],
"fingerprint": "1494007112284",
"parentFolderId": "6"
},
{
"accountId": "263956808",
"containerId": "2187445",
"variableId": "6",
"name": "DLV - attributes.label",
"type": "v",
"parameter": [
{
"type": "BOOLEAN",
"key": "setDefaultValue",
"value": "false"
},
{
"type": "INTEGER",
"key": "dataLayerVersion",
"value": "2"
},
{
"type": "TEMPLATE",
"key": "name",
"value": "attributes.label"
}
],
"fingerprint": "1494007112284",
"parentFolderId": "6"
}
],
"folder": [
{
"accountId": "263956808",
"containerId": "2187445",
"folderId": "6",
"name": "LunaMetrics Scroll Tracking Plugin",
"fingerprint": "1462238385246"
}
],
"builtInVariable": [
{
"accountId": "263956808",
"containerId": "2187445",
"type": "PAGE_PATH",
"name": "Page Path"
},
{
"accountId": "263956808",
"containerId": "2187445",
"type": "DEBUG_MODE",
"name": "Debug Mode"
}
],
"fingerprint": "0",
"tagManagerUrl": "https://tagmanager.google.com/#/versions/accounts/263956808/containers/2187445/versions/0?apiLink=version"
}
}