From 8f23c8fdef7a6220493d1d3c294211ea904c6e5e Mon Sep 17 00:00:00 2001 From: Dan Wilkerson Date: Fri, 4 May 2018 16:43:04 -0400 Subject: [PATCH] Fixed bug w/ reset() --- karma.conf.js | 2 +- luna-scroll-tracking.json | 96 ++++++++++++++++++++------------------ package.json | 3 +- scroll-tracker.js | 7 +-- scroll-tracker.min.js | 4 +- src/scroll-tracker.js | 5 +- test/scrollTracker.spec.js | 65 ++++++++++++++++++-------- 7 files changed, 109 insertions(+), 73 deletions(-) diff --git a/karma.conf.js b/karma.conf.js index 5b8dd08..e9c7a70 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -14,7 +14,7 @@ module.exports = function(config) { autoWatch: true, - browsers: ['Chrome'] + browsers: ['PhantomJS'] }); }; diff --git a/luna-scroll-tracking.json b/luna-scroll-tracking.json index 7e2a9d0..8269da7 100644 --- a/luna-scroll-tracking.json +++ b/luna-scroll-tracking.json @@ -1,35 +1,35 @@ { "exportFormatVersion": 2, - "exportTime": "2017-10-16 14:28:16", + "exportTime": "2018-05-04 20:42:26", "containerVersion": { - "path": "accounts/316270/containers/2058549/versions/0", - "accountId": "316270", - "containerId": "2058549", + "path": "accounts/263956808/containers/2187445/versions/0", + "accountId": "263956808", + "containerId": "2187445", "containerVersionId": "0", "container": { - "path": "accounts/316270/containers/2058549", - "accountId": "316270", - "containerId": "2058549", - "name": "Example Container", - "publicId": "GTM-T72QJV", + "path": "accounts/263956808/containers/2187445", + "accountId": "263956808", + "containerId": "2187445", + "name": "Scroll Tracking", + "publicId": "GTM-NRXSRH", "usageContext": [ "WEB" ], - "fingerprint": "1508163882150", - "tagManagerUrl": "https://tagmanager.google.com/#/container/accounts/316270/containers/2058549/workspaces?apiLink=container" + "fingerprint": "1525466264139", + "tagManagerUrl": "https://tagmanager.google.com/#/container/accounts/263956808/containers/2187445/workspaces?apiLink=container" }, "tag": [ { - "accountId": "316270", - "containerId": "2058549", - "tagId": "4", + "accountId": "263956808", + "containerId": "2187445", + "tagId": "8", "name": "CU - Scroll Tracking - LunaMetrics Plugin", "type": "html", "parameter": [ { "type": "TEMPLATE", "key": "html", - "value": "" + "value": "" }, { "type": "BOOLEAN", @@ -37,17 +37,17 @@ "value": "false" } ], - "fingerprint": "1508163878195", + "fingerprint": "1525465877525", "firingTriggerId": [ "2147479553" ], - "parentFolderId": "11", + "parentFolderId": "6", "tagFiringOption": "ONCE_PER_EVENT" }, { - "accountId": "316270", - "containerId": "2058549", - "tagId": "5", + "accountId": "263956808", + "containerId": "2187445", + "tagId": "9", "name": "GA - Event - Scroll Tracking", "type": "ua", "parameter": [ @@ -133,19 +133,19 @@ "value": "{{YOUR_GA_TRACKING_ID}}" } ], - "fingerprint": "1508164001751", + "fingerprint": "1525465934066", "firingTriggerId": [ - "12" + "7" ], - "parentFolderId": "11", + "parentFolderId": "6", "tagFiringOption": "ONCE_PER_EVENT" } ], "trigger": [ { - "accountId": "316270", - "containerId": "2058549", - "triggerId": "12", + "accountId": "263956808", + "containerId": "2187445", + "triggerId": "7", "name": "Event - Scroll Tracking", "type": "CUSTOM_EVENT", "customEventFilter": [ @@ -165,15 +165,15 @@ ] } ], - "fingerprint": "1508163750962", - "parentFolderId": "11" + "fingerprint": "1462238385247", + "parentFolderId": "6" } ], "variable": [ { - "accountId": "316270", - "containerId": "2058549", - "variableId": "12", + "accountId": "263956808", + "containerId": "2187445", + "variableId": "5", "name": "DLV - attributes.distance", "type": "v", "parameter": [ @@ -193,13 +193,13 @@ "value": "attributes.distance" } ], - "fingerprint": "1508163750963", - "parentFolderId": "11" + "fingerprint": "1494007112284", + "parentFolderId": "6" }, { - "accountId": "316270", - "containerId": "2058549", - "variableId": "13", + "accountId": "263956808", + "containerId": "2187445", + "variableId": "6", "name": "DLV - attributes.label", "type": "v", "parameter": [ @@ -219,28 +219,34 @@ "value": "attributes.label" } ], - "fingerprint": "1508163750964", - "parentFolderId": "11" + "fingerprint": "1494007112284", + "parentFolderId": "6" } ], "folder": [ { - "accountId": "316270", - "containerId": "2058549", - "folderId": "11", + "accountId": "263956808", + "containerId": "2187445", + "folderId": "6", "name": "LunaMetrics Scroll Tracking Plugin", - "fingerprint": "1508163750961" + "fingerprint": "1462238385246" } ], "builtInVariable": [ { - "accountId": "316270", - "containerId": "2058549", + "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/316270/containers/2058549/versions/0?apiLink=version" + "tagManagerUrl": "https://tagmanager.google.com/#/versions/accounts/263956808/containers/2187445/versions/0?apiLink=version" } } \ No newline at end of file diff --git a/package.json b/package.json index b0f8aa6..4cffecd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "scroll-tracking-google-analytics", - "version": "2.0.2", + "version": "2.0.3", "description": "Plug-and-play context-aware scroll tracking with no dependencies.", "main": "Gruntfile.js", "scripts": { @@ -17,6 +17,7 @@ "grunt-contrib-jshint": "~0.12.0", "grunt-contrib-uglify": "^0.9.1", "jasmine-core": "^2.8.0", + "js-beautify": "^1.7.5", "karma": "^1.7.1", "karma-jasmine": "^1.1.0", "karma-phantomjs-launcher": "^1.0.4", diff --git a/scroll-tracker.js b/scroll-tracker.js index 28197a8..052af33 100644 --- a/scroll-tracker.js +++ b/scroll-tracker.js @@ -366,8 +366,9 @@ ScrollTracker.prototype.reset = function() { this._tracked = {}; - delete this._marks; - this.marks = {}; + this._marks = {}; + + this._update(); }; @@ -649,7 +650,7 @@ })(this); /* - * v2.0.2 + * v2.0.3 * Created by the Google Analytics consultants at http://www.lunametrics.com/ * Written by @notdanwilkerson * Documentation: https://github.com/lunametrics/gascroll/ diff --git a/scroll-tracker.min.js b/scroll-tracker.min.js index b53d728..4fc05e5 100644 --- a/scroll-tracker.min.js +++ b/scroll-tracker.min.js @@ -1,6 +1,6 @@ -!function(a){"use strict";function b(c){if(!(this instanceof b))return new b(c);c=c||{};var d=c.context||"body";if("string"==typeof d&&(d=m.querySelector(d)),!d)throw new Error("Unable to find context "+d);this._context=d,this.minHeight=c.minHeight||0,this._marks={},this._tracked={},this._config={percentages:{each:{},every:{}},pixels:{each:{},every:{}},elements:{each:{},every:{}}};var f=k(this._checkDepth.bind(this),500),g=this._update.bind(this),h=k(g,500);a.addEventListener("scroll",f,!0),a.addEventListener("resize",h),this._artifacts={timer:e(g),resize:h,scroll:f}}function c(a){return a.handlers.map(function(b){return b.bind(this,{data:{depth:a.depth,label:a.label}})})}function d(a){var b,c=Math.floor(a.numerator/a.n);for(b=1;b<=c;b++)a.callback(b*a.n)}function e(a){var b=f();return setInterval(function(){f()!==b&&(a(),b=f())},500)}function f(){var a=m.body,b=m.documentElement;return Math.max(a.scrollHeight,a.offsetHeight,b.clientHeight,b.scrollHeight,b.offsetHeight)}function g(a){var b=a.offsetHeight,c=h(),d=a.getBoundingClientRect();return Math.max(0,d.top>0?Math.min(b,c-d.top):d.bottom=a&&!this._tracked[a]&&(b[a].forEach(function(a){a()}),this._tracked[a]=!0)},b.prototype.reset=function(){this._tracked={},delete this._marks,this.marks={}},b.prototype._contextHeight=function(){return this._context!==m.body?this._context.scrollHeight-5:this._context.clientHeight-5},b.prototype._currentDepth=function(){var b,c=g(this._context);return this._context.scrollTop?b=this._context.scrollTop+c:(this._context.scrollTop=1,this._context.scrollTop?(this._context.scrollTop=0,b=this._context.scrollTop+c):b=a.pageYOffset||m.documentElement.scrollTop||m.body.scrollTop||0),c?b+c:b>=this._fromTop?b:-1},b.prototype._addMark=function(a){var b=a.depth;this._marks[b]=(this._marks[b]||[]).concat(c(a))},a.ScrollTracker=b}(this); +!function(a){"use strict";function b(c){if(!(this instanceof b))return new b(c);c=c||{};var d=c.context||"body";if("string"==typeof d&&(d=m.querySelector(d)),!d)throw new Error("Unable to find context "+d);this._context=d,this.minHeight=c.minHeight||0,this._marks={},this._tracked={},this._config={percentages:{each:{},every:{}},pixels:{each:{},every:{}},elements:{each:{},every:{}}};var f=k(this._checkDepth.bind(this),500),g=this._update.bind(this),h=k(g,500);a.addEventListener("scroll",f,!0),a.addEventListener("resize",h),this._artifacts={timer:e(g),resize:h,scroll:f}}function c(a){return a.handlers.map(function(b){return b.bind(this,{data:{depth:a.depth,label:a.label}})})}function d(a){var b,c=Math.floor(a.numerator/a.n);for(b=1;b<=c;b++)a.callback(b*a.n)}function e(a){var b=f();return setInterval(function(){f()!==b&&(a(),b=f())},500)}function f(){var a=m.body,b=m.documentElement;return Math.max(a.scrollHeight,a.offsetHeight,b.clientHeight,b.scrollHeight,b.offsetHeight)}function g(a){var b=a.offsetHeight,c=h(),d=a.getBoundingClientRect();return Math.max(0,d.top>0?Math.min(b,c-d.top):d.bottom=a&&!this._tracked[a]&&(b[a].forEach(function(a){a()}),this._tracked[a]=!0)},b.prototype.reset=function(){this._tracked={},this._marks={},this._update()},b.prototype._contextHeight=function(){return this._context!==m.body?this._context.scrollHeight-5:this._context.clientHeight-5},b.prototype._currentDepth=function(){var b,c=g(this._context);return this._context.scrollTop?b=this._context.scrollTop+c:(this._context.scrollTop=1,this._context.scrollTop?(this._context.scrollTop=0,b=this._context.scrollTop+c):b=a.pageYOffset||m.documentElement.scrollTop||m.body.scrollTop||0),c?b+c:b>=this._fromTop?b:-1},b.prototype._addMark=function(a){var b=a.depth;this._marks[b]=(this._marks[b]||[]).concat(c(a))},a.ScrollTracker=b}(this); /* - * v2.0.2 + * v2.0.3 * Created by the Google Analytics consultants at http://www.lunametrics.com/ * Written by @notdanwilkerson * Documentation: https://github.com/lunametrics/gascroll/ diff --git a/src/scroll-tracker.js b/src/scroll-tracker.js index 9c00a32..53121ac 100644 --- a/src/scroll-tracker.js +++ b/src/scroll-tracker.js @@ -366,8 +366,9 @@ ScrollTracker.prototype.reset = function() { this._tracked = {}; - delete this._marks; - this.marks = {}; + this._marks = {}; + + this._update(); }; diff --git a/test/scrollTracker.spec.js b/test/scrollTracker.spec.js index 87fd6ee..506d358 100644 --- a/test/scrollTracker.spec.js +++ b/test/scrollTracker.spec.js @@ -76,33 +76,32 @@ describe('scroll-tracker', function(){ "nested": { ".nested-every[0]": 0, "10px": 10, - "10%": 40, + "10%": 39, "90px": 90, - "25%": 100, + "25%": 98, "100px": 100, - "50%": 200, + "50%": 197, "200px": 200, ".nested-every[1]": 200, - "75%": 300, + "75%": 296, "300px": 300, - "90%": 360, - "100%": 400, - "400px": 400 + "90%": 355, + "100%": 395 }, "normal": { "10px": 10, "90px": 90, - "10%": 190, - "25%": 475, - "50%": 950, + "10%": 189, + "25%": 473, + "50%": 947, "1000px": 1000, ".every[0]": 1000, "#each": 1200, ".every[1]": 1300, - "75%": 1425, + "75%": 1421, ".every[2]": 1500, - "90%": 1710, - "100%": 1900 + "90%": 1705, + "100%": 1895 } }; @@ -130,12 +129,12 @@ describe('scroll-tracker', function(){ var passed = {}; var outcome = { - '10%': 100, - '25%': 250, - '50%': 500, - '75%': 750, - '90%': 900, - '100%': 1000 + '10%': 99, + '25%': 248, + '50%': 497, + '75%': 746, + '90%': 895, + '100%': 995 }; tracker.on({ @@ -223,6 +222,33 @@ describe('scroll-tracker', function(){ }); + describe('reset()', function() { + + it ('should reset the internal marks', function() { + + var tracker = ScrollTracker(); + var events = []; + + tracker.on({ + percentages: { + each: [10] + } + }, function(evt) { + + events.push(evt); + + }); + + window.scrollTo(0, 1000); + + expect(events.length).toBe(1); + tracker.reset(); + expect(events.length).toBe(2); + + }); + + }); + describe('minHeight', function() { it ('should not set marks because the min height is too small', function() { @@ -332,6 +358,7 @@ describe('scroll-tracker', function(){ }); + }); function noop () {}