Sha256: 4379cb2fbf28dd9b40d44ec218a2fa6364a0457b8a50c7d50ee67e2cbfc4fc01

Contents?: true

Size: 915 Bytes

Versions: 2

Compression:

Stored size: 915 Bytes

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 self.track(name, *event)
    { name.to_s => [Send.new(event.last)] }
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rack-tracker-0.1.1 lib/rack/tracker/google_analytics/google_analytics.rb
rack-tracker-0.1.0 lib/rack/tracker/google_analytics/google_analytics.rb