Permalink
Browse files

Add basic ecommerce-support.

* `ecommerce.rb` contains helper classes for container-objects
  EcommerceItem, EcommerceOrder and such. To be extended.
* `piwik_controller.rb` is a mixin that will be added to the
  ActionController on init. For now, you'll need to pull it into your
  controller where you want to push order and item-settings to
  the Ecommerce-tags.
  • Loading branch information...
1 parent 0c676f6 commit ef0f040cc937e8aa23a208cb494c0fbb28a1d2b7 Bèr Kessels committed Oct 14, 2012
@@ -6,11 +6,11 @@
try {
var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", <%= id_site %>);
piwikTracker.trackPageView();
- <% @ecommerce_items.each do |ecommerce_item| %>
- piwikTracker.addEcommerceItem(<%= ecommerce_item.as_js_params %>);
+ <% ecommerce_items.each do |ecommerce_item| %>
+ piwikTracker.addEcommerceItem(<%= raw ecommerce_item.as_js_params %>);
<% end %>
- <% unless @ecommerce_order.nil? %>
- piwikTracker.trackEcommerceOrder(<%= @ecommerce_order.as_js_params %>);
+ <% unless ecommerce_order.nil? %>
+ piwikTracker.trackEcommerceOrder(<%= raw ecommerce_order.as_js_params %>);
<% end %>
piwikTracker.enableLinkTracking();
} catch( err ) {}
@@ -0,0 +1,40 @@
+module PiwikAnalytics
+ class EcommerceItem
+ def initialize(sku, name = "", categories=[], price = 0.0, quantity = 1)
+ categories = [categories] if categories.class == String
+ @sku, @name, @categories, @price, @quantity = sku, name, categories, price, quantity
+ end
+
+ # Returns a string used in template as parameters for the addEcommerceItem() call.
+ def as_js_params
+ categories = @categories.map{|c| "\"#{c}\""}.join(", ")
+ js_params = [@sku, @name, categories, @price, @quantity].map do |parameter|
+ "\"#{parameter.json_escape}\""
+ end.join(", ")
+
+ js_params
+ end
+
+ end
+
+ # An order in Piwik, is a finalized, payed order.
+ class EcommerceOrder
+ # Specifiy the order details to Piwik server & sends the data to Piwik server
+ # "A10000123", // (required) Unique Order ID
+ # 35, // (required) Order Revenue grand total (includes tax, shipping, and subtracted discount)
+ # 30, // (optional) Order sub total (excludes shipping)
+ # 5.5, // (optional) Tax amount
+ # 4.5, // (optional) Shipping amount
+ # false // (optional) Discount offered (set to false for unspecified parameter)
+ def initialize(order_id, grand_total, sub_total = 0.0, tax = 0.0, shipping = 0.0, discount = false)
+ @order_id, @grand_total, @sub_total, @tax, @shipping, @discount = order_id, grand_total, sub_total, tax, shipping, discount
+ end
+ def as_js_params
+ js_params = [@order_id, @grand_total, @sub_total, @tax, @shipping, @discount].map do |parameter|
+ "\"#{parameter.json_escape}\""
+ end.join(", ")
+
+ js_params
+ end
+ end
+end
@@ -14,6 +14,8 @@ class Piwik < Rails::Engine
initializer "piwik_analytics.init", :before=> :load_config_initializers do
require "piwik_analytics/helpers"
+ require "piwik_analytics/piwik_controller"
+ require "piwik_analytics/ecommerce"
load "piwik_analytics/init.rb"
end
end
@@ -4,17 +4,22 @@ def piwik_tracking_tag
config = PiwikAnalytics.configuration
return if config.disabled?
+ locals = {:url => config.url, :id_site => config.id_site}
+
if config.use_ecommerce?
file = "piwik_analytics/piwik_tracking_tag_ecommerce"
+ locals[:ecommerce_items] = @ecommerce_items || []
+ locals[:ecommerce_order] = @ecommerce_order
elsif config.use_async?
file = "piwik_analytics/piwik_tracking_tag_async"
else
file = "piwik_analytics/piwik_tracking_tag"
end
render({
:file => file,
- :locals => {:url => config.url, :id_site => config.id_site}
+ :locals => locals
})
end
+
end
end
@@ -0,0 +1,12 @@
+module PiwikAnalytics
+ module PiwikController
+ def piwik_analytics_add_ecommerce_item(sku, name = "", categories=[], price = 0.0, quantity = 1)
+ @ecommerce_items ||= []
+ @ecommerce_items << PiwikAnalytics::EcommerceItem.new(sku, name, categories, price, quantity)
+ end
+
+ def piwik_analytics_set_ecommerce_order(order_id, grand_total, sub_total = 0.0, tax = 0.0, shipping = 0.0, discount = false)
+ @ecommerce_order = PiwikAnalytics::EcommerceOrder.new(order_id, grand_total, sub_total, tax, shipping, discount)
+ end
+ end
+end

0 comments on commit ef0f040

Please sign in to comment.