You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/connections/sources/catalog/libraries/mobile/android/index.md
+202-3Lines changed: 202 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -96,6 +96,17 @@ Analytics analytics = new Analytics.Builder(context, YOUR_WRITE_KEY)
96
96
Analytics.setSingletonInstance(analytics);
97
97
```
98
98
99
+
```kotlin
100
+
// Create an analytics client with the given context and Segment write key.
101
+
val analytics =Analytics.Builder(context, YOUR_WRITE_KEY)
102
+
.trackApplicationLifecycleEvents() // Enable this to record certain application events automatically!
103
+
.recordScreenViews() // Enable this to record screen views automatically!
104
+
.build()
105
+
106
+
// Set the initialized instance as a globally accessible instance.
107
+
Analytics.setSingletonInstance(analytics);
108
+
```
109
+
99
110
**Notes**:
100
111
- You can automatically track lifecycle events such as `Application Opened`, `Application Installed`, `Application Updated` to start quickly with core events. These are optional, but highly recommended.
101
112
- This only installs the Segment destination. This means that all your data is sent server-side to tools. To bundle additional destinations client-side, you'll need to take some additional steps as [shown here](/docs/connections/sources/catalog/libraries/mobile/android/#packaging-sdks-for-device-mode-destinations).
@@ -108,6 +119,10 @@ The entry point of the library is through the `Analytics` class. As you might ha
val analytics =Analytics.Builder(context, writeKey).build()
124
+
```
125
+
111
126
The `Analytics.Builder` class lets you customize settings for the Analytics client, including things like the flush interval and packaging Device-mode destinations. Refer to the Javadocs for details on customizable parameters.
112
127
113
128
We also maintain a global default instance which is initialized with defaults suitable to most implementations.
// You can also register your custom instance as a global singleton.
138
+
Analytics.setSingletonInstance(analytics)
139
+
Analytics.with(context).track(...)
140
+
```
141
+
121
142
In general, Segment recommends that you use the Builder method because it provides the most flexibility. Remember you can call `Analytics.setSingletonInstance` only _ONCE_, so it's best to put the initialization code inside your custom Application class.
122
143
123
144
```java
@@ -132,6 +153,18 @@ public class MyApp extends Application {
132
153
}
133
154
```
134
155
156
+
```kotlin
157
+
classMyApp : Application() {
158
+
overridefunonCreate() {
159
+
val analytics =Analytics.Builder(context, writeKey).build()
160
+
Analytics.setSingletonInstance(analytics)
161
+
162
+
// Safely call Analytics.with(context) from anywhere within your app!
Once you initialize an Analytics client, you can safely call any of its tracking methods from any thread. These events are dispatched asynchronously to the Segment servers and to any Device-mode destinations.
Segment recommends that you make an Identify call once when the user's first creates an account, and only using the Identify call later when their traits change. Segment remembers the previous userIDs and merges the new traits with the old ones.
@@ -300,6 +369,12 @@ Analytics analytics = new Analytics.Builder(context, writeKey)
300
369
.build();
301
370
```
302
371
372
+
```kotlin
373
+
val analytics =Analytics.Builder(context, writeKey)
374
+
.recordScreenViews()
375
+
.build()
376
+
```
377
+
303
378
### Group
304
379
305
380
Group calls let you associate an [identified user](/docs/connections/sources/catalog/libraries/server/java/#identify) user with a group. A group could be a company, organization, account, project or team! It also lets you record custom traits about the group, like industry or number of employees.
@@ -312,6 +387,10 @@ Example `group` call:
312
387
Analytics.with(context).group("a user's id", "a group id", newTraits().putEmployees(20));
313
388
```
314
389
390
+
```kotlin
391
+
Analytics.with(context).group("a user's id", "a group id", Traits().putEmployees(20))
392
+
```
393
+
315
394
The `group` call has the following fields:
316
395
317
396
<tableclass="api-table">
@@ -344,10 +423,15 @@ Alias is how you associate one identity with another. This is an advanced method
val analytics =Analytics.Builder(context, writeKey).defaultOptions(defaultOptions).build()
490
+
val context = analytics.getContext()
491
+
context.clear()
492
+
```
493
+
394
494
## Routing collected data
395
495
396
496
Once you set up calls using the basic Segment data collection APIs, choose which destinations to send it to, and how you want to send it to them.
@@ -435,6 +535,14 @@ Analytics analytics = new Analytics.Builder(context, writeKey)
435
535
.build();
436
536
```
437
537
538
+
```kotlin
539
+
val analytics =Analytics.Builder(context, writeKey)
540
+
.use(GoogleAnalyticsIntegration.FACTORY)
541
+
.use(BranchIntegration.FACTORY)
542
+
...
543
+
.build()
544
+
```
545
+
438
546
### Selecting Destinations
439
547
440
548
You can pass an `options` object on any of the basic Segment API calls that allows you to turn specific destinations on or off. By default, all destinations are enabled. (In Segment's other libraries, you could do this in the list of `integrations` inside the `options` object.)
@@ -452,6 +560,17 @@ Analytics.with(context).track("Purchased Item", new Properties(), new Options().
If you build your own instance of the client, you can also specify a default `options` object to use for each call. In the example below, _NONE_ of the analytics events are sent to Heap.
Notice that the first example uses an Enum to disable the destination, but the second example uses a String. Segment recommends that you use the Enum method for Device-mode destinations, and use the String method to change the behavior of Cloud-mode destinations. The Enum method ensures type safety, and prevents you from accidentally disabling "GoogleAnalytics" instead of "Google Analytics", while the String method gives you more flexibility in what options you pass to cloud-mode destinations.
471
603
472
604
Destination name flags are **case sensitive** and match [the destination's name in the docs](/docs/connections/destinations/catalog/) In some cases where a destination's name has more than one spelling (for example if it changed names, or brand capitalization styles, or if it was commonly misspelled and we added an alias) the documentation for that destination will include a section called "Adding (destination name) to the integrations object".
@@ -483,7 +615,7 @@ Destination name flags are **case sensitive** and match [the destination's name
483
615
You can retrieve the `anonymousId` set by the library by using:
val snapshot =Analytics.with(context).getSnapshot()
648
+
log(snapshot.integrationOperationAverageDuration)
649
+
log(snapshot.flushCount)
650
+
```
651
+
514
652
### Adding debug logging
515
653
516
654
If you run into issues while using the Android library, you can enable logging to help trace the issue. Logging also helps you see how long destinations take to complete their calls so you can find performance bottlenecks.
@@ -521,6 +659,10 @@ The logging is enabled by default in the default singleton instance if your appl
val analytics =Analytics.Builder(context, writeKey).logLevel(LogLevel.VERBOSE)...build()
664
+
```
665
+
524
666
You can choose to disable logging completely (`LogLevel.NONE`), enable basic logging for the SDK (`LogLevel.BASIC`), enable basic logging for Device-mode destination (`LogLevel.INFO`), or simply log everything (`LogLevel.VERBOSE`).
525
667
526
668
> success ""
@@ -539,6 +681,12 @@ public void optOut(boolean optOut) {
539
681
}
540
682
```
541
683
684
+
```kotlin
685
+
funoptOut(optOut: boolean) {
686
+
this.optOut.set(optOut)
687
+
}
688
+
```
689
+
542
690
Set the opt-out status for the current device and analytics client combination. This flag
543
691
persists across device reboots, so you can call it once in your application,
544
692
such as in a screen where a user can opt out of analytics tracking.
Find details on **best practices in event naming** as well as the **Track method payload** in the [Segment Track call spec](/docs/connections/spec/track/).
0 commit comments