Skip to content

Commit adeb09a

Browse files
authored
Add auto instrumentation settings (#279)
* add auto instrumentation settings * add subscribe and unsubscribe userinfo * update ci
1 parent 13148c0 commit adeb09a

File tree

5 files changed

+84
-4
lines changed

5 files changed

+84
-4
lines changed

.github/workflows/build.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ jobs:
2121
runs-on: ubuntu-22.04
2222

2323
steps:
24-
- uses: actions/checkout@v2
24+
- uses: actions/checkout@v4
25+
- name: Set up JDK 11
26+
uses: actions/setup-java@v4
27+
with:
28+
distribution: 'temurin'
29+
java-version: '11'
30+
2531
- name: Grant execute permission for gradlew
2632
run: chmod +x gradlew
2733
- name: cache gradle dependencies

.github/workflows/release.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ jobs:
1010
runs-on: ubuntu-22.04
1111
environment: deployment
1212
steps:
13-
- uses: actions/checkout@v2
13+
- uses: actions/checkout@v4
14+
- name: Set up JDK 11
15+
uses: actions/setup-java@v4
16+
with:
17+
distribution: 'temurin'
18+
java-version: '11'
1419
- name: Get tag
1520
id: vars
1621
run: echo ::set-output name=tag::${GITHUB_REF#refs/*/}

.github/workflows/snapshot.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ jobs:
99
runs-on: ubuntu-22.04
1010
environment: deployment
1111
steps:
12-
- uses: actions/checkout@v2
12+
- uses: actions/checkout@v4
13+
- name: Set up JDK 11
14+
uses: actions/setup-java@v4
15+
with:
16+
distribution: 'temurin'
17+
java-version: '11'
1318
- name: Grant execute permission for gradlew
1419
run: chmod +x gradlew
1520
- name: cache gradle dependencies

core/src/main/java/com/segment/analytics/kotlin/core/Analytics.kt

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,69 @@ open class Analytics protected constructor(
700700
return anonymousId()
701701
}
702702

703+
/**
704+
* Subscribes to UserInfo state changes.
705+
*
706+
* The handler is called immediately with the current UserInfo, then again whenever
707+
* the user's identity, traits, or referrer changes. The subscription remains active
708+
* for the lifetime of the Analytics instance unless explicitly unsubscribed.
709+
*
710+
* - Parameter handler: A closure called on the main queue with updated UserInfo.
711+
*
712+
* - Returns: A subscription ID that can be passed to `unsubscribe(_:)` to stop
713+
* receiving updates. If you don't need to unsubscribe, you can ignore the return value.
714+
*
715+
* - Note: Multiple calls create multiple independent subscriptions.
716+
*
717+
* ## Example
718+
* ```kotlin
719+
* // Subscribe for the lifetime of Analytics
720+
* analytics.subscribeToUserInfo { userInfo ->
721+
* print("User: ${userInfo.userId ?: userInfo.anonymousId}")
722+
* }
723+
*
724+
* // Subscribe with manual cleanup
725+
* val subscriptionId = analytics.subscribeToUserInfo { userInfo ->
726+
* // ... handle update
727+
* }
728+
* // Later, when you're done...
729+
* analytics.unsubscribe(subscriptionId)
730+
* ```
731+
*
732+
*/
733+
suspend fun subscribeToUserInfo(handler: (UserInfo) -> Unit) =
734+
store.subscribe(
735+
this,
736+
UserInfo::class,
737+
initialState = true,
738+
handler = handler,
739+
queue = fileIODispatcher
740+
)
741+
742+
/**
743+
* Unsubscribes from state updates.
744+
*
745+
* Stops receiving updates for the subscription associated with the given ID.
746+
* After calling this, the handler will no longer be invoked for state changes.
747+
*
748+
* - Parameter id: The subscription ID returned from a previous subscribe call.
749+
*
750+
* - Note: Unsubscribing an already-unsubscribed or invalid ID is a no-op.
751+
*
752+
* ## Example
753+
* ```kotlin
754+
* val id = analytics.subscribeToUserInfo { userInfo ->
755+
* print("User changed: ${userInfo.userId ?: "anonymous"}")
756+
* }
757+
*
758+
* // Later, stop listening
759+
* analytics.unsubscribe(id)
760+
* ```
761+
*/
762+
suspend fun unsubscribe(id: Int) {
763+
store.unsubscribe(id)
764+
}
765+
703766
/**
704767
* Retrieve the version of this library in use.
705768
* - Returns: A string representing the version in "BREAKING.FEATURE.FIX" format.

core/src/main/java/com/segment/analytics/kotlin/core/Settings.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ data class Settings(
2222
var edgeFunction: JsonObject = emptyJsonObject,
2323
var middlewareSettings: JsonObject = emptyJsonObject,
2424
var metrics: JsonObject = emptyJsonObject,
25-
var consentSettings: JsonObject = emptyJsonObject
25+
var consentSettings: JsonObject = emptyJsonObject,
26+
var autoInstrumentation: JsonObject = emptyJsonObject
2627
) {
2728
inline fun <reified T : Any> destinationSettings(
2829
name: String,

0 commit comments

Comments
 (0)