Added methods for events

Andrew Kane committed Apr 28, 2014
commit 82bff1d656c5ed290e9891ac108f23718a56111a
Showing 8 changed files with 54 additions and 20 deletions
CHANGELOG.md +4 -0
@@ @@ -1,3 +1,7 @@
+ ## 0.1.4
+
+ - Added `ahoy.ready()` and `ahoy.log()` for events
+
## 0.1.3
- Supports `current_user` from `ApplicationController`
README.md +2 -1
@@ @@ -17,6 +17,8 @@ See which campaigns generate the most revenue effortlessly.
Order.joins(:visit).group("utm_campaign").sum(:revenue)
```
+ :seedling: To track events like page views, check out [Ahoy Events](https://github.com/ankane/ahoy_events).
+
## Installation
Add this line to your application’s Gemfile:
@@ @@ -210,7 +212,6 @@ var ahoy = {"platform": "Mobile Web"}
## TODO
- - track events
- track emails
- simple dashboard
- hook to store additional fields
app/controllers/ahoy/base_controller.rb +21 -0
@@ @@ -0,0 +1,21 @@
+ module Ahoy
+ class BaseController < ApplicationController
+ # skip all filters
+ skip_filter *_process_action_callbacks.map(&:filter)
+
+ before_filter :halt_bots
+
+ protected
+
+ def browser
+ @browser ||= Browser.new(ua: request.user_agent)
+ end
+
+ def halt_bots
+ if browser.bot?
+ render json: {}
+ end
+ end
+
+ end
+ end
app/controllers/ahoy/visits_controller.rb +1 -15
@@ @@ -1,9 +1,5 @@
module Ahoy
- class VisitsController < ApplicationController
- # skip all filters
- skip_filter *_process_action_callbacks.map(&:filter)
-
- before_filter :halt_bots
+ class VisitsController < BaseController
def create
visit_token = generate_token
@@ @@ -33,15 +29,5 @@ module Ahoy
SecureRandom.urlsafe_base64(32).gsub(/[\-_]/, "").first(32)
end
- def browser
- @browser ||= Browser.new(ua: request.user_agent)
- end
-
- def halt_bots
- if browser.bot?
- render json: {}
- end
- end
-
end
end
config/routes.rb +3 -1
@@ @@ -3,5 +3,7 @@ Rails.application.routes.draw do
end
Ahoy::Engine.routes.draw do
- resources :visits, only: [:create]
+ scope module: "ahoy" do
+ resources :visits, only: [:create]
+ end
end
ahoy/controller.rb b/lib/ahoy/controller.rb +0 -2
@@ @@ -8,8 +8,6 @@ module Ahoy
end
end
- protected
-
def current_visit
visit_token = cookies[:ahoy_visit] || request.headers["Ahoy-Visit"]
if visit_token
ahoy/engine.rb b/lib/ahoy/engine.rb +0 -1
@@ @@ -1,5 +1,4 @@
module Ahoy
class Engine < ::Rails::Engine
- isolate_namespace Ahoy
end
end
vendor/assets/javascripts/ahoy.js +23 -0
@@ @@ -8,6 +8,8 @@
var visitToken, visitorToken;
var visitTtl = 4 * 60; // 4 hours
var visitorTtl = 2 * 365 * 24 * 60; // 2 years
+ var isReady = false;
+ var queue = [];
// cookies
@@ @@ -52,6 +54,14 @@
}
}
+ function setReady() {
+ var callback;
+ while (callback = queue.shift()) {
+ callback();
+ }
+ isReady = true;
+ }
+
// main
visitToken = getCookie("ahoy_visit");
@@ @@ -60,6 +70,7 @@
if (visitToken && visitorToken && visitToken != "test") {
// TODO keep visit alive?
log("Active visit");
+ setReady();
} else {
setCookie("ahoy_visit", "test", 1);
@@ @@ -86,9 +97,11 @@
$.post("/ahoy/visits", data, function(response) {
setCookie("ahoy_visit", response.visit_token, visitTtl);
setCookie("ahoy_visitor", response.visitor_token, visitorTtl);
+ setReady();
}, "json");
} else {
log("Cookies disabled");
+ setReady();
}
}
@@ @@ -107,5 +120,15 @@
return true;
};
+ ahoy.log = log;
+
+ ahoy.ready = function (callback) {
+ if (isReady) {
+ callback();
+ } else {
+ queue.push(callback);
+ }
+ };
+
window.ahoy = ahoy;
}(window));