amplitude-experiment 1.2.1 → 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: f4b95fab3639837e3bb1223ad3ba71405d9f92384fa73dedca9daa83b8069dac
4
- data.tar.gz: 3fdb770c922f5e82260cac4f53c1134555b85a3fb5ad91f5fd20956d3a42d300
3
+ metadata.gz: be0a173d9b7f4a54d8a800741ff6801d1ba62a64f33b8e8539be84ab5f91303d
4
+ data.tar.gz: 4b7afd4ffec350c766f0001c9b6a0a01910d2bba780f034c64dbdcdeaa05b702
5
5
  SHA512:
6
- metadata.gz: 5e3ba791304dc10b7d27d34806d3a6b85b195a86035a0f5656410b0e541280fbf82053b06b8f1a13dd2bd8966d2cfa390b723733d73ce138cc8c4542d5431d29
7
- data.tar.gz: 5eb707da5065fd9c8399d42967aa02c2f1705e944033e346579a01c4e66fe591c77cbfd812a0f6d1267be25995993a150014334727f80005f0114439a1562c30
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
@@ -6,6 +6,8 @@ module AmplitudeExperiment
6
6
  end
7
7
 
8
8
  def should_track(assignment)
9
+ return false if assignment.results.empty?
10
+
9
11
  canonical_assignment = assignment.canonicalize
10
12
  track = @cache.get(canonical_assignment).nil?
11
13
  @cache.put(canonical_assignment, 0) if track
@@ -1,3 +1,3 @@
1
1
  module AmplitudeExperiment
2
- VERSION = '1.2.1'.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.1
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-13 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