Class: AmplitudeExperiment::AmplitudeCookie

Inherits:
Object
  • Object
show all
Defined in:
lib/experiment/cookie.rb

Overview

This class provides utility functions for parsing and handling identity from Amplitude cookies.

Class Method Summary collapse

Class Method Details

Get the cookie name that Amplitude sets for the provided

Parameters:

  • api_key (String)

    The Amplitude API Key

Returns:

  • (String)

    The cookie name that Amplitude sets for the provided

Raises:

  • (ArgumentError)


10
11
12
13
14
# File 'lib/experiment/cookie.rb', line 10

def self.cookie_name(api_key)
  raise ArgumentError, 'Invalid Amplitude API Key' if api_key.nil? || api_key.length < 6

  "amp_#{api_key[0..5]}"
end

.generate(device_id) ⇒ String

Generates a cookie string to set for the Amplitude Javascript SDK

Parameters:

  • device_id (String)

    A device id to set

Returns:

  • (String)

    A cookie string to set for the Amplitude Javascript SDK to read



37
38
39
# File 'lib/experiment/cookie.rb', line 37

def self.generate(device_id)
  "#{device_id}.........."
end

.parse(amplitude_cookie) ⇒ User

Parse a cookie string and returns user

Parameters:

  • amplitude_cookie (String)

    A string from the amplitude cookie

Returns:

  • (User)

    a Experiment User context containing a device_id and user_id



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/experiment/cookie.rb', line 20

def self.parse(amplitude_cookie)
  values = amplitude_cookie.split('.', -1)
  user_id = nil
  unless values[1].nil? || values[1].empty?
    begin
      user_id = Base64.decode64(values[1]).force_encoding('UTF-8')
    rescue StandardError
      user_id = nil
    end
  end
  User.new(user_id: user_id, device_id: values[0])
end