public final class Analytics
- Object
- 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("tutorial_complete").param("seconds", 42).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.
Methods
Inherited methods
Method details
addProvider
public static void addProvider(AnalyticsProvider provider)AnalyticsContext and consent state.Parameters
providerAnalyticsProvider- the provider to add, ignored if null
removeProvider
public static void removeProvider(AnalyticsProvider provider)Parameters
providerAnalyticsProvider- the provider to remove
clearProviders
public static void clearProviders()getProviders
public static List<AnalyticsProvider> getProviders()Returns
setConsentMode
public static void setConsentMode(ConsentMode mode)ConsentMode.OPT_IN.Parameters
modeConsentMode- the consent mode, ignored if null
getConsentMode
public static ConsentMode getConsentMode()Returns
setConsent
public static void setConsent(AnalyticsConsent newConsent)ConsentMode).Parameters
newConsentAnalyticsConsent- the consent state, or null to clear
getConsent
public static AnalyticsConsent getConsent()Preferences on
first access. Returns null if the user has not made an explicit choice.Returns
screen
public static void screen(String name, String referrer)Parameters
nameString- the screen name
referrerString- the previous screen, may be null
event
public static void event(AnalyticsEvent event)Parameters
eventAnalyticsEvent- the event
autoEvent
public static void autoEvent(String name, String category, Map<String, Object> params)AnalyticsEvent and routes
it through the normal consent-gated event(AnalyticsEvent) path,
so it is automatically a no-op when consent has not been granted. As a
fast path it does nothing at all when no provider is registered, which
keeps the cost of the framework call sites negligible when analytics is
not in use. Never throws into the caller.Parameters
nameString- the event name
categoryString- the event category, may be null
paramsMap<String, Object>- optional event parameters, may be null
setUserId
public static void setUserId(String id)Parameters
idString- the user id, or null to clear
setUserProperty
public static void setUserProperty(String key, String value)Parameters
keyString- the property name
valueString- the property value
setDimension
public static void setDimension(String key, String value)"plan" or "role"). Dimensions are
persisted in Preferences so they survive restarts and are sent
with every first-party batch. Passing a null value removes the key.
Null or empty keys are ignored.Parameters
keyString- the dimension key
valueString- the dimension value, or null to remove the key
clearDimension
public static void clearDimension(String key)Parameters
keyString- the dimension key
clearDimensions
public static void clearDimensions()getDimensions
public static Map<String, String> getDimensions()Returns
crash
public static void crash(Throwable throwable, String message, boolean fatal)Reports a crash / exception to the registered analytics providers as an
exception signal – for example GA4’s app_exception or the
Firebase equivalent – so exception counts surface alongside your usage
metrics. No-op unless the crash reporting consent category is satisfied.
This is deliberately separate from Codename One Crash Protection
(CrashProtection). Crash Protection is a
dedicated crash-capture pipeline that records full, symbolicated stack
traces to the build cloud crash console for debugging. This method only
emits a lightweight exception event to whichever analytics backends you
have registered. The two subsystems are independent: use Crash
Protection to diagnose crashes, use Analytics.crash when you also
want exception events in your analytics; you can enable either or both.
Parameters
throwableThrowable- the captured exception, may be null
messageString- a human readable description, may be null
fatalboolean- whether the exception terminated the application
crash
public static void crash(AnalyticsCrashReport report)Parameters
reportAnalyticsCrashReport- the crash report
flush
public static void flush()clientId
public static String clientId()Returns
resetClientId
public static String resetClientId()