Avo can code generate Avo Functions in Kotlin targeted at the following platforms
Java
generated code for backend JVM projectsReach out if you want to use Avo Functions in Kotlin server
Avo functions usage consists of 4 steps.
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
TerminalCopy$$$npm install -g avoavo loginavo pull --branch my-branch-name
Learn more about the CLI here.
You can also download the file manually from your Avo workspace.
Initialize Avo by creating an object using constructor from the generated Avo file
KotlinCopy1val 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.
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:
You'll be able to call it like this from the generated code
KotlinCopy1avo.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.
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.
KotlinCopy12345678910class AvoImpl(env: AvoEnv,// Other parameters may not be present, depending on your tracking plansystemProperty0: int, systemProperty1: bool,mixpanelDestination: ICustomDestination,segmentDestination: ICustomDestination,otherDestination: ICustomDestination,avoInspector: Any? = null,strict: Boolean = true,application: Application, context: Context)
Creates the Avo object that will be used to track.
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.
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.
destination: ICustomDestination
: object, each destination you are sending events to gets a separate parameter in the init function with callbacks 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.
KotlinCopy123456789101112131415interface ICustomDestination {fun make(env: AvoEnv, apiKey: String) {} // This method is optional, you can skip it if you've already initialized your Analytics SDKfun logEvent(eventName: String, eventProperties: Map<String, *>)fun logPage(pageName: String, eventProperties: Map<String, *>)fun revenue(amount: Double, eventProperties: Map<String, *>)fun setUserProperties(userId: String, userProperties: Map<String, *>)fun identify(userId: String)fun unidentify()}
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.
KotlinCopy123456789101112131415161718192021222324252627282930313233343536// Example: Destination interface for the Mixpanel SDK. Replace the Mixpanel implementation with your own tracking SDK methodslet mixpanelDestinationInterface = object : ICustomDestination {var mixpanel: MixpanelAPI? = nullprivate setoverride fun make(env: AvoEnv, apiKey: String) {mixpanel = MixpanelAPI.getInstance(context.applicationContext, apiKey)}override fun logEvent(eventName: String, eventProperties: Map<String, *>) {mixpanel?.track(eventName, eventProperties)}override fun setUserProperties(userId: String, userProperties: Map<String, *) {mixpanel?.people?.set(userProperties)}override fun logPage(pageName: String, eventProperties: MutableMap<String, *>) {// Note: In this example the Mixpanel SDK 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.let mutableEventprops = eventProperties.toMutableMap()mutableEventprops["Page Name"] = pageNamemixpanel?.track("Page Viewed", mutableEventprops)}override fun revenue(amount: Double, eventProperties: Map<String, *>) {mixpanel?.people?.trackCharge(amount, eventProperties)}override fun identify(userId: String) {mixpanel?.identify(userId)}override fun unidentify() {mixpanel?.reset()}}
Read more about the destination interface here.
application: Application
and context: Context
are required by some analytics destinations.
KotlinCopy1fun 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.
avoLogger: (String) -> Unit
: custom implementation of the logger, e.g. { logMessage: String -> Log.d(this.localClassName, logMessage) }
KotlinCopy1fun setSystemProperties(systemProperty0: Int, systemProperty1: Boolean?, ...)
A method to update system properties after initialization.
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.
KotlinCopy1fun [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
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
Snowplow SDK's tracking interface is a little different from the common event tracking libraries and working with Snowplow through Avo is slightly different too.
If you add a snowplow destination to a Kotlin source you'll get an additional constructor parameter
KotlinCopy1snowplowDestination: AvoSnowplowDestination
You'll implement it like this:
KotlinCopy1234567891011121314151617181920212223242526272829303132333435363738394041val avo = AvoImpl(...,snowplowDestination = object : AvoSnowplowDestination {override fun make(env: AvoEnv) {// Your custom Snowplow initialization, that includes the `createTracker` callSnowplow.createTracker(this@MainActivity, "appTracker", "COLLECTOR_URL", HttpMethod.POST)// Learn more: https://docs.snowplowanalytics.com/docs/collecting-data/collecting-from-own-applications/mobile-trackers/mobile-trackers-v2-0/quick-start-guide/}override fun trackSelfDescribingEvent(schema: String, data: Map<String, *>, contexts: List<Map<String, Any>>) {val eventContent = SelfDescribingJson(schema, data)val event = SelfDescribing(eventContent)contexts.forEach { customContext ->val contextSchema = customContext["schema"]val contextData = customContext["data"]if (contextSchema is String && contextData is Any) {event.customContexts.add(SelfDescribingJson(contextSchema, contextData))}}Snowplow.getDefaultTracker()?.track(event)// Learn more: https://docs.snowplowanalytics.com/docs/collecting-data/collecting-from-own-applications/mobile-trackers/mobile-trackers-v2-0/tracking-events/}override fun trackPageView(title: String) {Snowplow.getDefaultTracker()?.track(ScreenView(title))// Learn more: https://docs.snowplowanalytics.com/docs/collecting-data/collecting-from-own-applications/mobile-trackers/mobile-trackers-v2-0/tracking-events/}override fun identify(userId: String) {Snowplow.getDefaultTracker()?.subject?.userId = userId// Learn more: https://docs.snowplowanalytics.com/docs/collecting-data/collecting-from-own-applications/mobile-trackers/mobile-trackers-v2-0/introduction/#SubjectConfiguration}override fun unidentify() {Snowplow.getDefaultTracker()?.subject?.userId = null// Learn more: https://docs.snowplowanalytics.com/docs/collecting-data/collecting-from-own-applications/mobile-trackers/mobile-trackers-v2-0/introduction/#SubjectConfiguration}})
You can send your data using the Avo generated Kotlin code to any data destination that accepts custom events, including: