Fixed visit tracking

Andrew Kane committed Jun 17, 2014
commit 863aa63bc12947d99cf1e65b19c082e3bbd0281e
Showing 3 changed files with 19 additions and 6 deletions
ahoy/controller.rb b/lib/ahoy/controller.rb +3 -1
@@ @@ -27,7 +27,9 @@ module Ahoy
end
def track_ahoy_visit
- ahoy.track_visit(defer: !Ahoy.track_visits_on_server)
+ if ahoy.new_visit?
+ ahoy.track_visit(defer: !Ahoy.track_visits_on_server)
+ end
end
end
ahoy/tracker.rb b/lib/ahoy/tracker.rb +5 -1
@@ @@ -23,7 +23,7 @@ module Ahoy
end
def track_visit(options = {})
- unless exclude? or existing_visit_id
+ unless exclude?
if options[:defer]
set_cookie("ahoy_track", true)
else
@@ @@ -60,6 +60,10 @@ module Ahoy
@visitor_id ||= ensure_uuid(existing_visitor_id || visitor_token)
end
+ def new_visit?
+ !existing_visit_id
+ end
+
def set_visit_cookie
set_cookie("ahoy_visit", visit_id, Ahoy.visit_duration)
end
vendor/assets/javascripts/ahoy.js +11 -4
@@ @@ -13,7 +13,7 @@
var ahoy = window.ahoy || window.Ahoy || {};
var $ = window.jQuery || window.Zepto || window.$;
- var visitId, visitorId;
+ var visitId, visitorId, track;
var visitTtl = 4 * 60; // 4 hours
var visitorTtl = 2 * 365 * 24 * 60; // 2 years
var isReady = false;
@@ @@ -138,14 +138,21 @@
visitId = getCookie("ahoy_visit");
visitorId = getCookie("ahoy_visitor");
+ track = getCookie("ahoy_track");
- if (visitId && visitorId) {
+ if (visitId && visitorId && !track) {
// TODO keep visit alive?
log("Active visit");
setReady();
} else {
- visitId = generateId();
- setCookie("ahoy_visit", visitId, visitTtl);
+ if (track) {
+ destroyCookie("ahoy_track");
+ }
+
+ if (!visitId) {
+ visitId = generateId();
+ setCookie("ahoy_visit", visitId, visitTtl);
+ }
// make sure cookies are enabled
if (getCookie("ahoy_visit")) {