FirebaseAnalytics

@DataCollectionPurpose(dataTypes = [SemanticType.ST_ANALYTICS_ID, SemanticType.ST_PAYMENTS_TRANSACTION_INFO, SemanticType.ST_HARDWARE_ID, SemanticType.ST_IDENTIFYING_ID, SemanticType.ST_COARSE_LOCATION], collectionPurposes = [CollectionPurpose.CP_ANALYTICS])
public final classFirebaseAnalytics


The top level Firebase Analytics singleton that provides methods for logging events and setting user properties. Seethe developer guides for general information on using Firebase Analytics in your apps.

Applications can get an instance of this class by callinggetInstance.getInstance is thread safe and can be called from any thread.

Summary

Nested types

The status value of the consent type.

The type of consent to set.

An Event is an important occurrence in your app that you want to measure.

Params supply information that contextualize Events.

A UserProperty is an attribute that describes the app-user.

Public methods

@NonNullTask<@NullableString>
@SuppressViolation(value = "catch_specific_exceptions")
getAppInstanceId()

Retrieves the app instance id from the service, ornull ifANALYTICS_STORAGE has been set toDENIED.

static @NonNullFirebaseAnalytics
@RequiresPermission(allOf = [Manifest.permission.INTERNET, Manifest.permission.ACCESS_NETWORK_STATE, Manifest.permission.WAKE_LOCK])
@Keep
getInstance(@NonNullContext context)

Returns the singleton FirebaseAnalytics interface.

@NonNullTask<@NullableLong>
@SuppressViolation(value = "catch_specific_exceptions")
getSessionId()

Retrieves the session id from the client.

void
logEvent(@NonNull @Size(min = 1, max = 40)String name, @NullableBundle params)

Logs an app event.

void

Clears all analytics data for this app from the device and resets the app instance id.

void

Sets whether analytics collection is enabled for this app on this device.

void

Sets the applicable end user consent state (e.g., for device identifiers) for this app on this device.

void
@MainThread
@Keep
setCurrentScreen(
    @NonNullActivity activity,
    @Nullable @Size(min = 1, max = 36)String screenName,
    @Nullable @Size(min = 1, max = 36)String screenClassOverride
)

This method is deprecated.

To log screen view events, call mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.SCREEN_VIEW, params) instead.

void

Adds parameters that will be set on every event logged from the SDK, including automatic ones.

void
setSessionTimeoutDuration(long milliseconds)

Sets the duration of inactivity that terminates the current session.

void

Sets the user ID property.

void
setUserProperty(
    @NonNull @Size(min = 1, max = 24)String name,
    @Nullable @Size(max = 36)String value
)

Sets a user property to a given value.

Extension functions

final void

Fluent version ofFirebaseAnalytics.logEvent.

final void

Fluent version ofFirebaseAnalytics.setConsent.

Public methods

getAppInstanceId

@SuppressViolation(value = "catch_specific_exceptions")
public @NonNullTask<@NullableStringgetAppInstanceId()

Retrieves the app instance id from the service, ornull ifANALYTICS_STORAGE has been set toDENIED.

Returns
@NonNullTask<@NullableString>

Task with the result of the retrieval

See also
setConsent

getInstance

@RequiresPermission(allOf = [Manifest.permission.INTERNET, Manifest.permission.ACCESS_NETWORK_STATE, Manifest.permission.WAKE_LOCK])
@Keep
public static @NonNullFirebaseAnalytics getInstance(@NonNullContext context)

Returns the singleton FirebaseAnalytics interface.

Parameters
@NonNullContext context

the context used to initialize Firebase Analytics. Must not benull.

getSessionId

@SuppressViolation(value = "catch_specific_exceptions")
public @NonNullTask<@NullableLonggetSessionId()

Retrieves the session id from the client. Returnsnull ifANALYTICS_STORAGE has been set toDENIED or session is expired.

Returns
@NonNullTask<@NullableLong>

Task with the result of the retrieval

See also
setConsent

logEvent

public void logEvent(@NonNull @Size(min = 1, max = 40)String name, @NullableBundle params)

Logs an app event. The event can have up to 25 parameters. Events with the same name must have the same parameters. Up to 500 event names are supported. Using predefinedEvent and/orParam is recommended for optimal reporting.

Parameters
@NonNull @Size(min = 1, max = 40)String name

The name of the event. Should contain 1 to 40alphanumericcharacters or underscores. The name must start with analphabeticcharacter. Some event names are reserved. SeeEvent for the list of reserved event names. The "firebase_", "google_" and "ga_" prefixes are reserved and should not be used. Note that event names are case-sensitive and that logging two events whose names differ only in case will result in two distinct events.

@NullableBundle params

The map of event parameters. Passing null indicates that the event has no parameters. Parameter names can be up to 40 characters long and must start with analphabeticcharacter and contain onlyalphanumericcharacters and underscores. String, long and double param types are supported. String parameter values can be up to 100 characters long. The "firebase_", "google_" and "ga_" prefixes are reserved and should not be used for parameter names.

resetAnalyticsData

public void resetAnalyticsData()

Clears all analytics data for this app from the device and resets the app instance id.

setAnalyticsCollectionEnabled

