Skip to content

Commit

Permalink
Add support for sending mpid, das
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjenkins authored and GitHub Enterprise committed Mar 18, 2019
1 parent 389aa7d commit 2fa4b89
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 6 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright 2016 mParticle, Inc.
Copyright 2019 mParticle, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 2 additions & 0 deletions docs/Batch.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Name | Type | Description | Notes
**user_attributes** | **object** | | [optional]
**deleted_user_attributes** | **list[str]** | | [optional]
**user_identities** | [**UserIdentities**](UserIdentities.md) | | [optional]
**mpid** | **int** | | [optional]
**mp_deviceid** | **str** | | [optional]

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

Expand Down
3 changes: 3 additions & 0 deletions docs/UserIdentities.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ Name | Type | Description | Notes
**email** | **str** | | [optional]
**alias** | **str** | | [optional]
**facebook_custom_audience_id** | **str** | | [optional]
**other2** | **str** | | [optional]
**other3** | **str** | | [optional]
**other4** | **str** | | [optional]

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

Expand Down
72 changes: 72 additions & 0 deletions example_usage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import mparticle
batch = mparticle.Batch()
batch.environment = 'development'

identities = mparticle.UserIdentities()
identities.customerid = '123456'
identities.email = 'user@example.com'
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
2 changes: 1 addition & 1 deletion mparticle/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def __init__(self, host=None, header_name=None, header_value=None, cookie=None,
self.host = host
self.cookie = cookie
# Set default User-Agent.
self.user_agent = 'mParticle Python client/0.10.5'
self.user_agent = 'mParticle Python client/0.10.6'

@property
def user_agent(self):
Expand Down
58 changes: 55 additions & 3 deletions mparticle/models/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@

class Batch(object):

def __init__(self, events=None, source_request_id=None, environment=None, ip=None, schema_version=None, device_info=None, application_info=None, user_attributes=None, deleted_user_attributes=None, user_identities=None, api_key=None):
def __init__(self, events=None, source_request_id=None, environment=None, ip=None, schema_version=None, device_info=None, application_info=None, user_attributes=None, deleted_user_attributes=None, user_identities=None, api_key=None, mpid=None, mp_deviceid=None):
"""
Batch - a model defined in Swagger
Expand All @@ -80,7 +80,9 @@ def __init__(self, events=None, source_request_id=None, environment=None, ip=Non
'user_attributes': 'object',
'deleted_user_attributes': 'list[str]',
'user_identities': 'UserIdentities',
'api_key':'str'
'api_key':'str',
'mpid':'int',
'mp_deviceid':'str'
}

self.attribute_map = {
Expand All @@ -94,7 +96,9 @@ def __init__(self, events=None, source_request_id=None, environment=None, ip=Non
'user_attributes': 'user_attributes',
'deleted_user_attributes': 'deleted_user_attributes',
'user_identities': 'user_identities',
'api_key':'api_key'
'api_key':'api_key',
'mpid':'mpid',
'mp_deviceid':'mp_deviceid'
}

self._api_key = api_key
Expand All @@ -108,6 +112,8 @@ def __init__(self, events=None, source_request_id=None, environment=None, ip=Non
self._user_attributes = user_attributes
self._deleted_user_attributes = deleted_user_attributes
self._user_identities = user_identities
self._mpid = mpid
self._mp_deviceid = mp_deviceid
self._allowed_types = {type(AppEvent('empty')):'custom_event',
type(FirstRunEvent()):'first_run',
type(SessionStartEvent()):'session_start',
Expand Down Expand Up @@ -400,6 +406,52 @@ def user_identities(self, user_identities):

self._user_identities = user_identities

@property
def mpid(self):
"""
Gets the mpid of this Batch.
:return: The mpid of this Batch.
:rtype: str
"""
return self._mpid

@mpid.setter
def mpid(self, mpid):
"""
Sets the mpid of this Batch.
:param mpid: The mpid of this Batch.
:type: str
"""

self._mpid = mpid

@property
def mp_deviceid(self):
"""
Gets the mp_deviceid of this Batch.
:return: The mp_deviceid of this Batch.
:rtype: str
"""
return self._mp_deviceid

@mp_deviceid.setter
def mp_deviceid(self, mp_deviceid):
"""
Sets the mp_deviceid of this Batch.
:param mp_deviceid: The mp_deviceid of this Batch.
:type: str
"""

self._mp_deviceid = mp_deviceid

def to_dict(self):
"""
Returns the model properties as a dict
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from setuptools import setup, find_packages

NAME = "mparticle"
VERSION = "0.10.5"
VERSION = "0.10.6"



Expand Down
28 changes: 28 additions & 0 deletions test/test_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,34 @@ def testBatch(self):
"""
model = mparticle.models.batch.Batch()

def testBatchMPID(self):
"""
Test Batch MPID
"""
model = mparticle.models.batch.Batch()

identity_dict = model.to_dict()
for key in identity_dict:
self.assertEqual(None, identity_dict[key])

model.mpid = 600868121729048600
identity_dict = model.to_dict()
self.assertEqual(600868121729048600, identity_dict["mpid"])

def testBatchDAS(self):
"""
Test Batch DAS
"""
model = mparticle.models.batch.Batch()

identity_dict = model.to_dict()
for key in identity_dict:
self.assertEqual(None, identity_dict[key])

model.mp_deviceid = "59780f39-d7a0-4ebe-9950-280f937c29e2"
identity_dict = model.to_dict()
self.assertEqual("59780f39-d7a0-4ebe-9950-280f937c29e2", identity_dict["mp_deviceid"])


if __name__ == '__main__':
unittest.main()

0 comments on commit 2fa4b89

Please sign in to comment.