Countly Android SDK

repository·master·Indexed 20 days ago

https://github.com/countly/countly-sdk-android

A comprehensive analytics and engagement toolkit for Android developers. The SDK supports tracking user behavior, crash reporting, A/B testing, push notifications, user profiles, performance monitoring, and feedback widgets. It is compatible with Countly Lite, Countly Flex, and Countly Enterprise server deployments.

Tokens
1.9K
Snippets
4
Records
11
Agent score
72%

What's inside countly-sdk-android

  1. Overview of Countly Android SDK

    master

    The Countly Android SDK is a product analytics and innovation tool designed for integration into Android applications. It is compatible with Countly Lite, Countly Flex, and Countly Enterprise server deployments.

    Key features supported by the SDK include:

    • Analytics: Track user behavior and product performance.
    • Push Notifications: Engage users with targeted notifications.
    • User Profiles: Manage and segment user data.
    • Crash Reports: Monitor application stability and errors.
    • A/B Testing: Run experiments to optimize features.
    • Performance Monitoring: Track app performance metrics.
    • Feedback Widgets: Collect user surveys, NPS, and ratings.
  2. Integrate Countly SDK into your Android project

    master

    To integrate the Countly Android SDK, refer to the official documentation for detailed setup instructions, including minimal integration steps and full configuration guides.

  3. Run the Sample Android app for demo

    master

    The sample application demonstrates how to send data to a remote Countly server. To explore the implementation and run the demo:

    1. Open the project in Android Studio.
    2. Select an appropriate Android device or emulator.
    3. Click Run to build and launch the application.
  4. Override device metrics using MetricProvider

    master

    The DeviceInfo class allows for overriding default device information by providing a custom implementation of the MetricProvider interface. This is useful if you need to spoof or manually provide device attributes like OS version, manufacturer, or resolution for testing or specific use cases. When a MetricProvider is passed to the DeviceInfo constructor, its methods are checked first; if they return a non-null value, that value is used instead of the actual device information.

    // Example of how one might implement a custom MetricProvider
    MetricProvider myOverride = new MetricProvider() {
        @NonNull
        @Override
        public String getOS() {
            return "Custom OS";
        }
        // ... implement other methods
    };
    
    // Pass it to DeviceInfo
    DeviceInfo deviceInfo = new DeviceInfo(myOverride);
  5. Understand the structure of a Countly Custom Event

    master

    A Countly Event represents a single custom event instance. It consists of a unique key and optional metadata used for segmentation and analytics.

    Key components include:

    • Key: The unique identifier for the event type.
    • Segmentation: A Map<String, Object> containing custom properties used to filter and group events (e.g., user attributes, session details).
    • Metrics: Fields like count (number of occurrences), sum (total value), and dur (duration) allow for quantitative analysis.
    • Temporal Data: timestamp, hour, and dow (day of week) track when the event occurred.
    • Identifiers: Optional IDs such as id, pvid (page view ID), cvid (conversion ID), and peid (page event ID) provide context for specific user actions.

    For more details on the JSON syntax used by these events, refer to the Countly Custom Events documentation.

  6. Retrieve crash-specific device metrics

    master

    When a crash occurs, DeviceInfo can provide a comprehensive set of metrics to help diagnose the environment.

    For non-native crashes, the following additional metrics are included:

    • _cpu: CPU architecture (ABI)
    • _opengl: OpenGL ES version
    • _root: Whether the device is rooted
    • _ram_total: Total device RAM
    • _ram_current: Currently available RAM
    • _disk_total: Total disk space
    • _disk_current: Used disk space
    • _bat: Battery level
    • _run: App running time
    • _orientation: Screen orientation (Landscape/Portrait)
    • _online: Network connectivity status
    • _muted: Whether the device is in silent/vibrate mode
    • _background: Whether the app was in the background

    For native crashes, the metric _native_cpp is set to true.

  7. Retrieve common device metrics

    master

    The DeviceInfo class provides several methods to gather hardware and software information from the Android device. These metrics are typically used for session, remote config, and crash analytics.

    Key metrics available include:

    • _device: Device model
    • _os: Operating system name
    • _os_version: OS version
    • _resolution: Screen resolution (e.g., "1080x1920")
    • _app_version: The application's version name
    • _manufacturer: Device manufacturer
    • _has_hinge: Whether the device has a hinge (for foldable devices)
    • _carrier: Network operator name
    • _density: Screen density (e.g., "XHDPI")
    • _locale: Language and country (e.g., "en_US")
    • _store: The installer package name (e.g., the Play Store)
    • _device_type: Categorization such as "mobile", "tablet", or "smarttv"
  8. Convert an Event to a JSONObject

    master

    The toJSON() method serializes the Event object into a JSONObject following the Countly custom event JSON syntax. This is useful when you need to manually inspect or transmit the event data in JSON format.

    Note that the segmentation object is only included in the resulting JSON if it contains at least one entry. Similarly, dur (duration) is only included if it is greater than 0.

    // Assuming an Event instance 'event' exists
    JSONObject json = event.toJSON();
  9. Create an Event from a JSONObject

    master

    The static fromJSON(JSONObject json) method acts as a factory to reconstruct an Event object from a JSONObject.

    • Success: Returns a valid Event object if the JSON contains a non-empty key field.
    • Failure: Returns null if the key is missing/empty or if a JSONException occurs during parsing.
    // Assuming 'json' is a valid JSONObject containing event data
    Event event = Event.fromJSON(json);
    if (event != null) {
        // Use the reconstructed event
    }
  10. Reference: DeviceInfo metric keys

    master

    The following keys are used when collecting device metrics for analytics via DeviceInfo.

    // Common metrics keys
    _device
    _os
    _os_version
    _resolution
    _app_version
    _manufacturer
    _has_hinge
    
    // Additional metrics keys
    _carrier
    _density
    _locale
    _store
    _device_type
    _cpu
    _opengl
    _root
    _ram_total
    _ram_current
    _disk_total
    _disk_current
    _bat
    _run
    _orientation
    _online
    _muted
    _background
    _native_cpp