Android - analytics.kotlin

https://github.com/Topsort/analytics.kotlin

FrontEnd

An Android library for reporting events to Topsort Analytics Endpoints v1 API

Installation

  1. Clone the repository from github: git clone https://github.com/topsort/analytics.kotlin.git
  2. From Android Studio, import the module: File -> New -> Import Module.
  3. A popup window will open with the title: Import Module From Source
  4. Select the source directory of the downloaded library named: TopsortAnalytics, and click finish
  5. Right click on the application project module, from the menu select Open module Settings
  6. From the left side of the displayed panel select dependencies
  7. Under Modules select the application module
  8. Under Declared Dependencies, click the + button, and select Module Dependency
  9. A popup will show up, select TopsortAnalytics, and at the bottom of the dialog select the type of configuration as implementation, then click ok

Usage

Seup

Kotlin

import android.app.Application
import com.topsort.analytics.Analytics

class KotlinApplication : Application() {

    override fun onCreate() {
        super.onCreate()

        // Either generate a unique session id here or hash an existing
        // identifier. It should be consistent for
        // each user (impression, click, purchase).

        Analytics.setup(
            this,
            "sessionId",
            "bearerToken"
        )
    }
}

Java

import android.app.Application;
import com.topsort.analytics.Analytics;

public class JavaApplication extends Application {


    @Override
    public void onCreate() {
        super.onCreate();

        Analytics
                .INSTANCE
                .setup(this, "sessionId", "bearerToken");
    }
}

Reporting Events

Kotlin

fun reportPurchase() {
    val item = PurchasedItem(
        resolvedBidId = "<The bid id from the auction winner>",
        productId = "productId",
        unitPrice = 1295,
        quantity = 20
    )

    Analytics.reportPurchase(
        id = "marketPlaceId",
        items = listOf(item)
    )
}

fun reportClick() {
    val placement = Placement(
        page = "search_results",
        location = "position_1"
    )

    Analytics.reportClick(
        placement = placement,
        productId = "productId"
    )
}

fun reportClick() {
    val placement = Placement(
        page = "search_results",
        location = "position_1"
    )

    Analytics.reportClickWithResolvedBidId(
        resolvedBidId = "<The bid id from the auction winner>",
        placement = placement
    )
}

fun reportImpression() {
    val placement = Placement(
        page = "search_results",
        location = "position_1"
    )

    val impression = Impression(
        productId = "productId",
        placement = placement
    )

    Analytics.reportImpression(listOf(impression))
}

fun reportImpressionWithResolvedBidId() {
    val placement = Placement(
        page = "search_results",
        location = "position_1"
    )

    Analytics.reportImpressionWithResolvedBidId(
        resolvedBidId = "<The bid id from the auction winner>",
        placement = placement
    )
}

Java

private void reportImpressionWithResolvedBidId() {
        Placement placement = new Placement(
                "search_results",
                null
        );

        Impression impression = new Impression(
                placement,
                null,
                null,
                null,
                "<The bid id from the auction winner>"

        );

        Analytics
                .INSTANCE
                .reportImpression(Collections.singletonList(impression));
    }

    private void reportImpression() {
        Placement placement = new Placement(
                "search_results",
                null
        );

        Impression impression = new Impression(
                placement,
                "productId",
                null,
                null,
                null
        );

        Analytics
                .INSTANCE
                .reportImpression(Collections.singletonList(impression));
    }

    private void reportClick() {
        Placement placement = new Placement(
                "search_results",
                null
        );

        Analytics
                .INSTANCE
                .reportClick(placement, "productId", null, null, null);
    }

    private void reportClickWithResolvedBidId() {
        Placement placement = new Placement(
                "search_results",
                null
        );

        Analytics
                .INSTANCE
                .reportClick(
                        placement,
                        null,
                        null,
                        null,
                        "<The bid id from the auction winner>"
                );
    }

    private void reportPurchase() {

        PurchasedItem item = new PurchasedItem(
                "productId",
                20,
                1295,
                null,
                null
        );

        Analytics
                .INSTANCE
                .reportPurchase(Collections.singletonList(item), "marketPlaceId");
    }

    private void reportPurchaseWithResolvedBidId() {
        PurchasedItem item = new PurchasedItem(
                "productId",
                20,
                1295,
                null,
                "<The bid id from the auction winner>"
        );

        Analytics
                .INSTANCE
                .reportPurchase(Collections.singletonList(item), "marketPlaceId");
    }