Module: BigBro

Defined in:
lib/bigbro.rb,
lib/bigbro/railtie.rb,
lib/bigbro/version.rb

Defined Under Namespace

Modules: Helpers, TestHelpers Classes: Railtie

Constant Summary collapse

Version =
'1.0.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.accountObject

Returns the value of attribute account.



46
47
48
# File 'lib/bigbro.rb', line 46

def 
  @account
end

.disabledObject

Returns the value of attribute disabled.



46
47
48
# File 'lib/bigbro.rb', line 46

def disabled
  @disabled
end

.domainObject

Returns the value of attribute domain.



46
47
48
# File 'lib/bigbro.rb', line 46

def domain
  @domain
end

Class Method Details

.disabled?Boolean

In development mode the Analytics code is always disabled, or it can be disabled manually via the configuration.

If no account is set, the code disables itself. Maybe the check in the set() method should be moved here, we’ll see.

Returns:

  • (Boolean)


90
91
92
# File 'lib/bigbro.rb', line 90

def disabled?
  self.disabled || self..blank? || Rails.env.development?
end

.host_for(request) ⇒ Object

Returns the analytics host for the given request (SSL or not)



96
97
98
# File 'lib/bigbro.rb', line 96

def host_for(request)
  (request.ssl? ? 'https://ssl' : 'http://www') + '.google-analytics.com'
end

.noscript_image_path_for(request, ga_host) ⇒ Object

Returns the noscript image path for the given request and GA Host



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/bigbro.rb', line 102

def noscript_image_path_for(request, ga_host)
  cookie = rand(   89_999_999) +    10_000_000
  req_no = rand(8_999_999_999) + 1_000_000_000
  random = rand(1_147_483_647) + 1_000_000_000
  now    = Time.now.to_i

  referer = request.referer.blank? ? '-' : CGI.escape(request.referer)
  path    = request.path.blank?    ? '/' : CGI.escape(request.path)

  utmcc =
    "__utma%3D#{cookie}.#{random}.#{now}.#{now}.#{now}.2%3B%2B" \
    "__utmz%3D#{cookie}.#{now}.2.2."                            \
      "utmccn%3D(direct)%7C"       \
      "utmcsr%3D(direct)%7C"       \
      "utmcmd%3D(none)%3B%2B"      \
   "__utmv%3D#{cookie}.noscript%3B"

  "#{ga_host}/__utm.gif?"    \
    "utmn=#{req_no}&"        \
    "utmac=#{}&"      \
    "utmhn=#{request.host}&" \
    "utmr=#{referer}&"       \
    "utmp=#{path}&"          \
    "utmcc=#{utmcc}&"        \
    'utmwv=1&'               \
    'utmje=0&'               \
    'utmsr=-&'               \
    'utmsc=-&'               \
    'utmul=-&'               \
    'utmfl=-&'               \
    'utmdt=-'
end

.set(options = {}) ⇒ Object

Sets the Analytics account and enforces it to be set in production mode.

If you’re developing and want to run your local copy in production mode, you can either pass an invalid account (e.g. to check how the JS code is generated) or pass the :disabled option set to true.

In test mode, the account is always set to the dummy “UA-420-THEBRAIN” string.

Sets the ‘UA-12345-67’ account:

BigBro.set(:account => 'UA-12345-67')

Sets the ‘UA-12345-67’ account and the ‘foo.com’ domain:

BigBro.set(:account => 'UA-12345-67', :domain => 'foo.com')

Disables analytics code generation:

BigBro.set(:disabled => true)


71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/bigbro.rb', line 71

def set(options = {})
  self., self.disabled, self.domain =
    options.values_at(:account, :disabled, :domain)

  if Rails.env.production?
    if self..blank? && !self.disabled
      raise ArgumentError, 'BigBro: analytics configuration missing'
    end
  elsif Rails.env.test?
    self. = 'UA-420-THEBRAIN'
  end
end