How to fix Analytics: Code 2 - Event name invalid in Firebase

FirebaseBEGINNERMEDIUMUpdated Dec 29, 2025ยท 93 views

This error occurs when you try to log a Firebase Analytics event with an invalid event name. Firebase enforces strict naming rules to maintain data quality and consistency across your analytics.

Quick fix

Fixes 2

user_signup
button_click
purchase_complete
form_submission
page_view
  1. Verify event name format
  2. Update your event logging code
  3. Test the corrected event
  4. Check parameter names too
firebase โ€” error
user_signup
button_click
purchase_complete
form_submission
page_view
Analytics: Code 2 - Event name invalid

What this error means

Firebase Analytics validates event names when they are logged to your analytics collection. Code 2 is a validation error that means the event name you provided doesn't meet Firebase's naming requirements. This validation happens at the client SDK level before the event is sent to Firebase servers. The error prevents malformed events from entering your analytics data, which protects the integrity of your analytics reports and dashboards. Firebase reserves certain event names for system events and enforces character restrictions to avoid conflicts with other analytics platforms and data processing pipelines.

Typical symptoms

  • Events fail to log in Firebase Analytics with Code 2 error in the console
  • Analytics dashboard shows fewer events than expected
  • Custom events never appear in your Firebase Analytics reports
  • Mobile app or web app analytics logging works for some events but not others

Common causes

  1. Event name starts with a number or underscore instead of a letter
  2. Event name contains special characters other than underscores (spaces, hyphens, periods, etc.)
  3. Event name exceeds 40 characters in length
  4. Event name uses a reserved Firebase prefix (firebase_, google_, ga_)
  5. Event name contains uppercase letters or mixed case (though case-sensitive, some implementations expect lowercase)

How to fix "Analytics: Code 2 - Event name invalid"

1Verify event name format

Check that your event name follows Firebase's naming requirements:
- Must start with an alphabetic character (a-z or A-Z)
- Can only contain alphanumeric characters and underscores
- Must be 1-40 characters long
- Cannot start with firebase_, google_, or ga_ prefixes

Example of valid event names:

bash
user_signup
button_click
purchase_complete
form_submission
page_view

Example of INVALID event names:

bash
1_user_signup          // Starts with number
_user_signup           // Starts with underscore
user-signup            // Contains hyphen
user signup            // Contains space
user.signup            // Contains period
firebase_custom_event  // Starts with reserved prefix
very_long_event_name_that_exceeds_the_character_limit_of_forty  // Too long
2Update your event logging code

Once you've identified the invalid event name, update your code to use a compliant name. Replace the problematic event name with a valid one that meets Firebase's requirements.

For web (JavaScript):

javascript
// WRONG
firebase.analytics().logEvent("1st_purchase");

// CORRECT
firebase.analytics().logEvent("first_purchase");

For mobile (Swift):

swift
// WRONG
Analytics.logEvent("user-login", parameters: [:])

// CORRECT
Analytics.logEvent("user_login", parameters: [:])

For mobile (Kotlin):

kotlin
// WRONG
FirebaseAnalytics.getInstance(context).logEvent("purchase@checkout", null)

// CORRECT
FirebaseAnalytics.getInstance(context).logEvent("purchase_checkout", null)
3Test the corrected event

After updating your code, test that the event logs successfully. Enable Firebase Analytics DebugView to monitor events in real-time.

For Android:

bash
adb shell setprop debug.firebase.analytics.app com.example.myapp

For iOS, run your app in Xcode with -FIRAnalyticsDebugEnabled flag.

Once DebugView is enabled, trigger the event in your app and verify it appears in the Firebase Console under Analytics > DebugView. The event should appear with no error codes.

4Check parameter names too

If your event name is valid but you're still seeing Code 2 errors, the problem might be in your custom parameters. Parameter names follow the same naming rules as event names:
- Must start with a letter
- Can contain alphanumeric characters and underscores only
- Must be 1-40 characters long
- Cannot use reserved prefixes

javascript
// Event name is valid, but parameter name has a hyphen
firebase.analytics().logEvent("user_login", {
  "login-method": "google"  // WRONG - hyphen not allowed
});

// Correct version
firebase.analytics().logEvent("user_login", {
  "login_method": "google"  // CORRECT
});

Advanced notes

Event name validation is case-sensitive, so "user_login" and "User_Login" are treated as different events. Firebase recommends using consistent lowercase snake_case naming across your entire application to avoid confusion.

If you're using Firebase Analytics on both web and mobile platforms, ensure event names are identical across platforms for consistent reporting. Cross-platform event naming makes it easier to analyze user behavior across different app versions.

Reserved event names like app_open, app_remove, app_clear_data, user_engagement, session_start, and session_end are automatically logged by Firebase SDKs and cannot be manually logged. Attempting to log these will result in validation errors.

For very large applications with many custom events, consider maintaining an event taxonomy document that lists all approved event and parameter names to prevent naming inconsistencies and validation errors.

Frequently asked questions

What does "2" mean?
Firebase Analytics validates event names when they are logged to your analytics collection. Code 2 is a validation error that means the event name you provided doesn't meet Firebase's naming requirements. This validation happens at the client SDK level before the event is sent to Firebase servers.
How do I fix "2"?
This error occurs when you try to log a Firebase Analytics event with an invalid event name. Firebase enforces strict naming rules to maintain data quality and consistency across your analytics.
What causes "2"?
Event name starts with a number or underscore instead of a letter Event name contains special characters other than underscores (spaces, hyphens, periods, etc.) Event name exceeds 40 characters in length
Is the "2" error serious?
It is a medium-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading