Skip to content

Instantly share code, notes, and snippets.

@SonOfLilit
Created June 5, 2014 00:42
Show Gist options
  • Save SonOfLilit/421c9f89bfdf6f803198 to your computer and use it in GitHub Desktop.
Save SonOfLilit/421c9f89bfdf6f803198 to your computer and use it in GitHub Desktop.
Basic hysteresis for riveted.js
commit 0fe549297387bf971f96154d9339ed7ee478a029
Author: Aur Saraf <sonoflilit@gmail.com>
Date: Thu Jun 5 03:18:14 2014 +0300
google analytics activity tracking
diff --git a/v3/js/riveted.js b/v3/js/riveted.js
index 5d8bf9e..b337113 100644
--- a/v3/js/riveted.js
+++ b/v3/js/riveted.js
@@ -20,6 +20,13 @@ var riveted = (function() {
classicGA,
googleTagManager;
+ var activityMeasure = 0,
+ highActivityValue = 30,
+ lowActivityValue = 10,
+ activityThreshold = 60,
+ activityCooldownFactor = 0.8,
+ lastActivity = startTime;
+
/*
* Determine which version of GA is being used
* "ga", "_gaq", and "dataLayer" are the possible globals
@@ -51,10 +58,10 @@ var riveted = (function() {
}
// Basic activity event listeners
- addListener(document, 'keydown', trigger);
- addListener(document, 'click', trigger);
- addListener(window, 'mousemove', throttle(trigger, 500));
- addListener(window, 'scroll', throttle(trigger, 500));
+ addListener(document, 'keydown', lowActivity);
+ addListener(document, 'click', highActivity);
+ addListener(window, 'mousemove', throttle(lowActivity, 500));
+ addListener(window, 'scroll', throttle(lowActivity, 500));
// Page visibility listeners
addListener(document, 'visibilitychange', visibilityChange);
@@ -208,6 +215,27 @@ var riveted = (function() {
}
+ function activity(amount) {
+ var time = new Date();
+ var elapsed = (time - lastActivity) / 1000;
+ lastActivity = time;
+ activityMeasure *= Math.pow(activityCooldownFactor, elapsed);
+
+ activityMeasure += amount;
+
+ if (activityMeasure >= activityThreshold) {
+ trigger();
+ }
+ }
+
+ function highActivity() {
+ activity(highActivityValue);
+ }
+
+ function lowActivity() {
+ activity(lowActivityValue);
+ }
+
function trigger() {
if (!started) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment