@@ -39,6 +39,8 @@ def main(
3939 order_id ,
4040 gclid ,
4141 ad_user_data_consent ,
42+ session_attributes_encoded = None ,
43+ session_attributes_dict = None ,
4244):
4345 """The main method that creates all necessary entities for the example.
4446
@@ -52,7 +54,19 @@ def main(
5254 gclid: The Google click ID for the click.
5355 ad_user_data_consent: The consent status for ad user data for all
5456 members in the job.
57+ session_attributes_encoded: a str token of encoded session atttributes.
58+ Only one of session_attributes_encoded or session_attributes_dict
59+ should be passed.
60+ session_attributes_dict: a dict[str, str] of session attribute
61+ key value pairs. Only one of session_attributes_encoded or
62+ session_attributes_dict should be passed.
5563 """
64+ if session_attributes_encoded and session_attributes_dict :
65+ raise ValueError (
66+ "Only one of 'session_attributes_encoded' or "
67+ "'session_attributes_dict' can be set."
68+ )
69+
5670 # [START add_user_identifiers]
5771 # Extract user email and phone from the raw data, normalize and hash it,
5872 # then wrap it in UserIdentifier objects. Create a separate UserIdentifier
@@ -146,7 +160,22 @@ def main(
146160 click_conversion .consent .ad_user_data = client .enums .ConsentStatusEnum [
147161 raw_record ["ad_user_data_consent" ]
148162 ]
149- # [END add_conversion_details]
163+
164+ # [START add_session_attributes]
165+ # Set one of the session_attributes_encoded or
166+ # session_attributes_key_value_pairs fields if either are provided.
167+ if session_attributes_encoded :
168+ click_conversion .session_attributes_encoded = session_attributes_encoded
169+ elif session_attributes_dict :
170+ for key , value in session_attributes_dict .items ():
171+ pair = client .get_type ("SessionAttributeKeyValuePair" )
172+ pair .session_attribute_key = key
173+ pair .session_attribute_value = value
174+ click_conversion .session_attributes_key_value_pairs .key_value_pairs .append (
175+ pair
176+ )
177+ # [END add_session_attributes]
178+ # [END add_conversion_details]
150179
151180 # [START upload_conversion]
152181 # Creates the conversion upload service client.
@@ -230,6 +259,10 @@ def normalize_and_hash(s):
230259
231260
232261if __name__ == "__main__" :
262+ # GoogleAdsClient will read the google-ads.yaml configuration file in the
263+ # home directory if none is specified.
264+ googleads_client = GoogleAdsClient .load_from_storage (version = "v19" )
265+
233266 parser = argparse .ArgumentParser (
234267 description = "Imports offline call conversion values for calls related "
235268 "to your ads."
@@ -278,7 +311,7 @@ def normalize_and_hash(s):
278311 help = "the Google click ID (gclid) for the click." ,
279312 )
280313 parser .add_argument (
281- "-d " ,
314+ "-n " ,
282315 "--ad_user_data_consent" ,
283316 type = str ,
284317 choices = [e .name for e in googleads_client .enums .ConsentStatusEnum ],
@@ -287,11 +320,35 @@ def normalize_and_hash(s):
287320 "the job."
288321 ),
289322 )
323+ # A mutually exclusive group means that session_attributes_encoded and
324+ # session_attributes_key_value_pairs cannot be passed in at the same time.
325+ session_attributes = parser .add_mutually_exclusive_group ()
326+ session_attributes .add_argument (
327+ "-e" ,
328+ "--session_attributes_encoded" ,
329+ type = str ,
330+ default = None ,
331+ help = ("A session attributes token." ),
332+ )
333+ session_attributes .add_argument (
334+ "-k" ,
335+ "--session_attributes_key_value_pairs" ,
336+ nargs = "+" ,
337+ type = str ,
338+ default = None ,
339+ help = (
340+ "A space-delimited list of session attribute key value pairs. Each "
341+ "pair should be separated by an equal sign, for example: "
342+ "'-k gad_campaignid=12345 gad_source=1'"
343+ ),
344+ )
290345 args = parser .parse_args ()
291346
292- # GoogleAdsClient will read the google-ads.yaml configuration file in the
293- # home directory if none is specified.
294- googleads_client = GoogleAdsClient .load_from_storage (version = "v19" )
347+ if args .session_attributes_key_value_pairs :
348+ # Convert the string-based input to a dict
349+ session_attributes_dict = dict (
350+ pair .split ("=" ) for pair in args .session_attributes_key_value_pairs
351+ )
295352
296353 try :
297354 main (
@@ -303,6 +360,11 @@ def normalize_and_hash(s):
303360 args .order_id ,
304361 args .gclid ,
305362 args .ad_user_data_consent ,
363+ # Only one of 'session_attributes_encoded' or
364+ # 'session_attributes_dict' can be passed at a time. If both are
365+ # passed the example will fail with a ValueError.
366+ session_attributes_encoded = args .session_attributes_encoded ,
367+ session_attributes_dict = session_attributes_dict ,
306368 )
307369 except GoogleAdsException as ex :
308370 print (
0 commit comments