{"id":"firebase-analytics","name":"firebase-analytics","summary":"分析イベントのログ、ユーザープロパティの設定、デフォルトイベントパラメータの設定、ファネル構築、画面表示トラッキングの追加などに活用できます。","body":"# Firebase Analytics Skill\n\nThis skill defines how to correctly implement Firebase Analytics in Flutter applications, covering setup, event logging, user properties, and data collection best practices.\n\n## When to Use\n\nUse this skill when:\n\n* Setting up and configuring Firebase Analytics in a Flutter project.\n* Logging predefined or custom analytics events.\n* Setting user properties or default event parameters.\n* Implementing screen view tracking with GoRouter or Navigator observers.\n* Building conversion funnels or tracking user flows.\n\n---\n\n## 1. Setup and Configuration\n\n```\nflutter pub add firebase_analytics\nflutter run\n```\n\n```dart\nimport 'package:firebase_analytics/firebase_analytics.dart';\n\n// After Firebase.initializeApp():\nFirebaseAnalytics analytics = FirebaseAnalytics.instance;\n```\n\n- Initialize Firebase before using any Firebase Analytics features.\n- Analytics **automatically logs** some events and user properties — no additional code needed for those.\n- On iOS, if your app does not use the IDFA (Advertising Identifier), use the IDFA-free Analytics dependency (`FirebaseAnalyticsCore` under Swift Package Manager, or `FirebaseAnalytics/Core` under CocoaPods) instead of the default `FirebaseAnalytics` dependency to avoid App Store review questions about advertising identifiers:\n  - **Swift Package Manager:** set `FIREBASE_ANALYTICS_WITHOUT_ADID=true` when building (`FIREBASE_ANALYTICS_WITHOUT_ADID=true flutter build ios`).\n\n### Add Navigator Observer for Automatic Screen Tracking\n\n```dart\nMaterialApp(\n  navigatorObservers: [\n    FirebaseAnalyticsObserver(analytics: FirebaseAnalytics.instance),\n  ],\n);\n```\n\nFor GoRouter, log screen views manually on route changes:\n\n```dart\nGoRouter(\n  observers: [FirebaseAnalyticsObserver(analytics: FirebaseAnalytics.instance)],\n);\n```\n\n### Verification Checklist\n\n1. Confirm `Firebase.initializeApp()` completes before accessing `FirebaseAnalytics.instance`.\n2. Run the app and check the Firebase DebugView console for incoming events.\n3. Confirm automatic events (`first_open`, `session_start`) appear without extra code.\n\n---\n\n## 2. Event Logging\n\nUse **predefined event methods** when possible for maximum detail in reports and access to future Google Analytics features:\n\n```dart\nawait FirebaseAnalytics.instance.logSelectContent(\n  contentType: \"image\",\n  itemId: itemId,\n);\n```\n\nUse the general `logEvent()` method for both predefined and custom events:\n\n```dart\nawait FirebaseAnalytics.instance.logEvent(\n  name: \"select_content\",\n  parameters: {\n    \"content_type\": \"image\",\n    \"item_id\": itemId,\n  },\n);\n```\n\n### Custom Event Example — E-commerce Add-to-Cart\n\n```dart\nFuture<void> logAddToCart(String productId, String productName, double price) async {\n  await FirebaseAnalytics.instance.logEvent(\n    name: 'add_to_cart',\n    parameters: {\n      'product_id': productId,\n      'product_name': productName,\n      'price': price,\n      'currency': 'USD',\n    },\n  );\n}\n```\n\n- Event names are **case-sensitive** — names differing only in case create two distinct events.\n- Up to **500 different event types** with no limit on total event volume.\n- Event names must start with an alphabetic character, contain only alphanumeric characters and underscores, and be no longer than **40 characters**.\n\n---\n\n## 3. Parameters and Properties\n\n- Parameter names: up to **40 characters**, must start with an alphabetic character, contain only alphanumeric characters and underscores.\n- String parameter values: up to **100 characters**.\n- The prefixes `firebase_`, `google_`, and `ga_` are **reserved** — do not use them for parameter names.\n- Up to **25 custom parameters** per event.\n- Register custom parameters in the Analytics console to use them as dimensions or metrics in reports.\n\nSet default parameters for all future events (not supported on web):\n\n```dart\nawait FirebaseAnalytics.instance.setDefaultEventParameters({\n  'app_version': '1.2.3',\n  'environment': 'production',\n});\n```\n\nClear a default parameter by setting it to `null`.\n\n---\n\n## 4. User Properties\n\n```dart\nawait FirebaseAnalytics.instance.setUserProperty(\n  name: 'favorite_food',\n  value: favoriteFood,\n);\n```\n\nSet the user ID to correlate events across devices:\n\n```dart\nawait FirebaseAnalytics.instance.setUserId(id: 'user_12345');\n```\n\n- Create custom definitions for user properties in the Analytics console before using them.\n- Up to **25 custom user properties** per project.\n- Use user properties for audience segmentation, report filtering, or A/B test targeting.\n\n---\n\n## 5. Best Practices\n\n- **Request necessary permissions** before collecting user data, especially on platforms with strict privacy controls.\n- **Never log** sensitive or personally identifiable information in events or user properties.\n- Use **consistent naming conventions** (snake_case) for custom events and parameters.\n- Group related events to track user flows and conversion funnels.\n- Use **DebugView** in the Firebase console during development — enable it on a physical device with:\n  - **Android:** `adb shell setprop debug.firebase.analytics.app <package_name>`\n  - **iOS:** Add `-FIRDebugEnabled` to scheme arguments in Xcode.\n- **Test** analytics implementation before deploying to production by confirming events appear in DebugView.\n\n---\n\n## References\n\n- [FlutterFire GitHub Repository](https://github.com/firebase/flutterfire)","author":"@evanca","ownerProfile":null,"authorContacts":null,"sourceUrl":"https://github.com/evanca/flutter-ai-rules/tree/main/skills/firebase-analytics","license":"MIT","category":"review","lang":"en","tokens":1120,"stars":0,"calls30d":2,"claimed":false,"visibility":"public","origin":"crawler","version":"0.1.0","createdAt":"2026-08-22","updatedAt":"2026-08-22","files":[],"requires":{"mcp":[],"tools":[]},"safety":{"flags":[],"scannedAt":"2026-08-22","hasScripts":false,"networkEndpoints":[]}}