public class Analytics
extends java.lang.Object
The idea is simple: one pipeline for all your data. Segment is the single hub to collect, translate and route your data with the flip of a switch.
Analytics for Android will automatically batch events, queue them to disk, and upload it periodically to Segment for you. It will also look up your project's settings (that you've configured in the web interface), specifically looking up settings for bundled integrations, and then initialize them for you on the user's phone, and mapping our standardized events to formats they can all understand. You only need to instrument Segment once, then flip a switch to install new tools.
This class is the main entry point into the client API. Use with(android.content.Context)
for the global singleton instance or construct your own instance
with Analytics.Builder
.
Modifier and Type | Class and Description |
---|---|
static class |
Analytics.Builder
Fluent API for creating
Analytics instances. |
static class |
Analytics.BundledIntegration
Deprecated.
|
static interface |
Analytics.Callback<T>
A callback interface that is invoked when the Analytics client initializes bundled
integrations.
|
static class |
Analytics.LogLevel
Controls the level of logging.
|
Modifier and Type | Method and Description |
---|---|
void |
alias(java.lang.String newId) |
void |
alias(java.lang.String newId,
Options options)
The alias method is used to merge two user identities, effectively connecting two sets of user
data as one.
|
void |
flush()
Asynchronously flushes all messages in the queue to the server, and tells bundled integrations
to do the same.
|
AnalyticsContext |
getAnalyticsContext()
Get the
AnalyticsContext used by this instance. |
android.app.Application |
getApplication()
Return the
Application used to create this instance. |
Logger |
getLogger()
Deprecated.
This will be removed in a future release.
|
Analytics.LogLevel |
getLogLevel()
Deprecated.
This will be removed in a future release.
|
StatsSnapshot |
getSnapshot()
Creates a
StatsSnapshot of the current stats for this instance. |
void |
group(java.lang.String groupId) |
void |
group(java.lang.String groupId,
Traits traits) |
void |
group(java.lang.String groupId,
Traits groupTraits,
Options options)
The group method lets you associate a user with a group.
|
void |
identify(java.lang.String userId) |
void |
identify(java.lang.String userId,
Traits newTraits,
Options options)
Identify lets you tie one of your users and their actions to a recognizable
userId . |
void |
identify(Traits traits) |
Logger |
logger(java.lang.String tag)
Return a new
Logger with the given sub-tag. |
void |
logout()
Deprecated.
Use
reset() instead |
void |
onIntegrationReady(Analytics.BundledIntegration integration,
Analytics.Callback callback)
Deprecated.
Use
onIntegrationReady(String, Callback) instead. |
<T> void |
onIntegrationReady(java.lang.String key,
Analytics.Callback<T> callback)
Register to be notified when a bundled integration is ready.
|
void |
optOut(boolean optOut)
Set the opt-out status for the current device and analytics client combination.
|
void |
reset()
Resets the analytics client by clearing any stored information about the user.
|
void |
screen(java.lang.String name) |
void |
screen(java.lang.String name,
Properties properties) |
void |
screen(java.lang.String category,
java.lang.String name)
Deprecated.
Use
screen(String) instead. |
void |
screen(java.lang.String category,
java.lang.String name,
Properties properties)
Deprecated.
Use
screen(String, Properties) instead. |
void |
screen(java.lang.String category,
java.lang.String name,
Properties properties,
Options options)
The screen methods let your record whenever a user sees a screen of your mobile app, and attach
a name, category or properties to the screen.
|
static void |
setSingletonInstance(Analytics analytics)
Set the global instance returned from
with(android.content.Context) . |
void |
shutdown()
Stops this instance from accepting further requests.
|
void |
track(java.lang.String event) |
void |
track(java.lang.String event,
Properties properties) |
void |
track(java.lang.String event,
Properties properties,
Options options)
The track method is how you record any actions your users perform.
|
static Analytics |
with(android.content.Context context)
Return a reference to the global default
Analytics instance. |
public static Analytics with(android.content.Context context)
Analytics
instance.
This instance is automatically initialized with defaults that are suitable to most implementations.
If these settings do not meet the requirements of your application, you can override
defaults in analytics.xml
, or you can construct your own instance with full control
over the configuration by using Analytics.Builder
.
By default, events are uploaded every 30 seconds, or every 20 events (whichever occurs first), and debugging is disabled.
public static void setSingletonInstance(Analytics analytics)
with(android.content.Context)
.
This method must be called before any calls to with(android.content.Context)
and may only be called once.
public void identify(@NonNull java.lang.String userId)
identify(String, Traits, Options)
public void identify(@NonNull Traits traits)
identify(String, Traits, Options)
public void identify(@Nullable java.lang.String userId, @Nullable Traits newTraits, @Nullable Options options)
userId
. It
also lets you record traits
about the user, like their email, name, account type, etc.
Traits and userId will be automatically cached and available on future sessions for the same
user. To update a trait on the server, call identify with the same user id (or null). You can
also use identify(Traits)
for this purpose.
userId
- Unique identifier which you recognize a user by in your own database. If this is
null or empty, any previous id we have (could be the anonymous id) will be used.newTraits
- Traits about the user.options
- To configure the call.java.lang.IllegalArgumentException
- if both userId
and newTraits
are not providedpublic void group(@NonNull java.lang.String groupId)
group(String, Traits, Options)
public void group(@NonNull java.lang.String groupId, @Nullable Traits traits)
group(String, Traits, Options)
public void group(@NonNull java.lang.String groupId, @Nullable Traits groupTraits, @Nullable Options options)
If you've called identify(String, Traits, Options)
before, this will automatically
remember the userId. If not, it will fall back to use the anonymousId instead.
groupId
- Unique identifier which you recognize a group by in your own database. Must not
be null or empty.options
- To configure the call.java.lang.IllegalArgumentException
- if groupId is null or an empty string.public void track(@NonNull java.lang.String event)
track(String, Properties, Options)
public void track(@NonNull java.lang.String event, @Nullable Properties properties)
track(String, Properties, Options)
public void track(@NonNull java.lang.String event, @Nullable Properties properties, @Nullable Options options)
event
- Name of the event. Must not be null or empty.properties
- Properties
to add extra information to this call.options
- To configure the call.java.lang.IllegalArgumentException
- if event name is null or an empty string.public void screen(@Nullable java.lang.String category, @Nullable java.lang.String name)
screen(String)
instead.public void screen(@Nullable java.lang.String category, @Nullable java.lang.String name, @Nullable Properties properties)
screen(String, Properties)
instead.public void screen(@Nullable java.lang.String name)
public void screen(@Nullable java.lang.String name, @Nullable Properties properties)
public void screen(@Nullable java.lang.String category, @Nullable java.lang.String name, @Nullable Properties properties, @Nullable Options options)
category
- A category to describe the screen. Deprecated.name
- A name for the screen.properties
- Properties
to add extra information to this call.options
- To configure the call.public void alias(@NonNull java.lang.String newId)
alias(String, Options)
public void alias(@NonNull java.lang.String newId, @Nullable Options options)
Usage:
analytics.track("user did something");
analytics.alias(newId);
analytics.identify(newId);
newId
- The new ID you want to alias the existing ID to. The existing ID will be either
the previousId if you have called identify, or the anonymous ID.options
- To configure the calljava.lang.IllegalArgumentException
- if newId is null or emptypublic void flush()
public AnalyticsContext getAnalyticsContext()
AnalyticsContext
used by this instance.public StatsSnapshot getSnapshot()
StatsSnapshot
of the current stats for this instance.public android.app.Application getApplication()
Application
used to create this instance.@Deprecated public Analytics.LogLevel getLogLevel()
Analytics.LogLevel
for this instance.public Logger getLogger()
Logger
instance used by this client.@Deprecated public void logout()
reset()
insteadpublic void reset()
public void optOut(boolean optOut)
public <T> void onIntegrationReady(java.lang.String key, Analytics.Callback<T> callback)
In most cases, integrations would have already been initialized, and the callback will be invoked fairly quickly. However there may be a latency the first time the app is launched, and we don't have settings for bundled integrations yet. This is compounded if the user is offline on the first run.
You can only register for one callback per integration at a time, and passing in a callback
will remove the previous callback for that integration.
Usage:
analytics.onIntegrationReady("Amplitude", new Callback() {
@Override public void onIntegrationReady(Object instance) {
Amplitude.enableLocationListening();
}
});
analytics.onIntegrationReady("Mixpanel", new Callback<>() {
@Override public void onIntegrationReady(MixpanelAPI mixpanel) {
mixpanel.clearSuperProperties();
}
})*
public void onIntegrationReady(Analytics.BundledIntegration integration, Analytics.Callback callback)
onIntegrationReady(String, Callback)
instead.public void shutdown()