Class AppUsage
-
- All Implemented Interfaces:
public final class AppUsage
The mobile services server supports a specific format of usage data and event types. Its client usage implementation, AppUsage, follows the Usage Framework API (Usage) and provides simplified utility methods and encrypted usage store.
1. Initializing AppUsage-- A one-time initialization is required via initialize. It is recommended to use
EncryptionUtil
to obtain the encryption key. If this is the first timeAppUsage
is being initialized andnull
is provided as the encryption key, an encryption key will be generated transparently and will be used for subsequentinitialize
calls.// Application Id and version will be used during upload to form the mobile services URL. byte[] encryptionKey = EncryptionUtil.getEncryptionKey("aliasForAppUsage", myPasscode); AppUsage.initialize(appContext, "myUsageStore", settingsParameter, encryptionKey);
2. Reporting-- All usage reported will be persisted to a encrypted backing store. The reporting needs to begin with a sessionStart, followed by one or more
event()
or pairs ofeventStart()
andeventEnd()
methods, then end with a sessionEnd method.Repeat these method call sequences if you want to have multiple report sessions.
If the
sessionStart()
orsessionEnd()
method is missing, all events reported will be skipped.Use one of the
event()
methods if you know the duration. Otherwise, use a pair ofeventStart()
andeventEnd()
methods, and the duration will be calculated for you.The system default target ID will be used when a target ID is not specified for the reporting methods.
// 1. Marks the beginning of a Session. AppUsage.sessionStart(); // 2. Adds one or more events. AppUsage.event( "eventType1", // Event type, optional. "testEvent1Key", // Event key. When not specified, 'other' will be used. 4L, // Duration. new AppUsageInfo() // Event details using AppUsageInfo. .screen("firstScreen") // Chained method from AppUsageInfo. .value("randomValue")); // Chained method from AppUsageInfo. AppUsage.event( null, // Null event type. "testEvent2Key", 4L, new AppUsageInfo() .screen("second screen") .category("value2")); // Or use eventStart and eventEnd pairs, the duration will be calculated for you. AppUsage.eventStart( "eventType2", "key1", new AppUsageInfo() .screen("third screen") .value("value2")); AppUsage.eventEnd( "eventType2", "key1", new AppUsageInfo() .measurement("measurement")); AppUsage.eventStart( // The default event key 'other' is used when no key is specified. "eventType3", "key3", new AppUsageInfo() .screen("home") .others("otherValue")); AppUsage.eventEnd( "eventType3", "key3", new AppUsageInfo() .screen("home") .others("otherValue")); // Marks the end of a session. Can have more than one sessions using pairs of sessionStart() // and sessionEnd(). AppUsage.sessionEnd();
3. Uploading-- Before using AppUsageUploader, you need to enable the server to upload application-specific usage statistics and reports from mobile devices.
See Defining Usage Report Policy.
AppUsageUploader
runs in anAsyncTask
, it retrieves the snapshot from the usage store, converts the usage records to the format required by mobile services, then uploads them to the server.// Establishes a connection to mobile services using okHttpClient. .... // Uploads to the server. AppUsageUploader.upload(okHttpClient, targetID);
4. Viewing Usage Reports on Server-- After successful client data upload, you can find your reports on the server.
-
-
Method Summary
Modifier and Type Method Description static void
initialize(@NonNull() Context context, @NonNull() String storeName, @NonNull() SettingsParameters settingsParameters, @Nullable() Array<byte> encryptionKey)
Initializes the mobile services usage. static void
initialize(@NonNull() Context context, @NonNull() String storeName, @Nullable() Array<byte> encryptionKey)
Initializes the mobile services usage. static boolean
isInitialized()
Checks if AppUsage has been initialized. static void
changeEncryptionKey(@Nullable() Array<byte> newEncryptionKey)
Changes the encryption key of the store while the store is open. static void
sessionStart(@NonNull() String targetId)
Reports a SESSION_START event for the specified targetID. static void
sessionStart()
Reports a SESSION_START event for the system default targetID. static void
sessionEnd(@NonNull() String targetId)
Reports a SESSION_END event for the specified targetID. static void
sessionEnd()
Reports a SESSION_END event for the system default targetID. static void
event(@NonNull() String targetId, @Nullable() String type, @Nullable() String key, @Nullable() Long duration, @NonNull() AppUsageInfo info)
Reports an EVENT event for the system default targetID. static void
event(@Nullable() String type, @Nullable() String key, @Nullable() Long duration, @NonNull() AppUsageInfo info)
Reports an EVENT event for the system default targetID. static void
eventStart(@NonNull() String targetId, @Nullable() String type, @Nullable() String key, @NonNull() AppUsageInfo info)
Reports an EVENT_START event for the system default targetID. static void
eventStart(@Nullable() String type, @Nullable() String key, @NonNull() AppUsageInfo info)
Reports an EVENT_START event for the system default targetID. static void
eventEnd(@NonNull() String targetId, @Nullable() String type, @Nullable() String key, @NonNull() AppUsageInfo info)
Reports an EVENT_END event for the system default targetID. static void
eventEnd(@Nullable() String type, @Nullable() String key, @NonNull() AppUsageInfo info)
Reports an EVENT_END event for the system default targetID. static void
eventBehaviorViewDisplayed(@NonNull() String viewId, @Nullable() String elementId, @Nullable() String action, @Nullable() String interactionValue)
Submit a ViewDisplayed analytics behavior event. static void
eventBehaviorUserInteraction(@NonNull() String viewId, @Nullable() String elementId, @Nullable() String action, @Nullable() String interactionValue)
Submit a UserInteraction analytics behavior event. static void
deleteStore()
Deletes the underlying usage persistence store and leaves AppUsage
in un-initialized state, that is, isInitialized would returnfalse
after calling this method.static void
enableReporter(@Nullable() String targetId, boolean enable)
Enables or disables a usage reporter with the specified target ID. -
-
Method Detail
-
initialize
static void initialize(@NonNull() Context context, @NonNull() String storeName, @NonNull() SettingsParameters settingsParameters, @Nullable() Array<byte> encryptionKey)
Initializes the mobile services usage.
- Parameters:
context
- Android application contextstoreName
- the name of the usage record persistence storesettingsParameters
- Application ID, device ID, and application version will be retrieved from this object.encryptionKey
- the key to encrypt the underlying persistence store.
-
initialize
static void initialize(@NonNull() Context context, @NonNull() String storeName, @Nullable() Array<byte> encryptionKey)
Initializes the mobile services usage.
- Parameters:
context
- Android application contextstoreName
- the name of the usage record persistence storeencryptionKey
- the key to encrypt the underlying persistence store.
-
isInitialized
static boolean isInitialized()
Checks if AppUsage has been initialized.
- Returns:
True
if AppUsage has been initialized,false
otherwise.
-
changeEncryptionKey
static void changeEncryptionKey(@Nullable() Array<byte> newEncryptionKey)
Changes the encryption key of the store while the store is open.
- Parameters:
newEncryptionKey
- new encryption key.
-
sessionStart
static void sessionStart(@NonNull() String targetId)
Reports a SESSION_START event for the specified targetID.
- Parameters:
targetId
- target ID of a usage reporter
-
sessionStart
static void sessionStart()
Reports a SESSION_START event for the system default targetID.
-
sessionEnd
static void sessionEnd(@NonNull() String targetId)
Reports a SESSION_END event for the specified targetID.
- Parameters:
targetId
- target ID of a usage reporter
-
sessionEnd
static void sessionEnd()
Reports a SESSION_END event for the system default targetID.
-
event
static void event(@NonNull() String targetId, @Nullable() String type, @Nullable() String key, @Nullable() Long duration, @NonNull() AppUsageInfo info)
Reports an EVENT event for the system default targetID.
- Parameters:
targetId
- target ID of a usage reportertype
- type of the eventkey
- event key.duration
- duration of the event in milliseconds as Long.info
- a map of additional attributes that should be reported for the event
-
event
static void event(@Nullable() String type, @Nullable() String key, @Nullable() Long duration, @NonNull() AppUsageInfo info)
Reports an EVENT event for the system default targetID.
- Parameters:
type
- type of the eventkey
- event key.duration
- duration of the event in milliseconds as Long.info
- a map of additional attributes that should be reported for the event
-
eventStart
static void eventStart(@NonNull() String targetId, @Nullable() String type, @Nullable() String key, @NonNull() AppUsageInfo info)
Reports an EVENT_START event for the system default targetID.
Note that the user needs to call eventEnd (or its variant) with the same key, and the time between the reported
EVENT_START
andEVENT_END
will be calculated as the duration.If
eventEnd
method is not called, thisEVENT_START
will be ignored.- Parameters:
targetId
- target ID of a usage reportertype
- type of the eventkey
- event key.info
- a map of additional attributes that should be reported for the event
-
eventStart
static void eventStart(@Nullable() String type, @Nullable() String key, @NonNull() AppUsageInfo info)
Reports an EVENT_START event for the system default targetID.
Note that the user needs to call eventEnd (or its variant) with the same key, and the time between the reported
EVENT_START
andEVENT_END
will be calculated as the duration.If
eventEnd
method is not called, thisEVENT_START
will be ignored.- Parameters:
type
- type of the eventkey
- event key.info
- a map of additional attributes that should be reported for the event
-
eventEnd
static void eventEnd(@NonNull() String targetId, @Nullable() String type, @Nullable() String key, @NonNull() AppUsageInfo info)
Reports an EVENT_END event for the system default targetID.
Note that before calling this method, eventStart (or one of its variant) must be called, otherwise, this
EVENT_END
will be discarded.The time between the reported
EVENT_START
andEVENT_END
will be calculated as the duration.- Parameters:
targetId
- target ID of a usage reportertype
- type of the eventkey
- event key.info
- a map of additional attributes that should be reported for the event
-
eventEnd
static void eventEnd(@Nullable() String type, @Nullable() String key, @NonNull() AppUsageInfo info)
Reports an EVENT_END event for the system default targetID.
Note that before calling this method, eventStart (or one of its variant) must be called, otherwise, this
EVENT_END
will be discarded.The time between the reported
EVENT_START
andEVENT_END
will be calculated as the duration.- Parameters:
type
- type of the eventkey
- event key.info
- a map of additional attributes that should be reported for the event
-
eventBehaviorViewDisplayed
static void eventBehaviorViewDisplayed(@NonNull() String viewId, @Nullable() String elementId, @Nullable() String action, @Nullable() String interactionValue)
Submit a ViewDisplayed analytics behavior event.
- Parameters:
viewId
- Screen/View nameelementId
- interacted UI Element or on screen controlaction
- user performed actioninteractionValue
- value related to the interaction, if applicable
-
eventBehaviorUserInteraction
static void eventBehaviorUserInteraction(@NonNull() String viewId, @Nullable() String elementId, @Nullable() String action, @Nullable() String interactionValue)
Submit a UserInteraction analytics behavior event. For example, a Button-click or row select.
- Parameters:
viewId
- Screen/View nameelementId
- interacted UI Element or on screen controlaction
- user performed action.interactionValue
- value related to the interaction, if applicable.
-
deleteStore
static void deleteStore()
Deletes the underlying usage persistence store and leaves
AppUsage
in un-initialized state, that is, isInitialized would returnfalse
after calling this method.
-
enableReporter
static void enableReporter(@Nullable() String targetId, boolean enable)
Enables or disables a usage reporter with the specified target ID.
- Parameters:
targetId
- target ID of a usage reporter.enable
- Enables the reporter iftrue
.
-
-
-
-