Class: Satellite::Adapters::GoogleAnalytics::Utme::CustomVariables

Inherits:
Object
  • Object
show all
Defined in:
lib/satellite/adapters/google_analytics.rb

Constant Summary collapse

@@valid_keys =
1..5

Instance Method Summary collapse

Constructor Details

#initializeCustomVariables

Returns a new instance of CustomVariables.



218
219
220
# File 'lib/satellite/adapters/google_analytics.rb', line 218

def initialize
  @contents = { }
end

Instance Method Details

#set_custom_variable(slot, custom_variable) ⇒ Object

Raises:

  • (ArgumentError)


222
223
224
225
# File 'lib/satellite/adapters/google_analytics.rb', line 222

def set_custom_variable(slot, custom_variable)
  raise ArgumentError, "Cannot set a slot other than #{@@valid_keys}. Given #{slot}" if not @@valid_keys.include?(slot)
  @contents[slot] = custom_variable
end

#to_sObject

follows google custom variable format

8(NAMES)9(VALUES)11(SCOPES)

best explained by examples

1) pageTracker._setCustomVar(1,“foo”, “val”, 1)

> 8(foo)9(bar)11(1)

2) pageTracker._setCustomVar(1,“foo”, “val”, 1) pageTracker._setCustomVar(2,“bar”, “vok”, 3)

> 8(foo*bar)9(val*vok)11(1*3)

3) pageTracker._setCustomVar(1,“foo”, “val”, 1) pageTracker._setCustomVar(2,“bar”, “vok”, 3) pageTracker._setCustomVar(4,“baz”, “vol”, 1)

> 8(foo*bar*4!baz)9(val*vak*4!vol)11(1*3*4!1)

4) pageTracker._setCustomVar(4,“foo”, “bar”, 1)

> 8(4!foo)9(4!bar)11(4!1)



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/satellite/adapters/google_analytics.rb', line 257

def to_s
  return '' if @contents.empty?

  ordered_keys = @contents.keys.sort
  names = values = scopes = ''

  ordered_keys.each do |slot|
    custom_variable = @contents[slot]
    predecessor = @contents[slot-1]

    has_predecessor = !!predecessor
    has_scoped_predecessor = !!(predecessor.try(:opt_scope))

    star = names.empty? ? '' : '*'
    bang = (slot == 1 || has_predecessor) ? '' : "#{slot}!"

    scope_star = scopes.empty? ? '' : '*'
    scope_bang = (slot == 1 || has_scoped_predecessor) ? '' : "#{slot}!"

    names += "#{star}#{bang}#{custom_variable.name}"
    values += "#{star}#{bang}#{custom_variable.value}"
    scopes += "#{scope_star}#{scope_bang}#{custom_variable.opt_scope}" if custom_variable.opt_scope
  end

  output = "8(#{names})9(#{values})"
  output += "11(#{scopes})" if not scopes.empty?
  output
end

#unset_custom_variable(slot) ⇒ Object

Raises:

  • (ArgumentError)


227
228
229
230
# File 'lib/satellite/adapters/google_analytics.rb', line 227

def unset_custom_variable(slot)
  raise ArgumentError, "Cannot unset a slot other than #{@@valid_keys}. Given #{slot}" if not @@valid_keys.include?(slot)
  @contents.delete(slot)
end