Skip to content

Commit

Permalink
First draft of the DSL. Not ready for production yet since JS generation
Browse files Browse the repository at this point in the history
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
  • Loading branch information
halfdan committed Feb 3, 2013
1 parent d81e9cc commit ee9e6b0
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 6 deletions.
1 change: 1 addition & 0 deletions app/views/piwik_analytics/piwik_tracking_tag.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</script><script type="text/javascript">
try {
var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", <%= id_site %>);
<%= tracker %>
piwikTracker.trackPageView();
piwikTracker.enableLinkTracking();
} catch( err ) {}
Expand Down
5 changes: 5 additions & 0 deletions lib/piwik_analytics.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require File.join(File.dirname(__FILE__), 'piwik_analytics', 'configuration')
require File.join(File.dirname(__FILE__), 'piwik_analytics', 'piwik_tracker')

module PiwikAnalytics
require 'piwik_analytics/engine' if defined?(Rails)
Expand All @@ -9,5 +10,9 @@ class <<self
def configuration
@configuration ||= PiwikAnalytics::Configuration.new
end

def tracker
@tracker ||= PiwikAnalytics::PiwikTracker.new
end
end
end
7 changes: 7 additions & 0 deletions lib/piwik_analytics/controller_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module PiwikAnalytics
module ControllerHelpers
def piwik_tracking(&block)
yield PiwikAnalytics.tracker
end
end
end
3 changes: 2 additions & 1 deletion lib/piwik_analytics/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class Piwik < Rails::Engine
end

initializer "piwik_analytics.init", :before=> :load_config_initializers do
require "piwik_analytics/helpers"
require "piwik_analytics/view_helpers"
require "piwik_analytics/controller_helpers"
load "piwik_analytics/init.rb"
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/piwik_analytics/init.rb
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
ActionView::Base.send :include, PiwikAnalytics::Helpers
ActionView::Base.send :include, PiwikAnalytics::ViewHelpers
ActionController::Base.send :include, PiwikAnalytics::ControllerHelpers
106 changes: 106 additions & 0 deletions lib/piwik_analytics/piwik_tracker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
module PiwikAnalytics
class PiwikTracker

def initialize
@calls = Array.new
end

def method_missing(meth, *args, &block)
name = meth.to_s.camlize(:lower)
valid_method? name
valid_arguments? args
call = { method: name, args: args }
@calls.push call
end

def to_s
@calls.each do |call|

end
end

private
def valid_method?(name)
@@methods.has_key? name
end

def valid_arguments?(name, args)
method = @@methods[name]
# Check each argument (name, optional)
method.each do |arg|
unless arg[:optional]
return false unless args.has_key? arg[:name]
end
end
# Return in order
return true
end
end

@@methods = {
"setDocumentTitle" => [
{ name: :title, optional: false }
],
"setCustomUrl" => [
{ name: :url, optional: false }
],
"setReferrerUrl" => [
{ name: :url, optional: false }
],
"setSiteId" => [
{ name: :site_id, optional: false }
],
"trackGoal" => [
{ name: :id_goal, optional: false },
{ name: :custom_revenue, optional: true }
],
"trackSiteSearch" => [
{ name: :keyword, optional: false },
{ name: :category, optional: true, default: false },
{ name: :search_count, optional: true, default: false }
],
"setCustomVariable" => [
{ name: :index, optional: false },
{ name: :name, optional: false },
{ name: :value, optional: false },
{ name: :scope, optional: true, default: "visit" }
],
"deleteCustomVariable" => [
{ name: :index, optional: false },
{ name: :scope, optional: false }
],
"setCookieDomain" => [
{ name: :domain, optional: false }
],
"setDomains" => [
{ name: :domains, optional: false }
],
"setCookiePath" => [
{ name: :path, optional: false }
],
"setIgnoreClasses" => [
{ name: :classes, optional: false }
],
"setDownloadClasses" => [
{ name: :classes, optional: false }
],
"setLinkClasses" => [
{ name: :classes, optional: false }
],
"setLinkTrackingTimer" => [
{ name: :time, optional: false }
],
"setDownloadExtensions" => [
{ name: :extensions, optional: false }
],
"addDownloadExtensions" => [
{ name: :extensions, optional: false }
],
"enableLinkTracking" => [
{ name: :enable, optional: false }
],
"setRequestMethod" => [
{ name: :method, optional: false }
]
}
end
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
module PiwikAnalytics
module Helpers
def piwik_tracking_tag
module ViewHelpers
def piwik_tracking_tag(&block)
config = PiwikAnalytics.configuration
return if config.disabled?

yield PiwikAnalytics.tracker if block_given?

if 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}
file: file,
locals: {
url: config.url,
id_site: config.id_site,
tracker: PiwikAnalytics.tracker
}
})
end
end
Expand Down

0 comments on commit ee9e6b0

Please sign in to comment.