Sha256: 037bc0e2e8b2a44891221268b4353999113c55177ec8166e749121da428a72b0

Contents?: true

Size: 761 Bytes

Versions: 3

Compression:

Stored size: 761 Bytes

Contents

class Rack::Tracker::GoogleAnalytics < Rack::Tracker::Handler
  class Event < OpenStruct
    def write
      ['send', event].to_json.gsub(/\[|\]/, '')
    end

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

    def attributes
      Hash[to_h.map { |k,v| ['event' + k.to_s.capitalize, v] }]
    end
  end

  class Ecommerce < Struct.new(:action, :payload)
    def write
      [self.action, self.payload.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 => [Event.new(event)] }
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rack-tracker-0.0.4 lib/rack/tracker/google_analytics/google_analytics.rb
rack-tracker-0.0.3 lib/rack/tracker/google_analytics/google_analytics.rb
rack-tracker-0.0.2 lib/rack/tracker/google_analytics/google_analytics.rb