Sha256: c934cb1097caef580b2a5876f2bf7f8080879d5245e1009179d12de3cca2ba85

Contents?: true

Size: 1.07 KB

Versions: 2

Compression:

Stored size: 1.07 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.stringify_values).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.stringify_values].to_json.gsub(/\[|\]/, '')
    end
  end

  def tracker
    options[:tracker].respond_to?(:call) ? options[:tracker].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

2 entries across 2 versions & 1 rubygems

Version Path
rack-tracker-0.3.0 lib/rack/tracker/google_analytics/google_analytics.rb
rack-tracker-0.2.6 lib/rack/tracker/google_analytics/google_analytics.rb