Skip to content

Commit

Permalink
Merge pull request #1 from MyUNiDAYS/initial
Browse files Browse the repository at this point in the history
Initial
  • Loading branch information
Reedyuk authored Nov 25, 2022
2 parents 30a1750 + ef30339 commit 9282359
Show file tree
Hide file tree
Showing 17 changed files with 408 additions and 29 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
- name: Build
run: ./gradlew build
- name: Run create xcframework
run: ./gradlew assembleLibraryXCFramework
run: ./gradlew assembleSegmenktXCFramework
- name: Run publish
run: ./gradlew publish
env:
Expand All @@ -51,7 +51,7 @@ jobs:
env:
GITHUB_TOKEN: ${{ github.token }}
with:
upload_url: https://uploads.github.com/repos/MyUNiDAYS/libraryname/releases/${{ steps.lastRelease.outputs.id }}/assets?name=${{ github.ref_name }}.zip
upload_url: https://uploads.github.com/repos/MyUNiDAYS/Segmenkt/releases/${{ steps.lastRelease.outputs.id }}/assets?name=${{ github.ref_name }}.zip
asset_path: build/${{ github.ref_name }}.zip
asset_name: ${{ github.ref_name }}.zip
asset_content_type: application/zip
8 changes: 8 additions & 0 deletions .idea/artifacts/Segmenkt_jslegacy_0_0_9.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 63 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,44 +1,90 @@
<h1 align="left">Template Kotlin Library
<img alt="GitHub last commit" src="https://img.shields.io/github/last-commit/MyUNiDAYS/template-kotlin-library?style=flat-square"> <a href="https://git.live"><img src="https://img.shields.io/badge/collaborate-on%20gitlive-blueviolet?style=flat-square"></a>
</h1>
<h1 align="left">SegmenKT Kotlin SDK <img alt="GitHub last commit" src="https://img.shields.io/github/last-commit/reedyuk/segmenkt?style=flat-square"> <a href="https://git.live"><img src="https://img.shields.io/badge/collaborate-on%20gitlive-blueviolet?style=flat-square"></a></h1>

The Template Kotlin Library SDK
Segment + Kotlin = SegmenKT

The SegmenKT Kotlin SDK is a Kotlin-first SDK for Segment. Its API is similar to the <a href="https://github.com/segmentio/analytics-android/">Segment Android SDK</a> but also supports <b>multiplatform</b> projects, enabling you to use Segment directly from your common source targeting <strong>iOS</strong> and <strong>Android</strong>.

## Installation

### KMM

```
implementation("com.myunidays:package:0.0.1")
implementation("com.myunidays:segmenkt:0.0.9")
```

### Android

```
implementation("com.myunidays:package-android:0.0.1")
implementation("com.myunidays:segmenkt-android:0.0.9")
```

### iOS

Add to the binary to your swift package like this:
Run gradle task in this project
```
./gradlew assembleXCFramework
```

