Tealium for Python

Tealium for Python

by jason_koo on ‎04-15-2016 11:12 AM - edited on ‎06-23-2017 03:30 PM by Community Manager (867 Views)

This guide shows how to add Tealium to your Python application to track activity in the Universal Data Hub.

Table of Contents Placeholder

Getting Started

Requirements

Installation 

The Tealium for Python package can be installed using pip. If you haven't used pip before please read the pip installation guide.

Using Python 2:

pip install tealium

Using Python 3:

pip3 install tealium

Initialization

Once the Tealium for Python package is installed you are ready to start coding. Use the following code to import the Tealium module and initialize with the following parameters:

  • account - the name of your account
  • profile - the name of the profile within your account
  • environment - (optional) one of "qa", or "prod"
  • path - (optional) path to save persisted data
  • datasource - (optional) the data source key from UDH

It may be necessary to use named parameters when omitting optional parameters from the call, e.g. "datasource" must be named if omitting "path".

from tealium import Tealium
teal = Tealium('account', 'profile', 'environment', datasource:'datasource')

Tracking

Once the Tealium object has been defined you can begin tracking events with the method trackEvent(), which takes the following parameters:

  • event (required) - a name to identify the event
  • data - optional dictionary with event data as key/value pairs
  • callback - optional callback function

A function that triggers or responds to an event trigger could look like the following:  

def tealiumCallback(info, success, error=None):
if error is not None:
print (info, success, error)
return
print (info, success)

eventData = {'someKey': 'someValue'}
teal.trackEvent('someEvent',
data:eventData,
callback:tealiumCallback) 

Additional Resources

Function Definitions

Tealium()

Creates a Tealium object that will be used to track events.

Tealium(account, profile, environment, path, datasource)
Parameters Type Description
account String Tealium account name eg. "companyXYZ"
profile String Tealium profile name eg. "main"
environment (optional) String Tealium environment identifer eg. "prod"
path (optional) String Where to save persistent data eg. "/path/to/persist/data"
datasource (optional) String The data source key from UDH eg. "abc123"

trackEvent()

Creates a Tealium object that will be used to track events.

trackEvent(eventName, data, callback)
Parameters Type Description
eventName String Name of the event eg. "user_login"
data Dictionary Dictionary of additional data source keys and values (optional)
callback Function Optional callback function

resetSessionId()

Reset the session ID. For use when needing to start a new session other than at app start time.

resetSessionId()
Returns Description Example Value
String A timestamp in milliseconds, converted to a String, that is associated with all subsequent tracking. The session ID is maintained internally and automatically included as the tealium_vid event attribute. "1473371215123"