{"id":21762030,"url":"https://github.com/segment-integrations/analytics-kotlin-live","last_synced_at":"2026-02-12T04:36:54.127Z","repository":{"id":65507908,"uuid":"490770660","full_name":"segment-integrations/analytics-kotlin-live","owner":"segment-integrations","description":null,"archived":false,"fork":false,"pushed_at":"2024-11-25T19:48:20.000Z","size":333,"stargazers_count":1,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-27T04:33:23.883Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/segment-integrations.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-05-10T16:07:55.000Z","updated_at":"2024-12-01T10:28:58.000Z","dependencies_parsed_at":"2024-03-07T20:27:23.067Z","dependency_job_id":"df084e46-e714-4996-92e4-fe9bd002d9d6","html_url":"https://github.com/segment-integrations/analytics-kotlin-live","commit_stats":null,"previous_names":["segment-integrations/analytics-kotlin-live","segmentio/edgefn-kotlin"],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/segment-integrations%2Fanalytics-kotlin-live","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/segment-integrations%2Fanalytics-kotlin-live/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/segment-integrations%2Fanalytics-kotlin-live/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/segment-integrations%2Fanalytics-kotlin-live/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/segment-integrations","download_url":"https://codeload.github.com/segment-integrations/analytics-kotlin-live/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248720970,"owners_count":21151020,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-26T12:10:42.280Z","updated_at":"2026-02-12T04:36:54.095Z","avatar_url":"https://github.com/segment-integrations.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Analytics Live for Kotlin\n\nAnalytics Live allows you to execute JavaScript stored in your workspace that \ncan create on-device Plugins that work just like regular on-device Plugins.\n\n![Analytics Live Plugin Diagram](imgs/analytics-kotlin-live-diagram.png)\n\nWhen you install the Analytics Live Plugin it can download a file containing\nJavaScript instructions that are used to create one or more on-device Plugins.\n\nThese plugins can then modify an event in the timeline like a regular on-device\nplugin.\n\n## Getting Started\n\nAdd the LivePlugins plugin dependency to your `build.gradle`\n\n```kotlin\n   dependencies {\n     implementation 'com.segment.analytics.kotlin:analytics-kotlin-live:\u003clatest_version\u003e'\n   }\n```\n\nAdd the LivePlugins plugin to your Analytics instance\n\n```kotlin\nimport com.segment.analytics.liveplugins.kotlin.LivePlugins\nimport com.segment.analytics.kotlin.android.Analytics\n...\n\nanalytics = Analytics(\n            \"WRITEKEY\",\n            applicationContext\n        ) {\n            this.collectDeviceId = true\n            this.trackApplicationLifecycleEvents = true\n            this.trackDeepLinks = true\n            this.flushAt = 1\n            this.flushInterval = 0\n        }\n\n        analytics.add(LivePlugins(fallbackFile: null))\n```\n\nNote: A `fallbackFile` can be provided if you want a default file to be\navailable at first start up or no file is configured in your space.\n\nSee the [LivePluginExample](Examples/LivePluginExample/) project for a example \non how to do this.\n\nFor more on uploading the Analytics Live Plugin file see the [segmentcli](https://github.com/segment-integrations/segmentcli/) repo. \n\n## JavaScript API\n\n### Utility Functions\n```javascript\n/*\nLog a message to the native console\n*/\nconsole.log(message)\n```\n\n### Pre-defined Variables\n```javascript\n/*\n  This analytics pre-defined value represents the instance of the Analytics\n  class running in your native application.\n*/\nlet analytics = Analytics(\"\u003cYOUR WRITE KEY\u003e\")\n```\n\n### Exposed Native Classes\n```javascript\n// Represents the native-code Analytics class\nclass Analytics {\n\t/* \n\tCreate a new instance of Analytics\n\tParams:\n\t\twriteKey: String - The writekey this instance will send data to\n\t\n\tto-do: See if other config options make sense to expose.\n\t*/\n\tconstructor(writeKey) { }\n\n\t/*\n\tGet the traits for the current identified user\n\tReturns: \n\t\tObject/Dictionary containing the users traits\n\t*/\n\tget traits() {}\n\n\t/*\n\tGet the current user ID\n\t*/\n\tget userId() {}\n\n\t/*\n\tGet the current anonymous ID\n\t*/\n\tget anonymousId() {}\n\n\t/*\n\tSend a track event to segment\n\tParams:\n\t\tevent: String - the name of the event\n\t\tproperties: Object - any additional properties or null\n\t*/\n\ttrack(event, properties) {}\n\t\n\t/*\n\tSend an identify event to segment\n\tParams:\n\t\tuserId: String - the user id\n\t\ttraits: Object - any additional information about the user\n\t*/\n\tidentify(userId, traits) {}\n\t\n\t/*\n\tSend a screen event to segment\n\tParams:\n\t\ttitle: String - The title of this screen\n\t\tcategory: String - The category of this screen, or null\n\t\tproperties: Object - Any additional properties or null\n\t*/\n\tscreen(title, category, properties) {}\n\t\n\t/*\n\tSend a group event to segment\n\tParams:\n\t\tgroupId: String - The groupId to associate with this session\n\t\ttraits: Object - Any additional information about this group or session\n\t*/\n\tgroup(groupId, traits) {}\n\t\n\t/*\n\tSend an alias event to segment\n\tParams:\n\t\tnewId: String - Associate this new ID with this user as an alias.\n\t*/\n\talias(newId) {}\n\t\n\t/*\n\tResets the current session.  A new anonymousId is created.  All user identity,\n\ttraits, etc. are removed from this analytics instance.\n\t*/\n\treset() {}\n\t\n\t/*\n\tTell this analytics instance to send any queued events off to segment.\n\t*/\n\tflush() {}\n\t\n\t/*\n\tAdd a LivePlugin subclass to this analytics instance. The LivePlugin subclass will\n\tdetermine whether this LivePlugin should run as part of the source timeline, or\n\ta specific destination timeline.\n\t*/\n\tadd(livePlugin) {}\n}\n\n// Represents an enum defining an live plugin type\nconst LivePluginType = {\n\tbefore: \"before\",\n    enrichment: \"enrichment\",\n    after: \"after\",\n    utility: \"utility\"\n};\n\n// Represents the type of settings update being received\nconst UpdateType = {\n\tinitial: true,\n\trefresh: false\n}\n\n// Subclass LivePlugin to add custom behaviors/transformations\nclass LivePlugin {\n\t/* \n\tCreate a LivePlugin\n\tParams:\n\t\ttype: LivePluginType - The type of live plugin\n\t\tdestination: String - A string representing the destination key this\n\t\t\t\t\t\t\t  live plugin will be active for\n\t*/\n\tconstructor(type, destination) {}\n\t\n  \t/*\n\tReturns the LivePluginType designation for this live plugin\n\t*/\n\tget type() {}\n\t\n\t/*\n\tReturns the destination this live plugin is active on, or null\n\t*/\n\tget destination() {}\n\t\n\t/*\n\tUpdate is called by the system when we receive new settings from Segment\n\tor the Analytics system is starting up.  \n\t\n\tImplementing this method is useful\n\tfor doing any setup that might be needed based on settings.\n\t\n\tParams:\n\t\tsettings: Object - A key/value object of all settings received from Segment\n\t\ttype: UpdateType - Determines if this is an initial batch of settings or a\n\t\t\t\t\t\t   subsequent update\n\t*/\n\tupdate(settings, type) {}\n\t\n\t/*\n\tWhen events move through the system, `process` is called.  This will naturally \n\tcall out to the appropriate handlers such as `track`, `identify`, etc.  If you\n\twant to perform transformations regardless of event type, override this method.\n\t\n\tCall `super.process()` if you'd like to preserve existing behavior of the base\n\tclass.\n\t\n\tParams:\n\t\tevent: Object - A key/value set of event data.\n\t\n\tReturns: \n\t\tA transformed or modified event object, the original event object,\n\t\tor null if the event is to be discarded.\n\t*/\n\tprocess(event) {}\n\t\n\t/*\n\tOverride this method to apply transformations specific to track events.\n\t\n\tParams:\n\t\tevent: Object - A key/value set of event data.\n\t\n\tReturns: \n\t\tA transformed or modified event object, the original event object,\n\t\tor null if the event is to be discarded.\n\t*/\n\ttrack(event) {}\n\n\t/*\n\tOverride this method to apply transformations specific to identify events.\n\t\n\tParams:\n\t\tevent: Object - A key/value set of event data.\n\t\n\tReturns: \n\t\tA transformed or modified event object, the original event object,\n\t\tor null if the event is to be discarded.\n\t*/\n\tidentify(event) {}\n\n\t/*\n\tOverride this method to apply transformations specific to group events.\n\t\n\tParams:\n\t\tevent: Object - A key/value set of event data.\n\t\n\tReturns: \n\t\tA transformed or modified event object, the original event object,\n\t\tor null if the event is to be discarded.\n\t*/\n\tgroup(event) {}\n\n\t/*\n\tOverride this method to apply transformations specific to alias events.\n\t\n\tParams:\n\t\tevent: Object - A key/value set of event data.\n\t\n\tReturns: \n\t\tA transformed or modified event object, the original event object,\n\t\tor null if the event is to be discarded.\n\t*/\n\talias(event) {}\n\n\t/*\n\tOverride this method to apply transformations specific to screen events.\n\t\n\tParams:\n\t\tevent: Object - A key/value set of event data.\n\t\n\tReturns: \n\t\tA transformed or modified event object, the original event object,\n\t\tor null if the event is to be discarded.\n\t*/\n\tscreen(event) {}\n\n\t/*\n\tReset is called on an live plugin when the system is told to reset.  Any\n\tuser information should be discarded.\n\t*/\n\treset() {}\n\n\t/*\n\tFlush is called on an live plugin when the system is told to send any\n\tqueued events off to Segment.\n\t*/\n\tflush() {}\n}\n```\n\n### Creating and Using Live Plugins\n```javascript\n// This live plugin will fix an incorrectly named event property\nclass FixProductViewed extends LivePlugin {\n\ttrack(event) {\n\t\tif (event.event == \"Product Viewed\") {\n\t\t\t// set the correct property to the value\n\t\t\tevent.properties.product_id = event.properties.product_did\n\t\t\t// delete the misnamed property\n\t\t\tdelete event.properties.product_did\n\t\t}\n\t\treturn event\n\t}\n}\n\n// create an instance of our FixProductViewed live plugin\nlet productViewFix = new FixProductViewed(LivePluginType.enrichment, null)\n\n// add it to the top level analytics instance.  any track events that come\n// through the system will now have event.properties.product_did renamed\n// to product_id.\nanalytics.add(productViewFix)\n\n\n// This live plugin will remove advertisingId from all events going to Amplitude\nclass RemoveAdvertisingId extends LivePlugin {\n\tprocess(event) {\n\t\t// delete the advertisingId\n\t\tdelete event.context.device.advertisingId\n\t\treturn super.process(event)\n\t}\n}\n\n// create an instance of our FixProductViewed live plugin\nlet deleteAdID = new RemoveAdvertisingId(LivePluginType.enrichment, \"Amplitude\")\n// add it to the top level analytics instance.  All events going to amplitude\n// will now have the advertisingId property removed.\nanalytics.add(deleteAdID)\n\n\n// This live plugin will reissue/convert a specific track event into a \n// screen event.\nclass ConvertTrackToScreen extends LivePlugin {\n\ttrack(event) {\n\t\t// if the event name matches ...\n\t\tif event.name == \"Screen Viewed\" {\n\t\t\t// issue a screen event instead\n\t\t\tanalytics.screen(event.name)\n\t\t}\n\t\t// returning null to prevent the original event from\n\t\t// moving forward.\n\t\treturn null\n\t}\n}\n\n// create an instance of our ConvertTrackToScreen live plugin\nlet convert = new ConvertTrackToScreen(LivePluginType.enrichment, null)\n// add it to the top level analytics instance.  Track events matching\n// the specified event name will be converted to screen calls instead.\nanalytics.add(convert)\n```\n\n### Using the Analytics classes within Live Plugins runtime\n```javascript\n// Issue a simple track call into the system.  This will \nanalytics.track(\"My Event\")\n\n// Create a new instance of Analytics that points to a different write key\nlet myAnalytics = new Analytics(\"\u003cALTERNATE WRITE KEY\u003e\")\nmyAnalytics.track(\"New analytics instance started.\")\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsegment-integrations%2Fanalytics-kotlin-live","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsegment-integrations%2Fanalytics-kotlin-live","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsegment-integrations%2Fanalytics-kotlin-live/lists"}