forked from mParticle/mparticle-python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_usage.py
72 lines (55 loc) · 2.61 KB
/
example_usage.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
import mparticle
batch = mparticle.Batch()
batch.environment = 'development'
identities = mparticle.UserIdentities()
identities.customerid = '123456'
identities.email = '[email protected]'
batch.user_identities = identities
device_info = mparticle.DeviceInformation()
# set any IDs that you have for this user
device_info.ios_advertising_id = '07d2ebaa-e956-407e-a1e6-f05f871bf4e2'
device_info.android_advertising_id = 'a26f9736-c262-47ea-988b-0b0504cee874'
batch.device_info = device_info
# arbitrary example allowing you to create a segment of users trial users
batch.user_attributes = {'Account type': 'trial', 'TrialEndDate':'2016-12-01'}
app_event = mparticle.AppEvent('Example', 'navigation')
app_event.timestamp_unixtime_ms = 1552596256103
product = mparticle.Product()
product.name = 'Example Product'
product.id = 'sample-sku'
product.price = 19.99
product_action = mparticle.ProductAction('purchase')
product_action.products = [product]
product_action.tax_amount = 1.50
product_action.total_amount = 21.49
commerce_event = mparticle.CommerceEvent(product_action)
commerce_event.timestamp_unixtime_ms = 1552596256103
session_start = mparticle.SessionStartEvent()
session_start.session_id = 12345678
session_start.timestamp_unixtime_ms = 1552596256103
session_end = mparticle.SessionEndEvent()
session_end.session_id = session_start.session_id # its mandatory that these match
session_end.session_duration_ms = 10000
session_end.timestamp_unixtime_ms = 1552596266103 + 10000
batch.events = [session_start, app_event, commerce_event, session_end]
batch.mpid = '600868121729048600'
batch.mp_deviceid = '59780f39-d7a0-4ebe-9950-280f937c29e2'
install = mparticle.ApplicationStateTransitionEvent.create_install_event()
install.timestamp_unixtime_ms = 1552596256103
upgrade = mparticle.ApplicationStateTransitionEvent.create_upgrade_event()
upgrade.timestamp_unixtime_ms = 1552596256103
foreground = mparticle.ApplicationStateTransitionEvent.create_foreground_event()
foreground.timestamp_unixtime_ms = 1552596256103
background = mparticle.ApplicationStateTransitionEvent.create_background_event()
background.timestamp_unixtime_ms = 1552596256103
configuration = mparticle.Configuration()
configuration.api_key = 'foo-key'
configuration.api_secret = 'foo-secret'
configuration.debug = True #enable logging of HTTP traffic
api_instance = mparticle.EventsApi(configuration)
try:
api_instance.upload_events(batch)
# you can also send multiple batches at a time to decrease the amount of network calls
#api_instance.bulk_upload_events([batch, batch])
except mparticle.rest.ApiException as e:
print "Exception while calling mParticle: %s\n" % e