```swift
.binaryTarget(
name: "project",
url: "https://github.com/MyUNiDAYS/template-kotlin-library/releases/download/0.0.1/0.0.1.zip",
checksum: "8c35293a410f4ec5d150c4f5464f6b5cf04a1a15d1ae9c29126bb0b7a7dc2a54"
),
Locate the framework in the following directory
```
build/XCFrameworks/
```
Add the xcframework file to your xcode project, OR you can grab it from this repo in Examples/ios/frameworks/

### JS

At the moment there is a JS target but it is currently filled with TODO's so it's not recommended to be used yet.

Where 0.0.1 is the release number, you will also need to change the checksum, xcode will tell you the different checksum if its wrong and just update that from the error message.
#### Important

In order for ios to work you will also need to include the segment framework to your ios project.

## How to use

### KMM
Initialise segment with a config

### Android
### Kotlin
```
val segmentConfig = Configuration(
writeKey = WriteKey(
android = "123",
ios = "ABC",
js = "1"
),
context = context
)
Analytics.setupWithConfiguration(segmentConfig)
```

### iOS
No need to define a key for iOS if you are using just Android.

### Swift
```
let segmentConfig = Configuration(writeKey: WriteKey(android: nil, ios: "", js: nil), context: nil)
Analytics.Companion().setupWithConfiguration(configuration: segmentConfig)
```

Then when you want to **Track**, **Identify**, **Group**, **Screen**

### Kotlin
```
Analytics.shared().track("Cool Event")
Analytics.shared().identify("1")
Analytics.shared().group("1")
Analytics.shared().screen("Cool Screen")
```

### Swift
```
Analytics.Companion().shared(context: nil).track(name: "Cool Event", properties: nil)
Analytics.Companion().shared(context: nil).identify(userId: "1", traits: nil)
Analytics.Companion().shared(context: nil).group(groupId: "1", traits: nil)
Analytics.Companion().shared(context: nil).screen(screenTitle: "Cool Screen", properties: nil)
```

## Examples

Run the examples from the examples directory for KMM, Android and iOS

## Contributing

Expand Down
39 changes: 39 additions & 0 deletions library/Segmenkt.podspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
Pod::Spec.new do |spec|
spec.name = 'segmenkt'
spec.version = '0.0.8'
spec.homepage = 'Link to a Kotlin/Native module homepage'
spec.source = { :http=> ''}
spec.authors = ''
spec.license = ''
spec.summary = 'Some description for a Kotlin/Native module'
spec.vendored_frameworks = 'build/cocoapods/framework/segmenkt.framework'
spec.libraries = 'c++'
spec.ios.deployment_target = '10.0'
spec.dependency 'Analytics', '~> 4.1.6'

spec.pod_target_xcconfig = {
'KOTLIN_PROJECT_PATH' => ':',
'PRODUCT_MODULE_NAME' => 'segmenkt',
}

spec.script_phases = [
{
:name => 'Build segmenkt',
:execution_position => :before_compile,
:shell_path => '/bin/sh',
:script => <<-SCRIPT
if [ "YES" = "$COCOAPODS_SKIP_KOTLIN_BUILD" ]; then
echo "Skipping Gradle build task invocation due to COCOAPODS_SKIP_KOTLIN_BUILD environment variable set to \"YES\""
exit 0
fi
set -ev
REPO_ROOT="$PODS_TARGET_SRCROOT"
"$REPO_ROOT/gradlew" -p "$REPO_ROOT" $KOTLIN_PROJECT_PATH:syncFramework \
-Pkotlin.native.cocoapods.platform=$PLATFORM_NAME \
-Pkotlin.native.cocoapods.archs="$ARCHS" \
-Pkotlin.native.cocoapods.configuration="$CONFIGURATION"
SCRIPT
}
]

end
23 changes: 22 additions & 1 deletion library/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ version = MODULE_VERSION_NUMBER

plugins {
kotlin("multiplatform")
kotlin("plugin.serialization") version "1.6.21"
kotlin("native.cocoapods")
id("com.android.library")
id("org.jlleitschuh.gradle.ktlint")
id("io.gitlab.arturbosch.detekt")
Expand Down Expand Up @@ -70,7 +72,11 @@ kotlin {
}
val jsMain by getting
val jsTest by getting
val androidMain by getting
val androidMain by getting {
dependencies {
api("com.segment.analytics.android:analytics:4.10.3")
}
}
val androidTest by getting {
dependencies {
implementation("junit:junit:4.13.2")
Expand Down Expand Up @@ -99,6 +105,21 @@ android {
}
}

kotlin {
cocoapods {
ios.deploymentTarget = "10.0"
noPodspec()
framework { isStatic = true }
pod("Analytics") {
version = "~> 4.1.6"
moduleName = "Segment"
source = git("https://github.com/Reedyuk/analytics-ios.git") {
branch = "master"
}
}
}
}

fun SigningExtension.whenRequired(block: () -> Boolean) {
setRequired(block)
}
Expand Down
16 changes: 8 additions & 8 deletions library/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ signing.keyId=""
signing.password=""

MODULE_PACKAGE_NAME=com.myunidays
MODULE_VERSION_NUMBER=0.0.1
MODULE_NAME=template-kotlin-library
MODULE_VERSION_NUMBER=0.0.9
MODULE_NAME=segmenkt

OPEN_SOURCE_REPO=https://oss.sonatype.org/service/local/staging/deploy/maven2/
PUBLISH_NAME=template-kotlin-library
PUBLISH_DESCRIPTION=
PUBLISH_URL=https://github.com/MyUNiDAYS/template-kotlin-library
PUBLISH_NAME=Segmenkt
PUBLISH_DESCRIPTION=Kotlin Multiplatform Wrapper for Segment
PUBLISH_URL=https://github.com/MyUNiDAYS/Segmenkt

PUBLISH_SCM_URL=https://github.com/MyUNiDAYS/template-kotlin-library
PUBLISH_SCM_CONNECTION=scm:git://git@github.com:MyUNiDAYS/template-kotlin-library.git
PUBLISH_SCM_DEVELOPERCONNECTION=scm:git:ssh://git@github.com:MyUNiDAYS/template-kotlin-library.git
PUBLISH_SCM_URL=https://github.com/MyUNiDAYS/Segmenkt
PUBLISH_SCM_CONNECTION=scm:git://git@github.com:MyUNiDAYS/Segmenkt.git
PUBLISH_SCM_DEVELOPERCONNECTION=scm:git:ssh://git@github.com:MyUNiDAYS/Segmenkt.git
4 changes: 3 additions & 1 deletion library/src/androidMain/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.myunidays.library"/>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.myunidays.segmenkt">

</manifest>
107 changes: 107 additions & 0 deletions library/src/androidMain/kotlin/com/myunidays/segmenkt/Analytics.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package com.myunidays.segmenkt

import android.content.Context
import com.segment.analytics.Options
import com.segment.analytics.Properties
import com.segment.analytics.Traits
import java.util.concurrent.TimeUnit

actual class Analytics internal constructor(val android: com.segment.analytics.Analytics) {

actual companion object {
actual fun setupWithConfiguration(configuration: Configuration): Analytics {
val analyticsConfig = com.segment.analytics.Analytics.Builder(configuration.application as Context, configuration.writeKey)
.collectDeviceId(configuration.collectDeviceId)
.experimentalUseNewLifecycleMethods(configuration.useLifecycleObserver)
.flushInterval(configuration.flushInterval.toLong(), TimeUnit.SECONDS)
.flushQueueSize(configuration.flushAt)
.tag(if (configuration.tag.isNullOrBlank()) configuration.writeKey else configuration.tag + "-" + configuration.writeKey)
if (configuration.trackDeepLinks) analyticsConfig.trackDeepLinks()
if (configuration.trackApplicationLifecycleEvents) analyticsConfig.trackApplicationLifecycleEvents()
configuration.apiHost?.let { analyticsConfig.defaultApiHost(it) }
val analytics = analyticsConfig.build()
com.segment.analytics.Analytics.setSingletonInstance(analytics)
return Analytics(analytics)
}
actual fun shared(context: Any?): Analytics =
Analytics(com.segment.analytics.Analytics.with(context as? Context))
}

actual fun alias(userId: String, options: Map<Any?, *>?) =
android.alias(
userId,
Options().apply {
options?.forEach { property ->
(property.key as? String)?.let { putContext(it, property.value) }
}
}
)

actual fun track(name: String, properties: Map<Any?, *>?, options: Map<Any?, *>?) =
android.track(
name,
Properties().apply {
properties?.forEach { property ->
(property.key as? String)?.let { putValue(it, property.value) }
}
},
Options().apply {
options?.forEach { property ->
(property.key as? String)?.let { putContext(it, property.value) }
}
}
)

actual fun identify(userId: String, traits: Map<Any?, *>?, options: Map<Any?, *>?) =
android.identify(
userId,
Traits().apply {
traits?.forEach { trait ->
(trait.key as? String)?.let { putValue(it, trait.value) }
}
},
Options().apply {
options?.forEach { property ->
(property.key as? String)?.let { putContext(it, property.value) }
}
}
)

actual fun screen(
screenTitle: String,
properties: Map<Any?, *>?,
options: Map<Any?, *>?
) = android.screen(
null,
screenTitle,
Properties().apply {
properties?.forEach { property ->
(property.key as? String)?.let { putValue(it, property.value) }
}
},
Options().apply {
options?.forEach { property ->
(property.key as? String)?.let { putContext(it, property.value) }
}
}
)

actual fun group(groupId: String, traits: Map<Any?, *>?, options: Map<Any?, *>?) =
android.group(
groupId,
Traits().apply {
traits?.forEach { trait ->
(trait.key as? String)?.let { putValue(it, trait.value) }
}
},
Options().apply {
options?.forEach { property ->
(property.key as? String)?.let { putContext(it, property.value) }
}
}
)

actual fun reset() {
android.reset()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.myunidays.segmenkt

actual val platform = PlatformType.ANDROID
17 changes: 17 additions & 0 deletions library/src/commonMain/kotlin/com/myunidays/segmenkt/Analytics.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.myunidays.segmenkt

expect class Analytics {

companion object {
fun setupWithConfiguration(configuration: Configuration): Analytics
fun shared(context: Any? = null): Analytics
}

fun track(name: String, properties: Map<Any?, *>? = null, options: Map<Any?, *>? = null)
fun identify(userId: String, traits: Map<Any?, *>? = null, options: Map<Any?, *>? = null)
fun alias(userId: String, options: Map<Any?, *>? = null)
fun screen(screenTitle: String, properties: Map<Any?, *>? = null, options: Map<Any?, *>? = null)
fun group(groupId: String, traits: Map<Any?, *>? = null, options: Map<Any?, *>? = null)

fun reset()
}
Loading

0 comments on commit 9282359

Please sign in to comment.