-
Notifications
You must be signed in to change notification settings - Fork 64
/
tracker.py
1040 lines (956 loc) · 37.5 KB
/
tracker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# """
# tracker.py
# Copyright (c) 2013-2023 Snowplow Analytics Ltd. All rights reserved.
# This program is licensed to you under the Apache License Version 2.0,
# and you may not use this file except in compliance with the Apache License
# Version 2.0. You may obtain a copy of the Apache License Version 2.0 at
# http://www.apache.org/licenses/LICENSE-2.0.
# Unless required by applicable law or agreed to in writing,
# software distributed under the Apache License Version 2.0 is distributed on
# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
# express or implied. See the Apache License Version 2.0 for the specific
# language governing permissions and limitations there under.
# """
import time
import uuid
from typing import Any, Optional, Union, List, Dict, Sequence
from warnings import warn
from snowplow_tracker import payload, SelfDescribingJson
from snowplow_tracker.subject import Subject
from snowplow_tracker.contracts import non_empty_string, one_of, non_empty, form_element
from snowplow_tracker.constants import (
VERSION,
DEFAULT_ENCODE_BASE64,
BASE_SCHEMA_PATH,
SCHEMA_TAG,
)
from snowplow_tracker.events import (
Event,
PagePing,
PageView,
SelfDescribing,
StructuredEvent,
ScreenView,
)
from snowplow_tracker.typing import (
JsonEncoderFunction,
EmitterProtocol,
FORM_NODE_NAMES,
FORM_TYPES,
FormNodeName,
ElementClasses,
FormClasses,
)
"""
Tracker class
"""
class Tracker:
def __init__(
self,
namespace: str,
emitters: Union[List[EmitterProtocol], EmitterProtocol],
subject: Optional[Subject] = None,
app_id: Optional[str] = None,
encode_base64: bool = DEFAULT_ENCODE_BASE64,
json_encoder: Optional[JsonEncoderFunction] = None,
) -> None:
"""
:param namespace: Identifier for the Tracker instance
:type namespace: string
:param emitters: Emitters to which events will be sent
:type emitters: list[>0](emitter) | emitter
:param subject: Subject to be tracked
:type subject: subject | None
:param app_id: Application ID
:type app_id: string_or_none
:param encode_base64: Whether JSONs in the payload should be base-64 encoded
:type encode_base64: bool
:param json_encoder: Custom JSON serializer that gets called on non-serializable object
:type json_encoder: function | None
"""
if subject is None:
subject = Subject()
if type(emitters) is list:
non_empty(emitters)
self.emitters = emitters
else:
self.emitters = [emitters]
self.subject = subject
self.encode_base64 = encode_base64
self.json_encoder = json_encoder
self.standard_nv_pairs = {"tv": VERSION, "tna": namespace, "aid": app_id}
self.timer = None
@staticmethod
def get_uuid() -> str:
"""
Set transaction ID for the payload once during the lifetime of the
event.
:rtype: string
"""
return str(uuid.uuid4())
@staticmethod
def get_timestamp(tstamp: Optional[float] = None) -> int:
"""
:param tstamp: User-input timestamp or None
:type tstamp: int | float | None
:rtype: int
"""
if isinstance(
tstamp,
(
int,
float,
),
):
return int(tstamp)
return int(time.time() * 1000)
"""
Tracking methods
"""
def track(
self,
event: Event,
) -> Optional[str]:
"""
Send the event payload to a emitter. Returns the tracked event ID.
:param event: Event
:type event: events.Event
:rtype: String
"""
payload = self.complete_payload(
event=event,
)
for emitter in self.emitters:
emitter.input(payload.nv_pairs)
if "eid" in payload.nv_pairs.keys():
return payload.nv_pairs["eid"]
def complete_payload(
self,
event: Event,
) -> payload.Payload:
payload = event.build_payload(
encode_base64=self.encode_base64,
json_encoder=self.json_encoder,
subject=self.subject,
)
payload.add("eid", Tracker.get_uuid())
payload.add("dtm", Tracker.get_timestamp())
payload.add_dict(self.standard_nv_pairs)
return payload
def track_page_view(
self,
page_url: str,
page_title: Optional[str] = None,
referrer: Optional[str] = None,
context: Optional[List[SelfDescribingJson]] = None,
tstamp: Optional[float] = None,
event_subject: Optional[Subject] = None,
) -> "Tracker":
"""
:param page_url: URL of the viewed page
:type page_url: non_empty_string
:param page_title: Title of the viewed page
:type page_title: string_or_none
:param referrer: Referrer of the page
:type referrer: string_or_none
:param context: Custom context for the event
:type context: context_array | None
:param tstamp: Optional event timestamp in milliseconds
:type tstamp: int | float | None
:param event_subject: Optional per event subject
:type event_subject: subject | None
:rtype: Tracker
"""
warn(
"track_page_view will be removed in future versions. Please use the new PageView class to track the event.",
DeprecationWarning,
stacklevel=2,
)
pv = PageView(
page_url=page_url,
page_title=page_title,
referrer=referrer,
event_subject=event_subject,
context=context,
true_timestamp=tstamp,
)
self.track(event=pv)
return self
def track_page_ping(
self,
page_url: str,
page_title: Optional[str] = None,
referrer: Optional[str] = None,
min_x: Optional[int] = None,
max_x: Optional[int] = None,
min_y: Optional[int] = None,
max_y: Optional[int] = None,
context: Optional[List[SelfDescribingJson]] = None,
tstamp: Optional[float] = None,
event_subject: Optional[Subject] = None,
) -> "Tracker":
"""
:param page_url: URL of the viewed page
:type page_url: non_empty_string
:param page_title: Title of the viewed page
:type page_title: string_or_none
:param referrer: Referrer of the page
:type referrer: string_or_none
:param min_x: Minimum page x offset seen in the last ping period
:type min_x: int | None
:param max_x: Maximum page x offset seen in the last ping period
:type max_x: int | None
:param min_y: Minimum page y offset seen in the last ping period
:type min_y: int | None
:param max_y: Maximum page y offset seen in the last ping period
:type max_y: int | None
:param context: Custom context for the event
:type context: context_array | None
:param tstamp: Optional event timestamp in milliseconds
:type tstamp: int | float | None
:param event_subject: Optional per event subject
:type event_subject: subject | None
:rtype: Tracker
"""
warn(
"track_page_ping will be removed in future versions. Please use the new PagePing class to track the event.",
DeprecationWarning,
stacklevel=2,
)
pp = PagePing(
page_url=page_url,
page_title=page_title,
referrer=referrer,
min_x=min_x,
max_x=max_x,
min_y=min_y,
max_y=max_y,
context=context,
true_timestamp=tstamp,
event_subject=event_subject,
)
self.track(event=pp)
return self
def track_link_click(
self,
target_url: str,
element_id: Optional[str] = None,
element_classes: Optional[ElementClasses] = None,
element_target: Optional[str] = None,
element_content: Optional[str] = None,
context: Optional[List[SelfDescribingJson]] = None,
tstamp: Optional[float] = None,
event_subject: Optional[Subject] = None,
) -> "Tracker":
"""
:param target_url: Target URL of the link
:type target_url: non_empty_string
:param element_id: ID attribute of the HTML element
:type element_id: string_or_none
:param element_classes: Classes of the HTML element
:type element_classes: list(str) | tuple(str,\\*) | None
:param element_target: ID attribute of the HTML element
:type element_target: string_or_none
:param element_content: The content of the HTML element
:type element_content: string_or_none
:param context: Custom context for the event
:type context: context_array | None
:param tstamp: Optional event timestamp in milliseconds
:type tstamp: int | float | None
:param event_subject: Optional per event subject
:type event_subject: subject | None
:rtype: Tracker
"""
warn(
"track_link_click will be removed in future versions. Please use the new SelfDescribing class to track the event.",
DeprecationWarning,
stacklevel=2,
)
non_empty_string(target_url)
properties = {}
properties["targetUrl"] = target_url
if element_id is not None:
properties["elementId"] = element_id
if element_classes is not None:
properties["elementClasses"] = element_classes
if element_target is not None:
properties["elementTarget"] = element_target
if element_content is not None:
properties["elementContent"] = element_content
event_json = SelfDescribingJson(
"%s/link_click/%s/1-0-1" % (BASE_SCHEMA_PATH, SCHEMA_TAG), properties
)
self.track_self_describing_event(
event_json=event_json,
context=context,
true_timestamp=tstamp,
event_subject=event_subject,
)
return self
def track_add_to_cart(
self,
sku: str,
quantity: int,
name: Optional[str] = None,
category: Optional[str] = None,
unit_price: Optional[float] = None,
currency: Optional[str] = None,
context: Optional[List[SelfDescribingJson]] = None,
tstamp: Optional[float] = None,
event_subject: Optional[Subject] = None,
) -> "Tracker":
"""
:param sku: Item SKU or ID
:type sku: non_empty_string
:param quantity: Number added to cart
:type quantity: int
:param name: Item's name
:type name: string_or_none
:param category: Item's category
:type category: string_or_none
:param unit_price: Item's price
:type unit_price: int | float | None
:param currency: Type of currency the price is in
:type currency: string_or_none
:param context: Custom context for the event
:type context: context_array | None
:param tstamp: Optional event timestamp in milliseconds
:type tstamp: int | float | None
:param event_subject: Optional per event subject
:type event_subject: subject | None
:rtype: Tracker
"""
warn(
"track_add_to_cart will be deprecated in future versions.",
DeprecationWarning,
stacklevel=2,
)
non_empty_string(sku)
properties = {}
properties["sku"] = sku
properties["quantity"] = quantity
if name is not None:
properties["name"] = name
if category is not None:
properties["category"] = category
if unit_price is not None:
properties["unitPrice"] = unit_price
if currency is not None:
properties["currency"] = currency
event_json = SelfDescribingJson(
"%s/add_to_cart/%s/1-0-0" % (BASE_SCHEMA_PATH, SCHEMA_TAG), properties
)
self.track_self_describing_event(
event_json=event_json,
context=context,
true_timestamp=tstamp,
event_subject=event_subject,
)
return self
def track_remove_from_cart(
self,
sku: str,
quantity: int,
name: Optional[str] = None,
category: Optional[str] = None,
unit_price: Optional[float] = None,
currency: Optional[str] = None,
context: Optional[List[SelfDescribingJson]] = None,
tstamp: Optional[float] = None,
event_subject: Optional[Subject] = None,
) -> "Tracker":
"""
:param sku: Item SKU or ID
:type sku: non_empty_string
:param quantity: Number added to cart
:type quantity: int
:param name: Item's name
:type name: string_or_none
:param category: Item's category
:type category: string_or_none
:param unit_price: Item's price
:type unit_price: int | float | None
:param currency: Type of currency the price is in
:type currency: string_or_none
:param context: Custom context for the event
:type context: context_array | None
:param tstamp: Optional event timestamp in milliseconds
:type tstamp: int | float | None
:param event_subject: Optional per event subject
:type event_subject: subject | None
:rtype: Tracker
"""
warn(
"track_remove_from_cart will be deprecated in future versions.",
DeprecationWarning,
stacklevel=2,
)
non_empty_string(sku)
properties = {}
properties["sku"] = sku
properties["quantity"] = quantity
if name is not None:
properties["name"] = name
if category is not None:
properties["category"] = category
if unit_price is not None:
properties["unitPrice"] = unit_price
if currency is not None:
properties["currency"] = currency
event_json = SelfDescribingJson(
"%s/remove_from_cart/%s/1-0-0" % (BASE_SCHEMA_PATH, SCHEMA_TAG), properties
)
self.track_self_describing_event(
event_json=event_json,
context=context,
true_timestamp=tstamp,
event_subject=event_subject,
)
return self
def track_form_change(
self,
form_id: str,
element_id: Optional[str],
node_name: FormNodeName,
value: Optional[str],
type_: Optional[str] = None,
element_classes: Optional[ElementClasses] = None,
context: Optional[List[SelfDescribingJson]] = None,
tstamp: Optional[float] = None,
event_subject: Optional[Subject] = None,
) -> "Tracker":
"""
:param form_id: ID attribute of the HTML form
:type form_id: non_empty_string
:param element_id: ID attribute of the HTML element
:type element_id: string_or_none
:param node_name: Type of input element
:type node_name: form_node_name
:param value: Value of the input element
:type value: string_or_none
:param type_: Type of data the element represents
:type type_: non_empty_string, form_type
:param element_classes: Classes of the HTML element
:type element_classes: list(str) | tuple(str,\\*) | None
:param context: Custom context for the event
:type context: context_array | None
:param tstamp: Optional event timestamp in milliseconds
:type tstamp: int | float | None
:param event_subject: Optional per event subject
:type event_subject: subject | None
:rtype: Tracker
"""
warn(
"track_form_change will be removed in future versions. Please use the new SelfDescribing class to track the event.",
DeprecationWarning,
stacklevel=2,
)
non_empty_string(form_id)
one_of(node_name, FORM_NODE_NAMES)
if type_ is not None:
one_of(type_.lower(), FORM_TYPES)
properties = dict()
properties["formId"] = form_id
properties["elementId"] = element_id
properties["nodeName"] = node_name
properties["value"] = value
if type_ is not None:
properties["type"] = type_
if element_classes is not None:
properties["elementClasses"] = element_classes
event_json = SelfDescribingJson(
"%s/change_form/%s/1-0-0" % (BASE_SCHEMA_PATH, SCHEMA_TAG), properties
)
self.track_self_describing_event(
event_json=event_json,
context=context,
true_timestamp=tstamp,
event_subject=event_subject,
)
return self
def track_form_submit(
self,
form_id: str,
form_classes: Optional[FormClasses] = None,
elements: Optional[List[Dict[str, Any]]] = None,
context: Optional[List[SelfDescribingJson]] = None,
tstamp: Optional[float] = None,
event_subject: Optional[Subject] = None,
) -> "Tracker":
"""
:param form_id: ID attribute of the HTML form
:type form_id: non_empty_string
:param form_classes: Classes of the HTML form
:type form_classes: list(str) | tuple(str,\\*) | None
:param elements: Classes of the HTML form
:type elements: list(form_element) | None
:param context: Custom context for the event
:type context: context_array | None
:param tstamp: Optional event timestamp in milliseconds
:type tstamp: int | float | None
:param event_subject: Optional per event subject
:type event_subject: subject | None
:rtype: Tracker
"""
warn(
"track_form_submit will be removed in future versions. Please use the new SelfDescribing class to track the event.",
DeprecationWarning,
stacklevel=2,
)
non_empty_string(form_id)
for element in elements or []:
form_element(element)
properties = dict()
properties["formId"] = form_id
if form_classes is not None:
properties["formClasses"] = form_classes
if elements is not None and len(elements) > 0:
properties["elements"] = elements
event_json = SelfDescribingJson(
"%s/submit_form/%s/1-0-0" % (BASE_SCHEMA_PATH, SCHEMA_TAG), properties
)
self.track_self_describing_event(
event_json=event_json,
context=context,
true_timestamp=tstamp,
event_subject=event_subject,
)
return self
def track_site_search(
self,
terms: Sequence[str],
filters: Optional[Dict[str, Union[str, bool]]] = None,
total_results: Optional[int] = None,
page_results: Optional[int] = None,
context: Optional[List[SelfDescribingJson]] = None,
tstamp: Optional[float] = None,
event_subject: Optional[Subject] = None,
) -> "Tracker":
"""
:param terms: Search terms
:type terms: seq[>=1](str)
:param filters: Filters applied to the search
:type filters: dict(str:str|bool) | None
:param total_results: Total number of results returned
:type total_results: int | None
:param page_results: Total number of pages of results
:type page_results: int | None
:param context: Custom context for the event
:type context: context_array | None
:param tstamp: Optional event timestamp in milliseconds
:type tstamp: int | float | None
:param event_subject: Optional per event subject
:type event_subject: subject | None
:rtype: Tracker
"""
warn(
"track_site_search will be removed in future versions. Please use the new SelfDescribing class to track the event.",
DeprecationWarning,
stacklevel=2,
)
non_empty(terms)
properties = {}
properties["terms"] = terms
if filters is not None:
properties["filters"] = filters
if total_results is not None:
properties["totalResults"] = total_results
if page_results is not None:
properties["pageResults"] = page_results
event_json = SelfDescribingJson(
"%s/site_search/%s/1-0-0" % (BASE_SCHEMA_PATH, SCHEMA_TAG), properties
)
self.track_self_describing_event(
event_json=event_json,
context=context,
true_timestamp=tstamp,
event_subject=event_subject,
)
return self
def track_ecommerce_transaction_item(
self,
order_id: str,
sku: str,
price: float,
quantity: int,
name: Optional[str] = None,
category: Optional[str] = None,
currency: Optional[str] = None,
context: Optional[List[SelfDescribingJson]] = None,
tstamp: Optional[float] = None,
event_subject: Optional[Subject] = None,
) -> "Tracker":
"""
This is an internal method called by track_ecommerce_transaction.
It is not for public use.
:param order_id: Order ID
:type order_id: non_empty_string
:param sku: Item SKU
:type sku: non_empty_string
:param price: Item price
:type price: int | float
:param quantity: Item quantity
:type quantity: int
:param name: Item name
:type name: string_or_none
:param category: Item category
:type category: string_or_none
:param currency: The currency the price is expressed in
:type currency: string_or_none
:param context: Custom context for the event
:type context: context_array | None
:param tstamp: Optional event timestamp in milliseconds
:type tstamp: int | float | None
:param event_subject: Optional per event subject
:type event_subject: subject | None
:rtype: Tracker
"""
warn(
"track_ecommerce_transaction_item will be deprecated in future versions.",
DeprecationWarning,
stacklevel=2,
)
non_empty_string(order_id)
non_empty_string(sku)
event = Event(
event_subject=event_subject, context=context, true_timestamp=tstamp
)
event.payload.add("e", "ti")
event.payload.add("ti_id", order_id)
event.payload.add("ti_sk", sku)
event.payload.add("ti_nm", name)
event.payload.add("ti_ca", category)
event.payload.add("ti_pr", price)
event.payload.add("ti_qu", quantity)
event.payload.add("ti_cu", currency)
self.track(event=event)
return self
def track_ecommerce_transaction(
self,
order_id: str,
total_value: float,
affiliation: Optional[str] = None,
tax_value: Optional[float] = None,
shipping: Optional[float] = None,
city: Optional[str] = None,
state: Optional[str] = None,
country: Optional[str] = None,
currency: Optional[str] = None,
items: Optional[List[Dict[str, Any]]] = None,
context: Optional[List[SelfDescribingJson]] = None,
tstamp: Optional[float] = None,
event_subject: Optional[Subject] = None,
) -> "Tracker":
"""
:param order_id: ID of the eCommerce transaction
:type order_id: non_empty_string
:param total_value: Total transaction value
:type total_value: int | float
:param affiliation: Transaction affiliation
:type affiliation: string_or_none
:param tax_value: Transaction tax value
:type tax_value: int | float | None
:param shipping: Delivery cost charged
:type shipping: int | float | None
:param city: Delivery address city
:type city: string_or_none
:param state: Delivery address state
:type state: string_or_none
:param country: Delivery address country
:type country: string_or_none
:param currency: The currency the price is expressed in
:type currency: string_or_none
:param items: The items in the transaction
:type items: list(dict(str:\\*)) | None
:param context: Custom context for the event
:type context: context_array | None
:param tstamp: Optional event timestamp in milliseconds
:type tstamp: int | float | None
:param event_subject: Optional per event subject
:type event_subject: subject | None
:rtype: Tracker
"""
warn(
"track_ecommerce_transaction will be deprecated in future versions.",
DeprecationWarning,
stacklevel=2,
)
non_empty_string(order_id)
event = Event(
event_subject=event_subject, context=context, true_timestamp=tstamp
)
event.payload.add("e", "tr")
event.payload.add("tr_id", order_id)
event.payload.add("tr_tt", total_value)
event.payload.add("tr_af", affiliation)
event.payload.add("tr_tx", tax_value)
event.payload.add("tr_sh", shipping)
event.payload.add("tr_ci", city)
event.payload.add("tr_st", state)
event.payload.add("tr_co", country)
event.payload.add("tr_cu", currency)
tstamp = Tracker.get_timestamp(tstamp)
self.track(event=event)
if items is None:
items = []
for item in items:
item["order_id"] = order_id
item["currency"] = currency
item["tstamp"] = tstamp
item["event_subject"] = event_subject
item["context"] = context
self.track_ecommerce_transaction_item(**item)
return self
def track_screen_view(
self,
name: Optional[str] = None,
id_: Optional[str] = None,
context: Optional[List[SelfDescribingJson]] = None,
tstamp: Optional[float] = None,
event_subject: Optional[Subject] = None,
) -> "Tracker":
"""
:param name: The name of the screen view event
:type name: string_or_none
:param id_: Screen view ID
:type id_: string_or_none
:param context: Custom context for the event
:type context: context_array | None
:param tstamp: Optional event timestamp in milliseconds
:type tstamp: int | float | None
:param event_subject: Optional per event subject
:type event_subject: subject | None
:rtype: Tracker
"""
warn(
"track_screen_view will be removed in future versions. Please use the new ScreenView class to track the event.",
DeprecationWarning,
stacklevel=2,
)
screen_view_properties = {}
if name is not None:
screen_view_properties["name"] = name
if id_ is not None:
screen_view_properties["id"] = id_
event_json = SelfDescribingJson(
"%s/screen_view/%s/1-0-0" % (BASE_SCHEMA_PATH, SCHEMA_TAG),
screen_view_properties,
)
self.track_self_describing_event(
event_json=event_json,
context=context,
true_timestamp=tstamp,
event_subject=event_subject,
)
return self
def track_mobile_screen_view(
self,
name: str,
id_: Optional[str] = None,
type: Optional[str] = None,
previous_name: Optional[str] = None,
previous_id: Optional[str] = None,
previous_type: Optional[str] = None,
transition_type: Optional[str] = None,
context: Optional[List[SelfDescribingJson]] = None,
tstamp: Optional[float] = None,
event_subject: Optional[Subject] = None,
) -> "Tracker":
"""
:param name: The name of the screen view event
:type name: string_or_none
:param id_: Screen view ID. This must be of type UUID.
:type id_: string | None
:param type: The type of screen that was viewed e.g feed / carousel.
:type type: string | None
:param previous_name: The name of the previous screen.
:type previous_name: string | None
:param previous_id: The screenview ID of the previous screenview.
:type previous_id: string | None
:param previous_type The screen type of the previous screenview
:type previous_type string | None
:param transition_type The type of transition that led to the screen being viewed.
:type transition_type string | None
:param context: Custom context for the event
:type context: context_array | None
:param tstamp: Optional event timestamp in milliseconds
:type tstamp: int | float | None
:param event_subject: Optional per event subject
:type event_subject: subject | None
:rtype: Tracker
"""
warn(
"track_mobile_screen_view will be removed in future versions. Please use the new ScreenView class to track the event.",
DeprecationWarning,
stacklevel=2,
)
if id_ is None:
id_ = self.get_uuid()
sv = ScreenView(
name=name,
id_=id_,
type=type,
previous_name=previous_name,
previous_id=previous_id,
previous_type=previous_type,
transition_type=transition_type,
event_subject=event_subject,
context=context,
true_timestamp=tstamp,
)
self.track(event=sv)
return self
def track_struct_event(
self,
category: str,
action: str,
label: Optional[str] = None,
property_: Optional[str] = None,
value: Optional[float] = None,
context: Optional[List[SelfDescribingJson]] = None,
tstamp: Optional[float] = None,
event_subject: Optional[Subject] = None,
) -> "Tracker":
"""
:param category: Category of the event
:type category: non_empty_string
:param action: The event itself
:type action: non_empty_string
:param label: Refer to the object the action is
performed on
:type label: string_or_none
:param property_: Property associated with either the action
or the object
:type property_: string_or_none
:param value: A value associated with the user action
:type value: int | float | None
:param context: Custom context for the event
:type context: context_array | None
:param tstamp: Optional event timestamp in milliseconds
:type tstamp: int | float | None
:param event_subject: Optional per event subject
:type event_subject: subject | None
:rtype: Tracker
"""
warn(
"track_struct_event will be removed in future versions. Please use the new Structured class to track the event.",
DeprecationWarning,
stacklevel=2,
)
se = StructuredEvent(
category=category,
action=action,
label=label,
property_=property_,
value=value,
context=context,
true_timestamp=tstamp,
event_subject=event_subject,
)
self.track(
event=se,
)
return self
def track_self_describing_event(
self,
event_json: SelfDescribingJson,
context: Optional[List[SelfDescribingJson]] = None,
tstamp: Optional[float] = None,
event_subject: Optional[Subject] = None,
) -> "Tracker":
"""
:param event_json: The properties of the event. Has two field:
A "data" field containing the event properties and
A "schema" field identifying the schema against which the data is validated
:type event_json: self_describing_json
:param context: Custom context for the event
:type context: context_array | None
:param tstamp: Optional event timestamp in milliseconds
:type tstamp: int | float | None
:param event_subject: Optional per event subject
:type event_subject: subject | None
:rtype: Tracker
"""
warn(
"track_self_describing_event will be removed in future versions. Please use the new SelfDescribing class to track the event.",
DeprecationWarning,
stacklevel=2,
)
sd = SelfDescribing(
event_json=event_json,
context=context,
true_timestamp=tstamp,
event_subject=event_subject,
)
self.track(
event=sd,
)
return self
# Alias
def track_unstruct_event(
self,
event_json: SelfDescribingJson,
context: Optional[List[SelfDescribingJson]] = None,
tstamp: Optional[float] = None,
event_subject: Optional[Subject] = None,
) -> "Tracker":
"""
:param event_json: The properties of the event. Has two field:
A "data" field containing the event properties and
A "schema" field identifying the schema against which the data is validated
:type event_json: self_describing_json
:param context: Custom context for the event
:type context: context_array | None
:param tstamp: Optional event timestamp in milliseconds
:type tstamp: int | float | None
:param event_subject: Optional per event subject
:type event_subject: subject | None
:rtype: Tracker
"""
warn(
"track_unstruct_event will be deprecated in future versions. Please use track_self_describing_event.",
DeprecationWarning,
stacklevel=2,
)
self.track_self_describing_event(
event_json=event_json,
context=context,
true_timestamp=tstamp,
event_subject=event_subject,
)
return self
def flush(self, is_async: bool = False) -> "Tracker":