Open halfdan opened 11 years ago
Sounds like a good and clean interface. :+1:
Thing is, though, in most situations, I'd consider the controller to be the best place to set these variables; they will need to pass them onto the view anyway in order to set them there with your proposed .set_custom_variable
. The controller will have to know about it anyway.
You state that
I am not quite sure how to make this accessible via the controller,..
Which implies you already thought about this issue too :)
The way I started implementing the setters in the controllers is by proxy-ing everything trough a generic PiwikAnalytics::PiwikTracker
object. This would then be initialised trough a generic factory method, somewhere(*).
def piwik_tracker
@piwik_tracker ||= PiwikAnalytics::PiwikTracker.new
end
Controllers can then set any attribute on the tracker:
def hello
piwik_tracker.document_title = "Hello World"
"Hello World"
end
(*) I don't know if in Rails PiwikAnalytics::Helpers
-methods are available to in controller, I guess not. In any case, id would have to be a module that is available in both the controller and the views, since both would need to speak to the same instance of @piwik_tracker
.
This was my idea, anyway. But I am not too familiar with the proper patterns in a rails engine, so it might just be plain stupid alltogether :)
Oh well, the text was just a copy&paste from the other ticket. Just wanted to have a separate ticket.
The way you implemented it is exactly how I imagined it. I still have some local changes where I have a similar implementation which is available is both controller and view.
I'll commit the changes next week, just need to find a good way to generate Javascript function calls out of it. E.g.:
<%= piwik_tracking_tag do |piwik|
<% piwik.track_goal 1 %>
<% end %>
should automatically transform to
<script type="text/javascript">
try {
var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 1);
piwikTracker.trackGoal(1);
piwikTracker.trackPageView();
piwikTracker.enableLinkTracking();
} catch( err ) {}
</script>
My implementation works by defining two helper methods, one for the controller and one for the view (both share the same PiwikTracker object). In the controller the helper will work as a configuration utility
def hello
piwik_tracker.document_title = "Hello World"
"Hello World"
end
The view helper works as shown above and allows you to access the configuration (in the block). It will then transform the PiwikTracker object to JavaScript calls.
@berkes, did you have time to look at the implementation?
I'm thinking of just implementing all methods manually instead of using ruby meta programming to generate them on-demand.
Sadly (or, actually not :) I did not have time to look at it before my holidays. I will implement it once I get back in the first week of May.
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:
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.
refs #7