class Satellite::Adapters::GoogleAnalytics

Constants

UTM_GIF_LOCATION
VERSION
seems to be the current version
search for 'utmwv' in http://www.google-analytics.com/ga.js

VERSION = ‘4.4sh’

Attributes

account_id[RW]
accept_language[RW]
debug[RW]
user_agent[RW]
utm_location[RW]

Public Class Methods

new(params, use_ssl=false) click to toggle source
# File lib/satellite/adapters/google_analytics.rb, line 7
def initialize(params, use_ssl=false)
  @utm_params = extend_with_default_params(params)
  self.utm_location = (use_ssl ? 'https://ssl' : 'http://www') + UTM_GIF_LOCATION
end

Public Instance Methods

[](key) click to toggle source
# File lib/satellite/adapters/google_analytics.rb, line 59
def [](key)
  @utm_params[key]
end
[]=(key, value) click to toggle source
# File lib/satellite/adapters/google_analytics.rb, line 54
def []=(key, value)
  value = Utme.parse(value) if key.to_s == 'utme'
  @utm_params[key] = value
end
set_custom_variable(index, name, value, scope=nil) click to toggle source
# File lib/satellite/adapters/google_analytics.rb, line 44
def set_custom_variable(index, name, value, scope=nil)
  self[:utme] = Utme.new if self[:utme].nil?
  self[:utme].set_custom_variable(index, name, value, scope)
end
track() click to toggle source
# File lib/satellite/adapters/google_analytics.rb, line 12
def track
  utm_url = tracking_url

  #if (debug == true)
    puts "--------sending request to GA-----------------------"
    puts @utm_params.inspect
    puts utm_url
  #end

  # actually send request
  open(utm_url, { "User-Agent" => 'Satellite/0.2.1', "Accept-Language" => self[:utmul] || 'de' })

  # reset events / custom variables here so they won't be reused in later requests
  self[:utme] = Utme.new
  true
end
track_event(category, action, label=nil, value=nil) click to toggle source
# File lib/satellite/adapters/google_analytics.rb, line 33
def track_event(category, action, label=nil, value=nil)
  self[:utme] = Utme.new if self[:utme].nil?
  self[:utme].set_event(category, action, label, value)
  track
end
track_page_view(path=nil) click to toggle source
# File lib/satellite/adapters/google_analytics.rb, line 39
def track_page_view(path=nil)
  self[:utmp] = path if path
  track
end
tracking_url() click to toggle source
# File lib/satellite/adapters/google_analytics.rb, line 29
def tracking_url
  utm_location + "?" + @utm_params.to_query
end
unset_custom_variable(index) click to toggle source
# File lib/satellite/adapters/google_analytics.rb, line 49
def unset_custom_variable(index)
  self[:utme] = Utme.new if self[:utme].nil?
  self[:utme].unset_custom_variable(index)
end

Private Instance Methods

extend_with_default_params(params) click to toggle source

adds default params

# File lib/satellite/adapters/google_analytics.rb, line 80
def extend_with_default_params(params)
  utme = params.delete(:utme)
  {
      :utmac => self.class.account_id,
      :utmcc => '__utma=999.999.999.999.999.1;', # stub for non-existent cookie,
      :utmcs => 'UTF-8',
      :utme => Utme.parse(utme),
      :utmhid => rand(0x7fffffff).to_s,
      :utmn => rand(0x7fffffff).to_s,
      :utmvid => rand(0x7fffffff).to_s,
      :utmwv => VERSION,
      # should get configured when initializing
      #:utmhn => 'google-analytics.satellite.local',
      #:utmr => 'https://rubygems.org/gems/satellite',
      #:utmp => '/google-analytics',
      #:utmip => '127.0.0.1',
  }.merge(params)
end