Reference
Avo Codegen
Programming languages
Python

Avo Codegen in Python

Platforms

Avo can code generate Avo Codegen in Python for your server side tracking. Avo supports Python 2 and Python 3.

Quickstart

Avo Codegen usage consists of 4 steps.

Step 1. Include the Avo file

Pull the generated code with the Avo CLI

To get the Avo generated Python file you must be a member of an Avo workspace with a Python source. Ask for an invite from a colleague or create a new workspace (opens in a new tab)

npm install -g avo
avo login
avo pull --branch my-branch-name

Learn more about the CLI here.

You can also download the file manually from your Avo workspace.

Step 2. Initialize Avo

Import Avo from the generated file and initialize it by calling the init_avo method before tracking

import avo
 
avo.init_avo({'env': 'dev'}, ...) # other parameters depend on your tracking plan setup

The actual parameters depend on your tracking plan setup, see the parameters explanation in the reference below.

Step 3. Call Avo Codegen to track your product usage

Every event in your tracking plan, marked with the "Implement with Codegen" checkbox, gets a function in the generated code, named according to the event name, in snake_case.

For example, if you have a "Signup Start" event defined like this in Avo:

Event 'Signup Start' defined in Avo with referral string property and implement with Codegen check

You'll be able to call it like this from the generated code

avo.signup_start(referral = 'direct')

Notice, that you are not passing the System property with the call. System properties are defined on the init step and then automatically included with all events. You can update the system properties with set_system_properties function.

Step 4. Verify the implementation

Use the Implementation status in your Avo workspace to verify that your implementation is correct.

Reference

init_avo

avo.init_avo(options, [system_properties], [mixpanel_destination], [segment_destination], [other_destination], ...)

Initializes Avo, needs to be called before the tracking methods. This method will call the make(env, apiKey) callback in all the provided destination interfaces. It will also initialize the analytics SDKs of the legacy Avo Managed destinations.

Arguments

options (dict): a dictionary with env (one of: 'dev', 'prod'), strict bool, verbose bool, noop bool

  • env: string, one of 'dev' or 'prod'.
  • [noop = false]: bool, if set, Avo won't make any network calls (no tracking) in development and staging environments. Note that the noop flag is ignored in production.
  • [strict = true]: bool, if set, Avo will throw an exception when it detects a tracking problem in development or staging. Note that the strict flag is ignored in production.
  • [verbose = true]: bool, if set and strict is false, will print the error messages to console.

system_properties (dict): a dictionary containing the system properties that should be sent with every event. When you define system properties in your Avo workspace you set name and type - the keys in this dictionary should be the same as system properties, in snake_case, and you should provide corresponding types, can be string, int, long, float, bool and list.

[destination (object)]: each destination you are sending events to gets a separate parameter in the init function with hooks that the Avo generated code will trigger, unless you are using the legacy Avo managed destinations. Each method in the destination interface is directly mapped to the Actions attached to each event in Avo. Learn more about event Actions in this doc.

Destination interface example

# Example: Destination interface for the Mixpanel SDK. Replace the Mixpanel implementation with your own tracking SDK methods
class CustomDestination:
    # This method is optional, you can skip it if you've already initialized your Analytics SDK
    def make(self, env, apiKey):
        # TODO: Replace the call below with a call to your destination
        self.mp = Mixpanel(apiKey)
 
    def track_event(self, user_id, event_name, event_properties):
        # TODO: Replace the call below with a call to your destination
        self.mp.track(user_id, event_name, event_properties)
 
    def set_user_properties(self, user_id, user_properties):
        # TODO: Replace the call below with a call to your destination
        self.mp.people_set(user_id, user_properties)
 
    def log_page(self, user_id, page_name, event_properties):
        # TODO: Replace the call below with a call to your destination
        # Note: Mixpanel does not provide a native method for page or screen tracking, so we send an event instead. Other SDKs may have a dedicated page tracking method.
        event_properties["Page Name"] = page_name
        self.mp.track(user_id, "Page Viewed", event_properties)
 
    def revenue(self, user_id, amount, event_properties):
        # TODO: Replace the call below with a call to your destination
        self.mp.people_track_charge(user_id, amount, event_properties)
 
    # The following methods are used for group analytics and are not required
    def add_current_user_to_group(self, user_id, group_type, group_id, group_properties):
        # TODO: Replace the call below with a call to your destination
        self.mp.people_set(user_id, { group_type : group_id })
        self.mp.group_set(group_type, group_id, group_properties)
 
    def set_group_properties(self, group_type, group_id, group_properties):
        # TODO: Replace the call below with a call to your destination
        self.mp.group_set(group_type, group_id, group_properties)
 
    def track_event_with_groups(self, user_id, event_name, event_properties, group_types_to_group_ids):
        # TODO: Replace the call below with a call to your destination
        for group_type in group_types_to_group_ids:
            event_properties[group_type] = group_types_to_group_ids[group_type]
        self.mp.track(user_id, event_name, event_properties)
 
# You can then call avo.init_avo with your destination interface
avo.init_avo({'env': 'dev'}, CustomDestination())

Learn more about group analytics here

To add the optional anonymousId parameter to the callbacks in your workspace reach out to us.

Segment example (without the group analytics)

Here is a more specific example of how you can use the destination interface in Python to integrate with your Segment SDK. In this example we're using the analytics (opens in a new tab) Python SDK from Segment

import analytics
 
# An object representing your custom destination
# that you pass into init_avo():
class SegmentDestination:
    def make(self, env):
        analytics.write_key = 'YOUR_WRITE_KEY'
 
    def track_event(self, user_id, event_name, event_properties):
        analytics.track(user_id, event_name, event_properties)
 
    def set_user_properties(self, user_id, user_properties):
        analytics.identify(user_id, user_properties)
 
    def log_page(self, user_id, page_name, event_properties):
        analytics.page(user_id, page_name, event_properties)
 
    def revenue(self, user_id, amount, event_properties):
      analytics.track(user_id, 'Revenue', { 'revenue': amount })

Read more about the destination interface here.

set_system_properties

avo.set_system_properties(system_props)

A method to update system properties.

Arguments

system_props (dict): a dictionary containing the system properties that should be sent with every event. When you define system properties in your Avo workspace you set name and type - the keys in this dictionary should be the same as system properties, in snake_case, and you should provide corresponding types.

Event tracking functions

avo.your_event_name([event_property_0], [event_property_1], ..., [user_property_0], [user_property_1], ..., [user_id_], [anonymous_id_] })

Every event you define in your tracking plan in Avo gets a function named after the event in snake_case. The arguments of the function depend on how it's defined in your tracking plan.

Arguments

event_property: type defined in the Avo tracking plan, can be string, int, long, float, bool and list. The argument key is snake_case version of the property name defined in the Avo UI. This is an event property that should be tracked with given event. Pass the value of the property to track here.

user_property: type defined in the Avo tracking plan, can be string, int, long, float, bool and list. The argument key is snake_case version of the property name defined in the Avo UI. This is a user property that should be updated with given event. Pass the value of the property to update here.

user_id_: string added to all events, used to connect event to specific user, you have to either provide it or the anonymous_id_

anonymous_id_: string, this argument is automatically added if corresponding setting is enabled, used to track anonymous users

Destinations

You can send your data using the Avo generated JavaScript code to any data destination that accepts custom events, including:

  • Amplitude
  • FacebookAnalytics
  • FullStory
  • Mixpanel
  • Mixpanel
  • Permutive
  • Segment
  • Snowplow
  • ZendeskConnect
  • Adobe Analytics
  • Apptelemetry
  • RudderStack
  • Freshpaint
  • PostHog
  • Google Analytics 4 / Firebase Analytics
  • Heap
  • Keen
  • Kissmetrics
  • LaunchDarkly Events
  • Pendo
  • Fivetran
  • AppsFlyer
  • Braze
  • Intercom
  • A home made SDK
  • Internal API