halfdan / piwik-ruby-tracking

A Ruby Gem that allows you to add the Piwik Tracking Code to Sinatra, Padrinorb and Rails
MIT License
52 stars 33 forks source link

Implement Piwik Ecommerce tags #7

Open berkes opened 11 years ago

berkes commented 11 years ago

Hi there, for a rails/spree ecommerce site, I am implementing piwik e-commerce tags.

They would be optionally: as in: you can toggle them on in your piwik.yml and after enabling, push ecommerce variables into the tags from your controllers. As outlined in the usage chapter in the Readme.

A few questions beforehand:

halfdan commented 11 years ago

Hi!

This is a very nice idea, but it should be more flexible in terms of configuration. Right now I am working on an extension for the view helper to provide access to the JavaScript Tracking API.

It might look like this:

<%= piwik_tracking_tag do |piwik| %>
  <% piwik.set_custom_variable 1, "Name", "Value", Scope" %>
  <% piwik.track_goal 1 %>
<% end %>

This will output JavaScript for both tracking options (async/sync). If you have any comments on this implementation I'd be happy to hear them. I am not quite sure how to make this accessible via the controller, but I'll figure something out.

To answer your questions:

Yes, this certainly is a feature I'd like to see implemented. I will push my current implementation (as described in the snipped) in a few days. Introducing another view (especially one that is conflicting the use_async: true setting in the config) might not be the best choice.

berkes commented 11 years ago

First off: I'll wait untill you have pushed your implementation, so I can make the ecommerce-tags use the same syntax, DSL and templates.

I like the block-approach, but I think for many of the variables, the view is the wrong place to pass them on to the tag, not? Would it not be better to build the tag in the controller at that?

set_piwik_tracking_tag do |piwik|
  piwik.track_goal 1 if @comment.published?
end

This goes for the e-commerce tags for sure: many of the variables are conditionally set, calculated and prepared in one or more of the various controllers. It would become really messy if I had to have code in application.rb like :

<%= piwik_tracking_tag do |piwik| %>
  <% if (cart.not_empty? && cart.has_new_items? && current_page_is_cart?) %>
     <% piwik.ecommerce.update_cart(@cart.total) %>
 <% end %>
<% end %>

It seems cleaner to me, to determine these tags in the controller(s) and push them into the piwik-tag from there. But then again, I am not extremely familiar with the exact boundiies of what belongs in a controller, view or even model :)

edit clarified my "messy" code my expanding it into a more realistic example.

halfdan commented 11 years ago

@berkes I'll push the first refined version of the DSL tomorrow. It would be great if you could take a look at it and tell me if that allows you to implement ECommerce Tracking easily. I'm open to suggestions!

berkes commented 11 years ago

Thanks for all the work; I am not sure if I can evaluate the code tomorrow, but I will get back ASAP.

halfdan commented 11 years ago

Take your time - I am not very happy with the code anyways (see branch dsl btw.), some parts are still missing.