public class AmplitudeClient
extends java.lang.Object
Amplitude.getInstance().logEvent();
Amplitude.getInstance().initialize(this, "APIKEY").enableForegroundTracking(getApplication())
Modifier and Type | Field and Description |
---|---|
protected java.lang.String |
apiKey
The Amplitude App API key.
|
protected android.content.Context |
context
The Android App Context.
|
protected com.amplitude.api.DatabaseHelper |
dbHelper
The shared Amplitude database helper instance.
|
static java.lang.String |
DEVICE_ID_KEY
The pref/database key for the device ID value.
|
protected java.lang.String |
deviceId
The user's Device ID value.
|
static java.lang.String |
END_SESSION_EVENT
The event type for end session events.
|
protected OkHttpClient |
httpClient
The shared OkHTTPClient instance.
|
protected boolean |
initialized |
protected java.lang.String |
instanceName
The name for this instance of AmplitudeClient.
|
static java.lang.String |
LAST_EVENT_ID_KEY
The pref/database key for the last event ID value.
|
static java.lang.String |
LAST_EVENT_TIME_KEY
The pref/database key for the last event time.
|
static java.lang.String |
LAST_IDENTIFY_ID_KEY
The pref/database key for the last identify ID value.
|
static java.lang.String |
OPT_OUT_KEY
The pref/database key for the opt out flag.
|
protected java.lang.String |
platform
The device's Platform value.
|
static java.lang.String |
PREVIOUS_SESSION_ID_KEY
The pref/database key for the previous session ID value.
|
static java.lang.String |
SEQUENCE_NUMBER_KEY
The pref/database key for the sequence number.
|
static java.lang.String |
START_SESSION_EVENT
The event type for start session events.
|
static java.lang.String |
USER_ID_KEY
The pref/database key for the user ID value.
|
protected java.lang.String |
userId
The user's ID value.
|
Constructor and Description |
---|
AmplitudeClient()
Instantiates a new default instance AmplitudeClient and starts worker threads.
|
AmplitudeClient(java.lang.String instance)
Instantiates a new AmplitudeClient with instance name and starts worker threads.
|
Modifier and Type | Method and Description |
---|---|
protected java.lang.String |
bytesToHexString(byte[] bytes)
Internal method to convert bytes to hex string
|
void |
clearUserProperties()
Clear user properties.
|
protected boolean |
contextAndApiKeySet(java.lang.String methodName)
Internal method to check whether application context and api key are set
|
AmplitudeClient |
disableCoppaControl()
Disable COPPA (Children's Online Privacy Protection Act) restrictions on ADID, city, IP address and location tracking.
|
AmplitudeClient |
disableDiagnosticLogging()
Deprecated.
- We removed Diagnostics class and this function has no-op.
Will completely remove it in the near future.
|
AmplitudeClient |
disableLocationListening()
Disable location listening in the SDK.
|
AmplitudeClient |
enableCoppaControl()
Enable COPPA (Children's Online Privacy Protection Act) restrictions on ADID, city, IP address and location tracking.
|
AmplitudeClient |
enableDiagnosticLogging()
Deprecated.
- We removed Diagnostics class and this function has no-op.
Will completely remove it in the near future.
|
AmplitudeClient |
enableForegroundTracking(android.app.Application app)
Enable foreground tracking for the SDK.
|
AmplitudeClient |
enableLocationListening()
Enable location listening in the SDK.
|
AmplitudeClient |
enableLogging(boolean enableLogging)
Enable/disable message logging by the SDK.
|
AmplitudeClient |
enableNewDeviceIdPerInstall(boolean newDeviceIdPerInstall)
Deprecated.
|
protected long |
getCurrentTimeMillis()
Internal method to fetch the current time millis.
|
java.lang.String |
getDeviceId()
Get the current device id.
|
long |
getSessionId()
Gets the current session id.
|
java.lang.String |
getUserId()
Gets the user's id.
|
void |
groupIdentify(java.lang.String groupType,
java.lang.Object groupName,
Identify groupIdentify) |
void |
groupIdentify(java.lang.String groupType,
java.lang.Object groupName,
Identify groupIdentify,
boolean outOfSession) |
void |
identify(Identify identify)
Identify.
|
void |
identify(Identify identify,
boolean outOfSession)
Identify.
|
AmplitudeClient |
initialize(android.content.Context context,
java.lang.String apiKey)
Initialize the Amplitude SDK with the Android application context and your Amplitude
App API key.
|
AmplitudeClient |
initialize(android.content.Context context,
java.lang.String apiKey,
java.lang.String userId)
Initialize the Amplitude SDK with the Android application context, your Amplitude App API
key, and a user ID for the current user.
|
AmplitudeClient |
initialize(android.content.Context context,
java.lang.String apiKey,
java.lang.String userId,
java.lang.String platform,
boolean enableDiagnosticLogging)
Initialize the Amplitude SDK with the Android application context, your Amplitude App API
key, a user ID for the current user, and a custom platform value.
|
boolean |
isOptedOut()
Returns whether or not the user is opted out of tracking.
|
void |
logEvent(java.lang.String eventType)
Log an event with the specified event type.
|
void |
logEvent(java.lang.String eventType,
org.json.JSONObject eventProperties)
Log an event with the specified event type and event properties.
|
void |
logEvent(java.lang.String eventType,
org.json.JSONObject eventProperties,
boolean outOfSession)
Log an event with the specified event type, event properties, with optional out of session
flag.
|
void |
logEvent(java.lang.String eventType,
org.json.JSONObject eventProperties,
org.json.JSONObject groups)
Log an event with the specified event type, event properties, and groups.
|
void |
logEvent(java.lang.String eventType,
org.json.JSONObject eventProperties,
org.json.JSONObject groups,
boolean outOfSession)
Log event with the specified event type, event properties, groups, with optional out of
session flag.
|
protected long |
logEvent(java.lang.String eventType,
org.json.JSONObject eventProperties,
org.json.JSONObject apiProperties,
org.json.JSONObject userProperties,
org.json.JSONObject groups,
org.json.JSONObject groupProperties,
long timestamp,
boolean outOfSession)
Log event.
|
void |
logEvent(java.lang.String eventType,
org.json.JSONObject eventProperties,
org.json.JSONObject groups,
long timestamp,
boolean outOfSession)
Log event with the specified event type, event properties, groups, timestamp, with optional
out of session flag.
|
protected void |
logEventAsync(java.lang.String eventType,
org.json.JSONObject eventProperties,
org.json.JSONObject apiProperties,
org.json.JSONObject userProperties,
org.json.JSONObject groups,
org.json.JSONObject groupProperties,
long timestamp,
boolean outOfSession)
Log event async.
|
void |
logEventSync(java.lang.String eventType)
Log an event with the specified event type.
|
void |
logEventSync(java.lang.String eventType,
org.json.JSONObject eventProperties)
Log an event with the specified event type and event properties.
|
void |
logEventSync(java.lang.String eventType,
org.json.JSONObject eventProperties,
boolean outOfSession)
Log an event with the specified event type, event properties, with optional out of session
flag.
|
void |
logEventSync(java.lang.String eventType,
org.json.JSONObject eventProperties,
org.json.JSONObject groups)
Log an event with the specified event type, event properties, and groups.
|
void |
logEventSync(java.lang.String eventType,
org.json.JSONObject eventProperties,
org.json.JSONObject groups,
boolean outOfSession)
Log event with the specified event type, event properties, groups, with optional out of
session flag.
|
void |
logEventSync(java.lang.String eventType,
org.json.JSONObject eventProperties,
org.json.JSONObject groups,
long timestamp,
boolean outOfSession)
Log event with the specified event type, event properties, groups, timestamp, with optional
sout of ession flag.
|
void |
logRevenue(double amount)
Deprecated.
- use
logRevenueV2 instead |
void |
logRevenue(java.lang.String productId,
int quantity,
double price)
Deprecated.
- use
logRevenueV2 instead |
void |
logRevenue(java.lang.String productId,
int quantity,
double price,
java.lang.String receipt,
java.lang.String receiptSignature)
Deprecated.
- use
logRevenueV2 instead |
void |
logRevenueV2(Revenue revenue)
Log revenue v2.
|
protected void |
makeEventUploadPostRequest(OkHttpClient client,
java.lang.String events,
long maxEventId,
long maxIdentifyId)
Internal method to generate the event upload post request.
|
protected android.util.Pair<android.util.Pair<java.lang.Long,java.lang.Long>,org.json.JSONArray> |
mergeEventsAndIdentifys(java.util.List<org.json.JSONObject> events,
java.util.List<org.json.JSONObject> identifys,
long numEvents)
Internal method to merge unsent events and identifies into a single array by sequence number.
|
AmplitudeClient |
regenerateDeviceId()
Regenerates a new random deviceId for current user.
|
protected java.lang.Object |
replaceWithJSONNull(java.lang.Object obj)
Internal method to replace null event fields with JSON null object.
|
protected void |
runOnLogThread(java.lang.Runnable r) |
protected long |
saveEvent(java.lang.String eventType,
org.json.JSONObject event)
Save event long.
|
AmplitudeClient |
setBearerToken(java.lang.String token)
Set Bearer Token to be included in request header.
|
AmplitudeClient |
setDeviceId(java.lang.String deviceId)
Sets a custom device id.
|
AmplitudeClient |
setDiagnosticEventMaxCount(int eventMaxCount)
Deprecated.
- We removed Diagnostics class and this function has no-op.
Will completely remove it in the near future.
|
AmplitudeClient |
setEventMaxCount(int eventMaxCount)
Sets event max count.
|
AmplitudeClient |
setEventUploadMaxBatchSize(int eventUploadMaxBatchSize)
Sets event upload max batch size.
|
AmplitudeClient |
setEventUploadPeriodMillis(int eventUploadPeriodMillis)
Sets event upload period millis.
|
AmplitudeClient |
setEventUploadThreshold(int eventUploadThreshold)
Sets event upload threshold.
|
AmplitudeClient |
setFlushEventsOnClose(boolean flushEventsOnClose)
Enable/disable flushing of unsent events on app close (enabled by default).
|
void |
setGroup(java.lang.String groupType,
java.lang.Object groupName)
Sets the user's group(s).
|
AmplitudeClient |
setLibraryName(java.lang.String libraryName)
Library name is default as `amplitude-android`.
|
AmplitudeClient |
setLibraryVersion(java.lang.String libraryVersion)
Library version is default as the latest Amplitude Android SDK version.
|
AmplitudeClient |
setLogLevel(int logLevel)
Sets the logging level.
|
AmplitudeClient |
setMinTimeBetweenSessionsMillis(long minTimeBetweenSessionsMillis)
Sets min time between sessions millis.
|
AmplitudeClient |
setOffline(boolean offline)
Sets offline.
|
AmplitudeClient |
setOptOut(boolean optOut)
Sets opt out.
|
AmplitudeClient |
setServerUrl(java.lang.String serverUrl)
Sets a custom server url for event upload.
|
AmplitudeClient |
setSessionTimeoutMillis(long sessionTimeoutMillis)
Sets session timeout millis.
|
AmplitudeClient |
setTrackingOptions(TrackingOptions trackingOptions) |
AmplitudeClient |
setUseDynamicConfig(boolean useDynamicConfig)
Turning this flag on will find the best server url automatically based on users' geo location.
|
AmplitudeClient |
setUserId(java.lang.String userId)
Sets the user id (can be null).
|
AmplitudeClient |
setUserId(java.lang.String userId,
boolean startNewSession)
Sets the user id (can be null).
|
void |
setUserProperties(org.json.JSONObject userProperties)
Sets user properties.
|
void |
setUserProperties(org.json.JSONObject userProperties,
boolean replace)
Deprecated.
|
void |
showEventExplorer(android.app.Activity activity)
Show Amplitude Event Explorer when you're running a debug build.
|
boolean |
startNewSessionIfNeeded(long timestamp)
Public method to start a new session if needed.
|
AmplitudeClient |
trackSessionEvents(boolean trackingSessionEvents)
Track session events amplitude client.
|
org.json.JSONArray |
truncate(org.json.JSONArray array)
Truncate values in a JSON array.
|
org.json.JSONObject |
truncate(org.json.JSONObject object)
Truncate values in a JSON object.
|
static java.lang.String |
truncate(java.lang.String value)
Truncate a string to 1024 characters.
|
protected void |
updateServer()
Internal method to upload unsent events.
|
protected void |
updateServer(boolean limit)
Internal method to upload unsent events.
|
void |
uploadEvents()
Force SDK to upload any unsent events.
|
AmplitudeClient |
useAdvertisingIdForDeviceId()
Whether to use the Android advertising ID (ADID) as the user's device ID.
|
protected boolean |
validateLogEvent(java.lang.String eventType)
Validate the event type being logged.
|
public static final java.lang.String START_SESSION_EVENT
public static final java.lang.String END_SESSION_EVENT
public static final java.lang.String DEVICE_ID_KEY
public static final java.lang.String USER_ID_KEY
public static final java.lang.String OPT_OUT_KEY
public static final java.lang.String SEQUENCE_NUMBER_KEY
public static final java.lang.String LAST_EVENT_TIME_KEY
public static final java.lang.String LAST_EVENT_ID_KEY
public static final java.lang.String LAST_IDENTIFY_ID_KEY
public static final java.lang.String PREVIOUS_SESSION_ID_KEY
protected android.content.Context context
protected OkHttpClient httpClient
protected com.amplitude.api.DatabaseHelper dbHelper
protected java.lang.String apiKey
protected java.lang.String instanceName
protected java.lang.String userId
protected java.lang.String deviceId
protected boolean initialized
protected java.lang.String platform
public AmplitudeClient()
public AmplitudeClient(java.lang.String instance)
instance
- public AmplitudeClient initialize(android.content.Context context, java.lang.String apiKey)
context
- the Android application contextapiKey
- your Amplitude App API keypublic AmplitudeClient initialize(android.content.Context context, java.lang.String apiKey, java.lang.String userId)
context
- the Android application contextapiKey
- your Amplitude App API keyuserId
- the user id to setpublic AmplitudeClient initialize(android.content.Context context, java.lang.String apiKey, java.lang.String userId, java.lang.String platform, boolean enableDiagnosticLogging)
context
- the Android application contextapiKey
- your Amplitude App API keyuserId
- the user id to setpublic AmplitudeClient enableForegroundTracking(android.app.Application app)
app
- the Android applicationpublic AmplitudeClient enableDiagnosticLogging()
public AmplitudeClient disableDiagnosticLogging()
public AmplitudeClient setDiagnosticEventMaxCount(int eventMaxCount)
public AmplitudeClient enableNewDeviceIdPerInstall(boolean newDeviceIdPerInstall)
newDeviceIdPerInstall
- whether to set a new device ID on app install.public AmplitudeClient useAdvertisingIdForDeviceId()
public AmplitudeClient enableLocationListening()
initialize(Context, String)
.public AmplitudeClient disableLocationListening()
initialize(Context, String)
.public AmplitudeClient setEventUploadThreshold(int eventUploadThreshold)
eventUploadThreshold
- the event upload thresholdpublic AmplitudeClient setEventUploadMaxBatchSize(int eventUploadMaxBatchSize)
eventUploadMaxBatchSize
- the event upload max batch sizepublic AmplitudeClient setEventMaxCount(int eventMaxCount)
eventMaxCount
- the event max countpublic AmplitudeClient setEventUploadPeriodMillis(int eventUploadPeriodMillis)
eventUploadPeriodMillis
- the event upload period millispublic AmplitudeClient setMinTimeBetweenSessionsMillis(long minTimeBetweenSessionsMillis)
minTimeBetweenSessionsMillis
- the min time between sessions millispublic AmplitudeClient setServerUrl(java.lang.String serverUrl)
serverUrl
- - a string url for event upload.public AmplitudeClient setBearerToken(java.lang.String token)
token
- public AmplitudeClient setSessionTimeoutMillis(long sessionTimeoutMillis)
sessionTimeoutMillis
- the session timeout millispublic AmplitudeClient setTrackingOptions(TrackingOptions trackingOptions)
public AmplitudeClient enableCoppaControl()
public AmplitudeClient disableCoppaControl()
public AmplitudeClient setOptOut(boolean optOut)
optOut
- whether or not to opt the user out of trackingpublic AmplitudeClient setLibraryName(java.lang.String libraryName)
public AmplitudeClient setLibraryVersion(java.lang.String libraryVersion)
public boolean isOptedOut()
public AmplitudeClient enableLogging(boolean enableLogging)
enableLogging
- whether to enable message logging by the SDK.public AmplitudeClient setLogLevel(int logLevel)
logLevel
- the log levelpublic AmplitudeClient setOffline(boolean offline)
offline
- whether or not the SDK should be offlinepublic AmplitudeClient setFlushEventsOnClose(boolean flushEventsOnClose)
flushEventsOnClose
- whether to flush unsent events on app closepublic AmplitudeClient trackSessionEvents(boolean trackingSessionEvents)
trackingSessionEvents
- whether to enable tracking of session eventspublic AmplitudeClient setUseDynamicConfig(boolean useDynamicConfig)
useDynamicConfig
- whether to enable dynamic configpublic void showEventExplorer(android.app.Activity activity)
activity
- root activitypublic void logEvent(java.lang.String eventType)
eventType
- the event typepublic void logEvent(java.lang.String eventType, org.json.JSONObject eventProperties)
eventType
- the event typeeventProperties
- the event propertiespublic void logEvent(java.lang.String eventType, org.json.JSONObject eventProperties, boolean outOfSession)
eventType
- the event typeeventProperties
- the event propertiesoutOfSession
- the out of sessionpublic void logEvent(java.lang.String eventType, org.json.JSONObject eventProperties, org.json.JSONObject groups)
eventType
- the event typeeventProperties
- the event propertiesgroups
- the groupspublic void logEvent(java.lang.String eventType, org.json.JSONObject eventProperties, org.json.JSONObject groups, boolean outOfSession)
eventType
- the event typeeventProperties
- the event propertiesgroups
- the groupsoutOfSession
- the out of sessionpublic void logEvent(java.lang.String eventType, org.json.JSONObject eventProperties, org.json.JSONObject groups, long timestamp, boolean outOfSession)
eventType
- the event typeeventProperties
- the event propertiesgroups
- the groupstimestamp
- the timestamp in millisecond since epochoutOfSession
- the out of sessionpublic void logEventSync(java.lang.String eventType)
eventType
- the event typepublic void logEventSync(java.lang.String eventType, org.json.JSONObject eventProperties)
eventType
- the event typeeventProperties
- the event propertiespublic void logEventSync(java.lang.String eventType, org.json.JSONObject eventProperties, boolean outOfSession)
eventType
- the event typeeventProperties
- the event propertiesoutOfSession
- the out of sessionpublic void logEventSync(java.lang.String eventType, org.json.JSONObject eventProperties, org.json.JSONObject groups)
eventType
- the event typeeventProperties
- the event propertiesgroups
- the groupspublic void logEventSync(java.lang.String eventType, org.json.JSONObject eventProperties, org.json.JSONObject groups, boolean outOfSession)
eventType
- the event typeeventProperties
- the event propertiesgroups
- the groupsoutOfSession
- the out of sessionpublic void logEventSync(java.lang.String eventType, org.json.JSONObject eventProperties, org.json.JSONObject groups, long timestamp, boolean outOfSession)
eventType
- the event typeeventProperties
- the event propertiesgroups
- the groupstimestamp
- the timestamp in milliseconds since epochoutOfSession
- the out of sessionprotected boolean validateLogEvent(java.lang.String eventType)
eventType
- the event typeprotected void logEventAsync(java.lang.String eventType, org.json.JSONObject eventProperties, org.json.JSONObject apiProperties, org.json.JSONObject userProperties, org.json.JSONObject groups, org.json.JSONObject groupProperties, long timestamp, boolean outOfSession)
eventType
- the event typeeventProperties
- the event propertiesapiProperties
- the api propertiesuserProperties
- the user propertiesgroups
- the groupstimestamp
- the timestampoutOfSession
- the out of sessionprotected long logEvent(java.lang.String eventType, org.json.JSONObject eventProperties, org.json.JSONObject apiProperties, org.json.JSONObject userProperties, org.json.JSONObject groups, org.json.JSONObject groupProperties, long timestamp, boolean outOfSession)
eventType
- the event typeeventProperties
- the event propertiesapiProperties
- the api propertiesuserProperties
- the user propertiesgroups
- the groupstimestamp
- the timestampoutOfSession
- the out of sessionprotected long saveEvent(java.lang.String eventType, org.json.JSONObject event)
eventType
- the event typeevent
- the eventpublic long getSessionId()
public boolean startNewSessionIfNeeded(long timestamp)
timestamp
- the timestamppublic void logRevenue(double amount)
logRevenueV2
insteadamount
- the amountpublic void logRevenue(java.lang.String productId, int quantity, double price)
logRevenueV2
insteadproductId
- the product idquantity
- the quantityprice
- the pricepublic void logRevenue(java.lang.String productId, int quantity, double price, java.lang.String receipt, java.lang.String receiptSignature)
logRevenueV2
insteadproductId
- the product idquantity
- the quantityprice
- the pricereceipt
- the receiptreceiptSignature
- the receipt signaturepublic void logRevenueV2(Revenue revenue)
Revenue
object to hold your revenue data and properties,
and log it as a revenue event using this method.revenue
- a Revenue
objectpublic void setUserProperties(org.json.JSONObject userProperties, boolean replace)
Identify
API to set multiple user properties with a single
command. Note: the replace parameter is deprecated and has no effect.userProperties
- the user propertiesreplace
- the replace - has no effectpublic void setUserProperties(org.json.JSONObject userProperties)
Identify
API to set multiple user properties with a single
command.userProperties
- the user propertiespublic void clearUserProperties()
public void identify(Identify identify)
Identify
object containing
user property operations to Amplitude server.identify
- an Identify
objectpublic void identify(Identify identify, boolean outOfSession)
Identify
object containing
user property operations to Amplitude server. If outOfSession is true, then the identify
event is sent with a session id of -1, and does not trigger any session-handling logic.identify
- an Identify
objectoutOfSession
- whther to log the identify event out of sessionpublic void setGroup(java.lang.String groupType, java.lang.Object groupName)
groupType
- the group type (ex: orgId)groupName
- the group name (ex: 15)public void groupIdentify(java.lang.String groupType, java.lang.Object groupName, Identify groupIdentify)
public void groupIdentify(java.lang.String groupType, java.lang.Object groupName, Identify groupIdentify, boolean outOfSession)
public org.json.JSONObject truncate(org.json.JSONObject object)
object
- the objectpublic org.json.JSONArray truncate(org.json.JSONArray array) throws org.json.JSONException
array
- the arrayorg.json.JSONException
- the json exceptionpublic static java.lang.String truncate(java.lang.String value)
value
- the valuepublic java.lang.String getUserId()
public AmplitudeClient setUserId(java.lang.String userId)
userId
- the user idpublic AmplitudeClient setUserId(java.lang.String userId, boolean startNewSession)
userId
- the user idpublic AmplitudeClient setDeviceId(java.lang.String deviceId)
deviceId
- the device idpublic AmplitudeClient regenerateDeviceId()
public void uploadEvents()
protected void updateServer()
protected void updateServer(boolean limit)
limit
- the limitprotected android.util.Pair<android.util.Pair<java.lang.Long,java.lang.Long>,org.json.JSONArray> mergeEventsAndIdentifys(java.util.List<org.json.JSONObject> events, java.util.List<org.json.JSONObject> identifys, long numEvents) throws org.json.JSONException
events
- the eventsidentifys
- the identifysnumEvents
- the num eventsorg.json.JSONException
- the json exceptionprotected void makeEventUploadPostRequest(OkHttpClient client, java.lang.String events, long maxEventId, long maxIdentifyId)
client
- the clientevents
- the eventsmaxEventId
- the max event idmaxIdentifyId
- the max identify idpublic java.lang.String getDeviceId()
protected void runOnLogThread(java.lang.Runnable r)
protected java.lang.Object replaceWithJSONNull(java.lang.Object obj)
obj
- the objprotected boolean contextAndApiKeySet(java.lang.String methodName)
methodName
- the parent method name to print in error messageprotected java.lang.String bytesToHexString(byte[] bytes)
bytes
- the bytesprotected long getCurrentTimeMillis()