From 0bf5c4619f6ee4780a33e87f2f87d4938c785b67 Mon Sep 17 00:00:00 2001
From: Thomas Steur <thomas.steur@gmail.com>
Date: Mon, 7 Nov 2016 19:38:21 +0000
Subject: [PATCH] added disableHeartBeatTimer feature

---
 CHANGELOG.md               |  1 +
 js/piwik.js                | 19 +++++++++++++------
 tests/javascript/index.php |  3 ++-
 3 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index dcc9e4c4d7..1e8878ae92 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -83,6 +83,7 @@ Read more about migrating a plugin from Piwik 2.X to Piwik 3 on our [Migration g
  * `Updater.componentUninstalled` triggered after a component was uninstalled
 * New HTTP Tracking API parameter `pv_id` which accepts a six character unique ID that identifies which actions were performed on a specific page view. Read more about it in the [HTTP Tracking API](https://developer.piwik.org/api-reference/tracking-api);
 * New event `Segment.addSegments` that lets you add segments.
+* New Piwik JavaScript Tracker method `disableHeartBeatTimer()` to disable the heartbeat timer if it was previously enabled. 
 
 ### New features
 * New "Sparklines" visualization that lets you create a widget showing multiple sparklines.
diff --git a/js/piwik.js b/js/piwik.js
index 59f0defc99..2691c1711e 100644
--- a/js/piwik.js
+++ b/js/piwik.js
@@ -6093,18 +6093,25 @@ if (typeof window.Piwik !== 'object') {
                 }
             };
 
-/*<DEBUG>*/
             /**
-             * Clear heartbeat.
+             * Disable heartbeat if it was previously activated.
              */
             this.disableHeartBeatTimer = function () {
                 heartBeatDown();
-                configHeartBeatDelay = null;
+                
+                if (configHeartBeatDelay || heartBeatSetUp) {
+                    if (windowAlias.removeEventListener) {
+                        windowAlias.removeEventListener('focus', heartBeatOnFocus, true);
+                        windowAlias.removeEventListener('blur', heartBeatOnBlur, true);
+                    } else if  (windowAlias.detachEvent) {
+                        windowAlias.detachEvent('onfocus', heartBeatOnFocus);
+                        windowAlias.detachEvent('onblur', heartBeatOnBlur);
+                    }
+                }
 
-                window.removeEventListener('focus', heartBeatOnFocus);
-                window.removeEventListener('blur', heartBeatOnBlur);
+                configHeartBeatDelay = null;
+                heartBeatSetUp = false;
             };
-/*</DEBUG>*/
 
             /**
              * Frame buster
diff --git a/tests/javascript/index.php b/tests/javascript/index.php
index f8cf328249..3cc05b286e 100644
--- a/tests/javascript/index.php
+++ b/tests/javascript/index.php
@@ -2096,7 +2096,7 @@ function PiwikTest() {
     });
 
     test("API methods", function() {
-        expect(71);
+        expect(72);
 
         equal( typeof Piwik.addPlugin, 'function', 'addPlugin' );
         equal( typeof Piwik.addPlugin, 'function', 'addTracker' );
@@ -2158,6 +2158,7 @@ function PiwikTest() {
         equal( typeof tracker.addListener, 'function', 'addListener' );
         equal( typeof tracker.enableLinkTracking, 'function', 'enableLinkTracking' );
         equal( typeof tracker.enableHeartBeatTimer, 'function', 'enableHeartBeatTimer' );
+        equal( typeof tracker.disableHeartBeatTimer, 'function', 'disableHeartBeatTimer' );
         equal( typeof tracker.killFrame, 'function', 'killFrame' );
         equal( typeof tracker.redirectFile, 'function', 'redirectFile' );
         equal( typeof tracker.setCountPreRendered, 'function', 'setCountPreRendered' );
-- 
GitLab