2222import time
2323import uuid
2424import six
25+
26+ from contracts import contract , new_contract
27+
2528from snowplow_tracker import payload , _version , SelfDescribingJson
2629from snowplow_tracker import subject as _subject
27- from contracts import contract , new_contract
30+ from snowplow_tracker . timestamp import Timestamp , TrueTimestamp , DeviceTimestamp
2831
2932
3033"""
4952Tracker class
5053"""
5154
55+
5256class Tracker :
5357
5458 new_contract ("not_none" , lambda s : s is not None )
@@ -128,7 +132,7 @@ def get_timestamp(tstamp=None):
128132 """
129133 if tstamp is None :
130134 return int (time .time () * 1000 )
131- elif isinstance (tstamp , (int , float )):
135+ elif isinstance (tstamp , (int , float , )):
132136 return int (tstamp )
133137
134138
@@ -160,11 +164,18 @@ def complete_payload(self, pb, context, tstamp):
160164 :param context: Custom context for the event
161165 :type context: context_array | None
162166 :param tstamp: Optional user-provided timestamp for the event
163- :type tstamp: int | float | None
167+ :type tstamp: timestamp | int | float | None
164168 :rtype: tracker
165169 """
166170 pb .add ("eid" , Tracker .get_uuid ())
167- pb .add ("dtm" , Tracker .get_timestamp (tstamp ))
171+
172+ if isinstance (tstamp , TrueTimestamp ):
173+ pb .add ("ttm" , tstamp .value )
174+ if isinstance (tstamp , DeviceTimestamp ):
175+ pb .add ("dtm" , Tracker .get_timestamp (tstamp .value ))
176+ elif isinstance (tstamp , (int , float , type (None ))):
177+ pb .add ("dtm" , Tracker .get_timestamp (tstamp ))
178+
168179 if context is not None :
169180 context_jsons = list (map (lambda c : c .to_json (), context ))
170181 context_envelope = SelfDescribingJson (CONTEXT_SCHEMA , context_jsons ).to_json ()
@@ -187,6 +198,8 @@ def track_page_view(self, page_url, page_title=None, referrer=None, context=None
187198 :type referrer: string_or_none
188199 :param context: Custom context for the event
189200 :type context: context_array | None
201+ :param tstamp: Optional user-provided timestamp for the event
202+ :type tstamp: timestamp | int | float | None
190203 :rtype: tracker
191204 """
192205 pb = payload .Payload ()
@@ -217,7 +230,7 @@ def track_page_ping(self, page_url, page_title=None, referrer=None, min_x=None,
217230 :param context: Custom context for the event
218231 :type context: context_array | None
219232 :param tstamp: Optional user-provided timestamp for the event
220- :type tstamp: int | float | None
233+ :type tstamp: timestamp | int | float | None
221234 :rtype: tracker
222235 """
223236 pb = payload .Payload ()
@@ -248,7 +261,7 @@ def track_link_click(self, target_url, element_id=None,
248261 :param context: Custom context for the event
249262 :type context: context_array | None
250263 :param tstamp: Optional user-provided timestamp for the event
251- :type tstamp: int | float | None
264+ :type tstamp: timestamp | int | float | None
252265 :rtype: tracker
253266 """
254267 properties = {}
@@ -286,7 +299,7 @@ def track_add_to_cart(self, sku, quantity, name=None, category=None,
286299 :param context: Custom context for the event
287300 :type context: context_array | None
288301 :param tstamp: Optional user-provided timestamp for the event
289- :type tstamp: int | float | None
302+ :type tstamp: timestamp | int | float | None
290303 :rtype: tracker
291304 """
292305 properties = {}
@@ -325,7 +338,7 @@ def track_remove_from_cart(self, sku, quantity, name=None, category=None,
325338 :param context: Custom context for the event
326339 :type context: context_array | None
327340 :param tstamp: Optional user-provided timestamp for the event
328- :type tstamp: int | float | None
341+ :type tstamp: timestamp | int | float | None
329342 :rtype: tracker
330343 """
331344 properties = {}
@@ -363,7 +376,7 @@ def track_form_change(self, form_id, element_id, node_name, value, type_=None,
363376 :param context: Custom context for the event
364377 :type context: context_array | None
365378 :param tstamp: Optional user-provided timestamp for the event
366- :type tstamp: int | float | None
379+ :type tstamp: timestamp | int | float | None
367380 :rtype: tracker
368381 """
369382 properties = dict ()
@@ -393,7 +406,7 @@ def track_form_submit(self, form_id, form_classes=None, elements=None,
393406 :param context: Custom context for the event
394407 :type context: context_array | None
395408 :param tstamp: Optional user-provided timestamp for the event
396- :type tstamp: int | float | None
409+ :type tstamp: timestamp | int | float | None
397410 :rtype: tracker
398411 """
399412
@@ -423,7 +436,7 @@ def track_site_search(self, terms, filters=None, total_results=None,
423436 :param context: Custom context for the event
424437 :type context: context_array | None
425438 :param tstamp: Optional user-provided timestamp for the event
426- :type tstamp: int | float | None
439+ :type tstamp: timestamp | int | float | None
427440 :rtype: tracker
428441 """
429442 properties = {}
@@ -595,7 +608,7 @@ def track_unstruct_event(self, event_json, context=None, tstamp=None):
595608 :param context: Custom context for the event
596609 :type context: context_array | None
597610 :param tstamp: User-set timestamp
598- :type tstamp: int | None
611+ :type tstamp: timestamp | int | None
599612 :rtype: tracker
600613 """
601614
0 commit comments