2222import time
2323import uuid
2424import six
25- from snowplow_tracker import payload , _version
25+ from snowplow_tracker import payload , _version , SelfDescribingJson
2626from snowplow_tracker import subject as _subject
2727from contracts import contract , new_contract
2828
@@ -55,6 +55,10 @@ class Tracker:
5555
5656 new_contract ("emitter" , lambda s : hasattr (s , "input" ))
5757
58+ new_contract ("self_describing_json" , lambda s : isinstance (s , SelfDescribingJson ))
59+
60+ new_contract ("context_array" , "list(self_describing_json)" )
61+
5862 @contract
5963 def __init__ (self , emitters , subject = None ,
6064 namespace = None , app_id = None , encode_base64 = DEFAULT_ENCODE_BASE64 ):
@@ -138,15 +142,16 @@ def complete_payload(self, pb, context, tstamp):
138142 :param pb: Payload builder
139143 :type pb: payload
140144 :param context: Custom context for the event
141- :type context: list(dict(string:*)) | None
145+ :type context: context_array | None
142146 :param tstamp: Optional user-provided timestamp for the event
143147 :type tstamp: int | float | None
144148 :rtype: tracker
145149 """
146150 pb .add ("eid" , Tracker .get_uuid ())
147151 pb .add ("dtm" , Tracker .get_timestamp (tstamp ))
148152 if context is not None :
149- context_envelope = {"schema" : CONTEXT_SCHEMA , "data" : context }
153+ context_jsons = list (map (lambda c : c .to_json (), context ))
154+ context_envelope = SelfDescribingJson (CONTEXT_SCHEMA , context_jsons ).to_json ()
150155 pb .add_json (context_envelope , self .encode_base64 , "cx" , "co" )
151156
152157 pb .add_dict (self .standard_nv_pairs )
@@ -165,7 +170,7 @@ def track_page_view(self, page_url, page_title=None, referrer=None, context=None
165170 :param referrer: Referrer of the page
166171 :type referrer: string_or_none
167172 :param context: Custom context for the event
168- :type context: list(dict(string:*)) | None
173+ :type context: context_array | None
169174 :rtype: tracker
170175 """
171176 pb = payload .Payload ()
@@ -200,7 +205,7 @@ def track_ecommerce_transaction_item(self, order_id, sku, price, quantity,
200205 :param currency: The currency the price is expressed in
201206 :type currency: string_or_none
202207 :param context: Custom context for the event
203- :type context: list(dict(string:*)) | None
208+ :type context: context_array | None
204209 :rtype: tracker
205210 """
206211 pb = payload .Payload ()
@@ -243,7 +248,7 @@ def track_ecommerce_transaction(self, order_id, total_value,
243248 :param items: The items in the transaction
244249 :type items: list(dict(str:*))
245250 :param context: Custom context for the event
246- :type context: list(dict(string:*)) | None
251+ :type context: context_array | None
247252 :rtype: tracker
248253 """
249254 pb = payload .Payload ()
@@ -278,7 +283,7 @@ def track_screen_view(self, name=None, id_=None, context=None, tstamp=None):
278283 :param id_: Screen view ID
279284 :type id_: string_or_none
280285 :param context: Custom context for the event
281- :type context: list(dict(string:*)) | None
286+ :type context: context_array | None
282287 :rtype: tracker
283288 """
284289 screen_view_properties = {}
@@ -287,10 +292,8 @@ def track_screen_view(self, name=None, id_=None, context=None, tstamp=None):
287292 if id_ is not None :
288293 screen_view_properties ["id" ] = id_
289294
290- event_json = {
291- "schema" : "%s/screen_view/%s/1-0-0" % (BASE_SCHEMA_PATH , SCHEMA_TAG ),
292- "data" : screen_view_properties
293- }
295+ event_json = SelfDescribingJson ("%s/screen_view/%s/1-0-0" % (BASE_SCHEMA_PATH , SCHEMA_TAG ), screen_view_properties )
296+
294297 return self .track_unstruct_event (event_json , context , tstamp )
295298
296299 @contract
@@ -311,7 +314,7 @@ def track_struct_event(self, category, action, label=None, property_=None, value
311314 :param value: A value associated with the user action
312315 :type value: int | float | None
313316 :param context: Custom context for the event
314- :type context: list(dict(string:*)) | None
317+ :type context: context_array | None
315318 :rtype: tracker
316319 """
317320 pb = payload .Payload ()
@@ -331,13 +334,13 @@ def track_unstruct_event(self, event_json, context=None, tstamp=None):
331334 A "data" field containing the event properties and
332335 A "schema" field identifying the schema against which the data is validated
333336
334- :type event_json: dict(string: string | dict)
337+ :type event_json: self_describing_json
335338 :param context: Custom context for the event
336- :type context: list(dict(string:*)) | None
339+ :type context: context_array | None
337340 :rtype: tracker
338341 """
339342
340- envelope = { "schema" : UNSTRUCT_EVENT_SCHEMA , "data" : event_json }
343+ envelope = SelfDescribingJson ( UNSTRUCT_EVENT_SCHEMA , event_json . to_json ()). to_json ()
341344
342345 pb = payload .Payload ()
343346
0 commit comments