amplitude-experiment 1.2.2 → 1.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e894e7e7ccce306d33d5078ae57fe08fb6a21ecb33e6be30f87c71a7bf119e93
4
- data.tar.gz: '08a1e613c0eca928c2d4b5ac7c98e7eddc7a35df497c8b0b31881f51b060245b'
3
+ metadata.gz: be0a173d9b7f4a54d8a800741ff6801d1ba62a64f33b8e8539be84ab5f91303d
4
+ data.tar.gz: 4b7afd4ffec350c766f0001c9b6a0a01910d2bba780f034c64dbdcdeaa05b702
5
5
  SHA512:
6
- metadata.gz: 4fe44dce88027b7d5e1912126a342bab21732e9657d5dadf46c533ea4f156f4d9aed81e97765e05ddcae2cb4ac703355e9b6e856fc2d2cbeb3a01ada5a3121ac
7
- data.tar.gz: b51fc6e95b72c063bcd7572c7a13220e1ee2a2e164d5d4307d8a194ab77ef2892a24edff30b5dd61277baf124d488b5449c87dd5c5c6eb0b619b5e2be0b14b84
6
+ metadata.gz: 9db6116fe99732edb6f15f4e4adb9df4f58ab9fa9ceea9f21c25e54b05bbdc7f1bfbb1cce15b65f53b22896ab0bf5edac4ef1969668fa7ffaf3280be10811551
7
+ data.tar.gz: cb6aec3974925f334a7a769ea24fbc67a7060d39ddc697af8f9e2d388da7cde447775440e41c0fa15d9bef921c4e9ead381477cbbdfb5350128ece95455a4a13
@@ -1,4 +1,5 @@
1
1
  require 'base64'
2
+ require 'json'
2
3
 
3
4
  module AmplitudeExperiment
4
5
  # This class provides utility functions for parsing and handling identity from Amplitude cookies.
@@ -6,9 +7,17 @@ module AmplitudeExperiment
6
7
  # Get the cookie name that Amplitude sets for the provided
7
8
  #
8
9
  # @param [String] api_key The Amplitude API Key
10
+ # @param [Boolean] new_format True if the cookie is in the Browser SDK 2.0 format
9
11
  # @return [String] The cookie name that Amplitude sets for the provided
10
- def self.cookie_name(api_key)
11
- raise ArgumentError, 'Invalid Amplitude API Key' if api_key.nil? || api_key.length < 6
12
+ def self.cookie_name(api_key, new_format: false)
13
+ raise ArgumentError, 'Invalid Amplitude API Key' if api_key.nil?
14
+
15
+ if new_format
16
+ raise ArgumentError, 'Invalid Amplitude API Key' if api_key.length < 10
17
+
18
+ return "AMP_#{api_key[0..9]}"
19
+ end
20
+ raise ArgumentError, 'Invalid Amplitude API Key' if api_key.length < 6
12
21
 
13
22
  "amp_#{api_key[0..5]}"
14
23
  end
@@ -16,8 +25,20 @@ module AmplitudeExperiment
16
25
  # Parse a cookie string and returns user
17
26
  #
18
27
  # @param [String] amplitude_cookie A string from the amplitude cookie
28
+ # @param [Boolean] new_format True if the cookie is in the Browser SDK 2.0 format
19
29
  # @return [User] a Experiment User context containing a device_id and user_id
20
- def self.parse(amplitude_cookie)
30
+ def self.parse(amplitude_cookie, new_format: false)
31
+ if new_format
32
+ begin
33
+ decoding = Base64.decode64(amplitude_cookie).force_encoding('UTF-8')
34
+ json_data = URI.decode_www_form_component(decoding)
35
+ user_session_hash = JSON.parse(json_data)
36
+ return User.new(user_id: user_session_hash['userId'], device_id: user_session_hash['deviceId'])
37
+ rescue StandardError => e
38
+ puts "Error parsing the Amplitude cookie: #{e.message}"
39
+ return nil
40
+ end
41
+ end
21
42
  values = amplitude_cookie.split('.', -1)
22
43
  user_id = nil
23
44
  unless values[1].nil? || values[1].empty?
@@ -33,9 +54,17 @@ module AmplitudeExperiment
33
54
  # Generates a cookie string to set for the Amplitude Javascript SDK
34
55
  #
35
56
  # @param [String] device_id A device id to set
57
+ # @param [Boolean] new_format True if the cookie is in the Browser SDK 2.0 format
36
58
  # @return [String] A cookie string to set for the Amplitude Javascript SDK to read
37
- def self.generate(device_id)
38
- "#{device_id}.........."
59
+ def self.generate(device_id, new_format: false)
60
+ return "#{device_id}.........." unless new_format
61
+
62
+ user_session_hash = {
63
+ 'deviceId' => device_id
64
+ }
65
+ json_data = JSON.generate(user_session_hash)
66
+ encoded_json = URI.encode_www_form_component(json_data)
67
+ Base64.strict_encode64(encoded_json)
39
68
  end
40
69
  end
41
70
  end
@@ -1,3 +1,3 @@
1
1
  module AmplitudeExperiment
2
- VERSION = '1.2.2'.freeze
2
+ VERSION = '1.2.3'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: amplitude-experiment
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Amplitude
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-09-19 00:00:00.000000000 Z
11
+ date: 2023-09-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby