Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Piwik DSL #10

Open
halfdan opened this issue Jan 5, 2013 · 4 comments
Open

Implement Piwik DSL #10

halfdan opened this issue Jan 5, 2013 · 4 comments
Milestone

Comments

@halfdan
Copy link
Owner

halfdan commented Jan 5, 2013

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.

refs #7

@berkes
Copy link
Contributor

berkes commented Jan 5, 2013

Sounds like a good and clean interface. 👍

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 :)

@halfdan
Copy link
Owner Author

halfdan commented Jan 5, 2013

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.

halfdan added a commit that referenced this issue Feb 3, 2013
is somewhat of an issue. Specified the API in a huge data structure for
now.

If you know some better way to do this, let me know.

method_missing is used to check whether a method exists, validate
the arguments and add it to list. The list will be converted to valid
JS calls (based on use_async?) and rendered out for piwik_tracking_tag.

refs #7, #10
@halfdan
Copy link
Owner Author

halfdan commented Feb 20, 2013

@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.

@berkes
Copy link
Contributor

berkes commented Feb 21, 2013

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants