Avo Functions in Kotlin

5 minute read

Platforms

Avo can code generate Avo Functions in Kotlin targeted at the following platforms

  • Android
  • Server is coming soon, for now we recommend using the Java generated code for backend JVM projects
Reach out if you want to use Avo Functions in Kotlin server

Quickstart

Avo functions usage consists of 4 steps.

Step 1. Include the Avo file

Pull the generated code with the Avo CLI

To get the Avo generated Kotlin file you must be a member of an Avo workspace with a Kotlin source. Ask for an invite from a colleague or create a new workspace

Terminal
Copy
$
$
$
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

Initialize Avo by creating an object using constructor from the generated Avo file

Kotlin
Copy
1
val avo = AvoImpl(env = AvoEnv.DEV/*, other parameters depending 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 functions to track your product usage

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

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 Avo check

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

Kotlin
Copy
1
avo.signupStart(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 setSystemProperties function.

Step 4. Verify the implementation

Use the Implementation status in your Avo workspace and the Avo Inspector to verify that your implementation is correct. If you don't want to use the Avo Inspector you can use the standalone visual debugger.

Reference

Constructor

Kotlin
Copy
1
2
3
4
5
6
7
8
class AvoImpl(env: AvoEnv,
// Other parameters may not be present, depending on your tracking plan
systemProperty0: int, systemProperty1: bool,
customDestination: ICustomDestination,
avoInspector: Any? = null,
strict: Boolean = true,
application: Application, context: Context
)

Creates the Avo object that will be used to track. This method will initialize the SDKs for all destinations that are managed by Avo, and call the make(env) callback in all custom destinations.

Arguments

env: AvoEnv: Can be set to dev, prod and staging.

systemProperties: a number of parameters equal to the number of system properties defined in your Avo workspace. The parameters are named the same as system properties, in camelCase, and require corresponding types: string, int, long, float, bool or list.

customDestination: ICustomDestination: object, required if target source has a custom destination set up in Avo. Otherwise not present.

Custom destination interface format:

Kotlin
Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
interface ICustomDestination {
fun make(env: AvoEnv)
fun logEvent(eventName: String, eventProperties: Map<String, *>)
fun logPage(pageName: String, eventProperties: Map<String, *>) {}
fun setUserProperties(userId: String, userProperties: Map<String, *>)
fun identify(userId: String)
fun unidentify()
}

Read more about custom destinations here.

avoInspector: Any?: optional Avo Inspector instance. If you use Avo Inspector pass it here to make the Avo functions automatically report the invocations to the Avo Inspector.

strict: Boolean: bool, if true, Avo will throw an exception when it detects a tracking problem in development or staging. Note that the strict flag is ignored in production.

Additional arguments

application: Application and context: Context are required by some analytics destinations.

setAvoLogger

Kotlin
Copy
1
fun setAvoLogger(logger: (String) -> Unit)

This method allows you to provide custom implementation of the logger used by the Avo Functions. Can for example be used to disable logs or change which logging method is used.

Arguments

avoLogger: (String) -> Unit: custom implementation of the logger, e.g. { logMessage: String -> Log.d(this.localClassName, logMessage) }

setSystemProperties

Kotlin
Copy
1
fun setSystemProperties(systemProperty0: Int, systemProperty1: Boolean?, ...)

A method to update system properties after initialization.

Arguments

systemProperties: a number of parameters equal to the number of system properties defined in your Avo workspace. The parameters are named the same as system properties, in camelCase, and require corresponding types: string, int, long, float, bool or list.

Event tracking functions

Kotlin
Copy
1
fun [yourEventName](eventProperty0: Int, eventProperty1: Double?, ..., userProperty0: Boolean?, userProperty1: List<Int>, ..., userId_: String)

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

Arguments

eventProperty: type defined in the Avo tracking plan, can be string, int, long, float, bool and list: Every event property attached to the event in the Avo UI gets a corresponding argument. The argument key is camelCase version of the property name. Pass the value of the property to track here.

userProperty: type defined in the Avo tracking plan as a user property, can be string, int, long, float, bool and list: Every user property attached to the event in the Avo UI gets a corresponding argument. The argument key is camelCase version of the property name. Pass the value of the property to update here.

userId_: String: used to connect event to specific user. Added if the event has the Identify User action

Destinations

You can send your data using the Avo generated Kotlin 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