1
+ import json
2
+ import logging
3
+ import urllib .parse
4
+
1
5
from .user import User
2
6
import base64
3
7
@@ -6,29 +10,52 @@ class AmplitudeCookie:
6
10
"""This class provides utility functions for parsing and handling identity from Amplitude cookies."""
7
11
8
12
@staticmethod
9
- def cookie_name (api_key : str ) -> str :
13
+ def cookie_name (api_key : str , new_format : bool = False ) -> str :
10
14
"""
11
15
Get the cookie name that Amplitude sets for the provided
12
16
Parameters:
13
17
api_key (str): The Amplitude API Key
18
+ new_format (bool): True if cookie is in Browser SDK 2.0 format
14
19
15
20
Returns:
16
21
The cookie name that Amplitude sets for the provided Amplitude API Key
17
22
"""
18
23
if not api_key :
19
24
raise ValueError ("Invalid Amplitude API Key" )
25
+
26
+ if new_format :
27
+ if len (api_key ) < 10 :
28
+ raise ValueError ("Invalid Amplitude API Key" )
29
+ return f"AMP_{ api_key [0 :10 ]} "
30
+
31
+ if len (api_key ) < 6 :
32
+ raise ValueError ("Invalid Amplitude API Key" )
20
33
return f"amp_{ api_key [0 :6 ]} "
21
34
22
35
@staticmethod
23
- def parse (amplitude_cookie : str ) -> User :
36
+ def parse (amplitude_cookie : str , new_format : bool = False ) -> User :
24
37
"""
25
38
Parse a cookie string and returns user
26
39
Parameters:
27
40
amplitude_cookie (str): A string from the amplitude cookie
41
+ new_format: True if cookie is in Browser SDK 2.0 format
28
42
29
43
Returns:
30
44
Experiment User context containing a device_id and user_id (if available)
31
45
"""
46
+
47
+ if new_format :
48
+ decoding = base64 .b64decode (amplitude_cookie ).decode ("utf-8" )
49
+ try :
50
+ user_session = json .loads (urllib .parse .unquote_plus (decoding ))
51
+ if "userId" not in user_session :
52
+ return User (user_id = None , device_id = user_session ["deviceId" ])
53
+ return User (user_id = user_session ["userId" ], device_id = user_session ["deviceId" ])
54
+ except Exception as e :
55
+ logger = logging .getLogger ("Amplitude" )
56
+ logger .error ("Error parsing the Amplitude cookie: " + str (e ))
57
+ return User ()
58
+
32
59
values = amplitude_cookie .split ('.' )
33
60
user_id = None
34
61
if values [1 ]:
@@ -39,13 +66,24 @@ def parse(amplitude_cookie: str) -> User:
39
66
return User (user_id = user_id , device_id = values [0 ])
40
67
41
68
@staticmethod
42
- def generate (device_id : str ) -> str :
69
+ def generate (device_id : str , new_format : bool = False ) -> str :
43
70
"""
44
71
Generates a cookie string to set for the Amplitude Javascript SDK
45
72
Parameters:
46
73
device_id (str): A device id to set
74
+ new_format: True if cookie is in Browser SDK 2.0 format
47
75
48
76
Returns:
49
77
A cookie string to set for the Amplitude Javascript SDK to read
50
78
"""
51
- return f"{ device_id } .........."
79
+ if not new_format :
80
+ return f"{ device_id } .........."
81
+
82
+ user_session_hash = {
83
+ "deviceId" : device_id
84
+ }
85
+ json_data = json .dumps (user_session_hash )
86
+ encoded_json = urllib .parse .quote (json_data )
87
+ base64_encoded = base64 .b64encode (bytearray (encoded_json , "utf-8" )).decode ("utf-8" )
88
+
89
+ return base64_encoded
0 commit comments