public void setAnalyticsCollectionEnabled(boolean enabled)

Sets whether analytics collection is enabled for this app on this device. This setting is persisted across app sessions. By default it is enabled.

Parameters
boolean enabled

Whether analytics collection is enabled for this app on this device.

setConsent

public void setConsent(
    @NonNullMap<FirebaseAnalytics.ConsentTypeFirebaseAnalytics.ConsentStatus> consentSettings
)

Sets the applicable end user consent state (e.g., for device identifiers) for this app on this device. Use the consent map to specify individual consent type values. Settings are persisted across app sessions.

Parameters
@NonNullMap<FirebaseAnalytics.ConsentTypeFirebaseAnalytics.ConsentStatus> consentSettings

The map of consent types. Supported consent type keys areAD_STORAGE,ANALYTICS_STORAGE,AD_USER_DATA andAD_PERSONALIZATION. Valid values areGRANTED andDENIED.

setCurrentScreen

@MainThread
@Keep
public void setCurrentScreen(
    @NonNullActivity activity,
    @Nullable @Size(min = 1, max = 36)String screenName,
    @Nullable @Size(min = 1, max = 36)String screenClassOverride
)
This method is deprecated.

To log screen view events, call mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.SCREEN_VIEW, params) instead.

Sets the current screen name, which specifies the current visual context in your app. This helps identify the areas in your app where users spend their time and how they interact with your app.

Note that screen reporting is enabled automatically and records the class name of the current Activity for you without requiring you to call this function. The class name can optionally be overridden by calling this function in the onResume callback of your Activity and specifying the screenClassOverride parameter.

If your app does not use a distinct Activity for each screen, you should call this function and specify a distinct screenName each time a new screen is presented to the user.

The name and classOverride remain in effect until the current Activity changes or a new call to setCurrentScreen is made.

This method must be called from the app's main thread.

Parameters
@NonNullActivity activity

The activity to which the screen name and class name apply.

@Nullable @Size(min = 1, max = 36)String screenName

The name of the current screen. Set to null to clear the current screen name.

@Nullable @Size(min = 1, max = 36)String screenClassOverride

The name of the screen class. By default this is the class name of the current Activity. Set to null to revert to the default class name.

setDefaultEventParameters

public void setDefaultEventParameters(@NullableBundle parameters)

Adds parameters that will be set on every event logged from the SDK, including automatic ones. The values passed in the parameters bundle will be added to the map of default event parameters. These parameters persist across app runs. They are of lower precedence than event parameters, so if an event parameter and a parameter set using this API have the same name, the value of the event parameter will be used. The same limitations on event parameters apply to default event parameters.

Parameters
@NullableBundle parameters

Parameters to be added to the map of parameters added to every event. They will be added to the map of default event parameters, replacing any existing parameter with the same name. Valid parameter values areString,long, anddouble. Setting a key's value tonull will clear that parameter. Passing in anull bundle will clear all parameters.

setSessionTimeoutDuration

public void setSessionTimeoutDuration(long milliseconds)

Sets the duration of inactivity that terminates the current session. The default value is 1800000 (30 minutes).

Parameters
long milliseconds

Session timeout duration in milliseconds

setUserId

public void setUserId(@NullableString id)

Sets the user ID property. This feature must be used in accordance withGoogle's Privacy Policy.

Parameters
@NullableString id

The user ID to ascribe to the user of this app on this device, which must be non-empty and no more than 256 characters long. Setting the ID to null removes the user ID.

setUserProperty

public void setUserProperty(
    @NonNull @Size(min = 1, max = 24)String name,
    @Nullable @Size(max = 36)String value
)

Sets a user property to a given value. Up to 25 user property names are supported. Once set, user property values persist throughout the app lifecycle and across sessions.

Parameters
@NonNull @Size(min = 1, max = 24)String name

The name of the user property to set. Should contain 1 to 24alphanumericcharacters or underscores and must start with analphabeticcharacter. The "firebase_", "google_" and "ga_" prefixes are reserved and should not be used for user property names.

@Nullable @Size(max = 36)String value

The value of the user property. Values can be up to 36 characters long. Setting the value to null removes the user property.

Extension functions

AnalyticsKt.logEvent

public final void AnalyticsKt.logEvent(
    @NonNullFirebaseAnalytics receiver,
    @NonNullString name,
    @ExtensionFunctionType @NonNull Function1<@NonNullParametersBuilderUnit> block
)

Fluent version ofFirebaseAnalytics.logEvent.

Example use:

Firebase.analytics.logEvent("myEvent") {
param(Params.VALUE, 3.99)
param(Params.CURRENCY, "USD")
}

AnalyticsKt.setConsent

public final void AnalyticsKt.setConsent(
    @NonNullFirebaseAnalytics receiver,
    @ExtensionFunctionType @NonNull Function1<@NonNullConsentBuilderUnit> block
)

Fluent version ofFirebaseAnalytics.setConsent.

Example use:

Firebase.analytics.setConsent {
adStorage = ConsentStatus.GRANTED
analyticsStorage = ConsentStatus.GRANTED
adUserData = ConsentStatus.GRANTED
adPersonalization = ConsentStatus.GRANTED
}

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-07-21 UTC.