Giter Site home page Giter Site logo

kotlinsdk's Introduction

KotlinClient

This package allows you to send signals to TelemetryDeck from your Android applications. Sign up for a free account at telemetrydeck.com

Installation

The TelemetryDeck is distributed using jitpack, so you'll need to add the jitpack dependency to your settings.gradle file:

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        maven { url 'https://jitpack.io' } // <-- add this line
    }
}

After that is done, add the following to your build.gradle file, under dependencies:

dependencies {
    // ...
    // Please replace 1.0.0 with the latest version of the SDK
    implementation 'com.github.TelemetryDeck:KotlinSDK:1.0.0'
}

Permission for internet access

Sending signals requires access to the internet so the following permission should be added to the app's AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />

Using the application manifest

The TelemetryManager can be initialized automatically by adding the application key to the application section of the app's AndroidManifest.xml:

<application>
...

<meta-data android:name="com.telemetrydeck.sdk.appID" android:value="XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" />

</application>

Replace XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX with your TelemetryDeck App ID.

In addition, the following optional properties are supported:

  • com.telemetrydeck.sdk.showDebugLogs
  • com.telemetrydeck.sdk.apiBaseURL
  • com.telemetrydeck.sdk.sendNewSessionBeganSignal
  • com.telemetrydeck.sdk.sessionID
  • com.telemetrydeck.sdk.testMode
  • com.telemetrydeck.sdk.defaultUser

Programatic Usage

For greater control you can instead manually start the TelemetryManager client

val builder = TelemetryManager.Builder()
            .appID("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")
            .showDebugLogs(true)
            .defaultUser("Person")

TelemetryManager.start(application, builder)

Sending Signals

To send a signal immediately

TelemetryManager.send("appLaunchedRegularly")

To enqueue a signal to be sent by TelemetryManager at a later time

TelemetryManager.queue("appLaunchedRegularly")

Custom Telemetry

Another way to send signals is to register a custom TelemetryProvider . A provider maintains a reference to the TelemetryManager in order to queue or send signals.

To create a provider, implement the TelemetryProvider interface:

class CustomProvider: TelemetryProvider {
    override fun register(ctx: Application?, manager: TelemetryManager) {
        // configure and start the provider
    }

    override fun stop() {
        // deactivate the provider
    }
}

Setup and start the provider during the register method.

Tips:

  • Do not retain a strong reference to the application context or the TelemetryManager.
  • You can use WeakReference<TelemetryManager> if you need to be able to call the TelemetryManager at a later time.

To use your custom provider, register it using the TelemetryManager.Builder :

val builder = TelemetryManager.Builder()
            .appID("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")
            .addProvider(CustomProvider())

When a signal is received by TelemetryManager, it can be enriched with platform and environment specific information. TelemetryManager calls the enrich method allowing every registered provider to add additional payload to a signal.

override fun enrich(
        signalType: String,
        clientUser: String?,
        additionalPayload: Map<String, String>
    ): Map<String, String> {
        val signalPayload = additionalPayload.toMutableMap()
        val today = LocalDateTime.now().dayOfWeek
        if (today == DayOfWeek.MONDAY) {
            signalPayload["isMonday"] = "yes"
        }
        return signalPayload
    }

TelemetryManager also makes use of providers in order to provide lifecycle and environment integration out of the box. Feel free to examine how they work and inspire your own implementations. You can also completely disable or override the default providers with your own.

  • SessionProvider - Monitors the app lifecycle in order to broadcast the NewSessionBegan signal. This provider is tasked with resetting the sessionID when sendNewSessionBeganSignal is enabled.
  • AppLifecycleTelemetryProvider - Emits signals for application and activity lifecycle events.
  • EnvironmentMetadataProvider - Adds environment and device information to outgoing Signals. This provider overrides the enrich method in order to append additional metdata for all signals before sending them.
// Append a custom provider
val builder = TelemetryManager.Builder()
           .appID("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")
           .addProvider(CustomProvider())


// Replace all default providers
val builder = TelemetryManager.Builder()
            .appID("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")
            .providers(listOf(CustomProvider(), AnotherProvider()))

Requirements

  • SDK 21 or later
  • Kotlin 1.6.10 or later
  • Java Compatibility Version 1.8

kotlinsdk's People

Contributors

kkostov avatar winsmith avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.