Added support for batch events and fixed cookie encoding

Andrew Kane committed May 27, 2014
commit 94b14019bae67068338f9fd249ace38511bd9fa3
Showing 3 changed files with 16 additions and 8 deletions
CHANGELOG.md +2 -0
@@ @@ -1,6 +1,8 @@
## 0.2.2 [unreleased]
- Added `exclude_method` option
+ - Added support for batch events
+ - Fixed cookie encoding
## 0.2.1
app/controllers/ahoy/events_controller.rb +10 -4
@@ @@ -2,11 +2,17 @@ module Ahoy
class EventsController < Ahoy::BaseController
def create
- options = {}
- if params[:time] and (time = Time.at(params[:time].to_f) rescue nil) and (1.minute.ago..Time.now).cover?(time)
- options[:time] = time
+ events = params[:name] ? [params] : ActiveSupport::JSON.decode(request.body.read)
+ events.each do |event|
+ options = {}
+ if event["time"] and (time = Time.at(event["time"].to_f) rescue nil) and (1.minute.ago..Time.now).cover?(time)
+ options[:time] = time
+ end
+ if event["id"]
+ options[:id] = event["id"]
+ end
+ ahoy.track event["name"], event["properties"], options
end
- ahoy.track params[:name], params[:properties], options
render json: {}
end
vendor/assets/javascripts/ahoy.js +4 -4
@@ @@ -27,7 +27,7 @@
if (ahoy.domain) {
cookieDomain = "; domain=" + ahoy.domain;
}
- document.cookie = name + "=" + value + expires + cookieDomain + "; path=/";
+ document.cookie = name + "=" + escape(value) + expires + cookieDomain + "; path=/";
}
function getCookie(name) {
@@ @@ -40,7 +40,7 @@
c = c.substring(1, c.length);
}
if (c.indexOf(nameEQ) === 0) {
- return c.substring(nameEQ.length, c.length);
+ return unescape(c.substring(nameEQ.length, c.length));
}
}
return null;
@@ @@ -100,7 +100,7 @@
$.ajax({
type: "POST",
url: "/ahoy/events",
- data: JSON.stringify(event),
+ data: JSON.stringify([event]),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function() {
@@ @@ -218,7 +218,7 @@
$(document).on("click", "a, button, input[type=submit]", function (e) {
var $target = $(e.currentTarget);
var properties = eventProperties(e);
- properties.text = properties.tag == "input" ? $target.val() : $.trim($target.text());
+ properties.text = properties.tag == "input" ? $target.val() : $.trim($target.text().replace(/[\s\r\n]+/g, " "));
properties.href = $target.attr("href");
ahoy.track("$click", properties);
});