Sha256: 847809fb49b1d8f89a0d64c304dce508ae26187a048606612699ebea8c663961

Contents?: true

Size: 1 KB

Versions: 6

Compression:

Stored size: 1 KB

Contents

class Rack::Tracker::GoogleAnalytics < Rack::Tracker::Handler
  class Send < OpenStruct
    def initialize(attrs = {})
      attrs.reverse_merge!(type: 'event')
      super
    end

    def write
      ['send', event].to_json.gsub(/\[|\]/, '')
    end

    def event
      { hitType: self.type }.merge(attributes).compact
    end

    def attributes
      Hash[to_h.slice(:category, :action, :label, :value).map { |k,v| [self.type.to_s + k.to_s.capitalize, v] }]
    end
  end

  class Ecommerce < OpenStruct
    def write
      ["ecommerce:#{self.type}", self.to_h.except(:type).compact].to_json.gsub(/\[|\]/, '')
    end
  end

  def tracker
    options[:tracker].try(:call, env) || options[:tracker]
  end

  def render
    Tilt.new( File.join( File.dirname(__FILE__), 'template', 'google_analytics.erb') ).render(self)
  end

  def ecommerce_events
    events.select{|e| e.kind_of?(Ecommerce) }
  end

  def self.track(name, *event)
    { name.to_s => [const_get(event.first.to_s.capitalize).new(event.last)] }
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rack-tracker-0.2.4 lib/rack/tracker/google_analytics/google_analytics.rb
rack-tracker-0.2.3 lib/rack/tracker/google_analytics/google_analytics.rb
rack-tracker-0.2.2 lib/rack/tracker/google_analytics/google_analytics.rb
rack-tracker-0.2.1 lib/rack/tracker/google_analytics/google_analytics.rb
rack-tracker-0.2.0 lib/rack/tracker/google_analytics/google_analytics.rb
rack-tracker-0.1.3 lib/rack/tracker/google_analytics/google_analytics.rb