Class Analytics
The application-facing entry point for analytics. Analytics holds the
set of registered providers, the user's consent
state and the pseudonymous client id, and fans every reporting call out to
all providers -- but only once the relevant consent has been satisfied.
Typical setup
// register one or more providers (no reflection -- explicit instances)
Analytics.addProvider(new CodenameOneAnalyticsProvider());
Analytics.addProvider(new GoogleAnalyticsProvider("G-XXXX", "api-secret"));
// GDPR: nothing is sent until the user grants consent (opt-in is the default)
Analytics.setConsent(AnalyticsConsent.granted());
Analytics.screen("Home", null);
Analytics.event(AnalyticsEvent.create("purchase").param("value", 9.99).build());
Consent and privacy
The ConsentMode governs behaviour before an explicit choice is
recorded. In the default ConsentMode.OPT_IN mode reporting calls are
silently dropped until setConsent(AnalyticsConsent) grants the
matching category. The consent choice and the client id are persisted in
Preferences so they survive restarts. The client id is not derived
from any hardware identifier and can be cleared with resetClientId()
to honour an erasure request.
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddProvider(AnalyticsProvider provider) Registers a provider and immediately supplies it with the currentAnalyticsContextand consent state.static voidRemoves every registered provider.static StringclientId()The pseudonymous client id, generating and persisting one on first use.static voidcrash(AnalyticsCrashReport report) Reports a crash / exception across all providers.static voidReports a crash / exception across all providers.static voidevent(AnalyticsEvent event) Records a named event across all providers.static voidflush()Flushes any buffered events in every provider.static AnalyticsConsentThe currently recorded consent, loading it fromPreferenceson first access.static ConsentModeThe active consent mode.static List<AnalyticsProvider> The currently registered providers.static voidremoveProvider(AnalyticsProvider provider) Removes a previously registered provider.static StringGenerates a fresh pseudonymous client id, persists it and re-initialises every provider with the new identity.static voidRecords a screen / page view across all providers.static voidsetConsent(AnalyticsConsent newConsent) Records the user's consent choice, persists it and notifies every provider.static voidsetConsentMode(ConsentMode mode) Sets the consent mode that governs behaviour before an explicit consent choice is recorded.static voidAssociates subsequent activity with a user id.static voidsetUserProperty(String key, String value) Sets a user property / custom dimension.
-
Method Details
-
addProvider
Registers a provider and immediately supplies it with the current
AnalyticsContextand consent state.Parameters
provider: the provider to add, ignored if null
-
removeProvider
Removes a previously registered provider.
Parameters
provider: the provider to remove
-
clearProviders
public static void clearProviders()Removes every registered provider. -
getProviders
The currently registered providers.
Returns
an immutable copy of the provider list
-
setConsentMode
Sets the consent mode that governs behaviour before an explicit consent choice is recorded. Defaults to
ConsentMode.OPT_IN.Parameters
mode: the consent mode, ignored if null
-
getConsentMode
The active consent mode.
Returns
the consent mode
-
setConsent
Records the user's consent choice, persists it and notifies every provider. Passing null clears the stored choice (reverting to the implicit behaviour of the current
ConsentMode).Parameters
newConsent: the consent state, or null to clear
-
getConsent
The currently recorded consent, loading it from
Preferenceson first access. Returns null if the user has not made an explicit choice.Returns
the consent state or null
-
screen
-
event
Records a named event across all providers. No-op unless the analytics consent category is satisfied.
Parameters
event: the event
-
setUserId
Associates subsequent activity with a user id. No-op unless the personalization consent category is satisfied.
Parameters
id: the user id, or null to clear
-
setUserProperty
-
crash
Reports a crash / exception across all providers. No-op unless the crash reporting consent category is satisfied.
Parameters
-
throwable: the captured exception, may be null -
message: a human readable description, may be null -
fatal: whether the exception terminated the application
-
-
crash
Reports a crash / exception across all providers. No-op unless the crash reporting consent category is satisfied.
Parameters
report: the crash report
-
flush
public static void flush()Flushes any buffered events in every provider. -
clientId
The pseudonymous client id, generating and persisting one on first use. Not derived from any hardware identifier.
Returns
the client id
-
resetClientId
Generates a fresh pseudonymous client id, persists it and re-initialises every provider with the new identity. Use this to honour a "right to be forgotten" / erasure request from the user.
Returns
the new client id
-