קל לארגן דפים בעזרת אוספים
אפשר לשמור ולסווג תוכן על סמך ההעדפות שלך.
במדריך למתחילים הזה מוסבר איך להוסיף את Google Analytics לאפליקציה ולהתחיל לתעד אירועים.
Google Analytics אוסף נתוני שימוש והתנהגות באפליקציה שלכם. ה-SDK מתעד שני סוגים עיקריים של מידע:
אירועים: מה קורה באפליקציה, כמו פעולות משתמשים, אירועי מערכת או שגיאות.
מאפייני משתמשים: מאפיינים שאתם מגדירים כדי לתאר פלחים של בסיס המשתמשים, כמו העדפות שפה או מיקום גיאוגרפי.
Analytics מתעדת באופן אוטומטי חלק מהאירועים ומאפייני המשתמשים. לא צריך להוסיף קוד כדי להפעיל אותם.
לפני שמתחילים
אם עדיין לא הוספתם את Firebase לפרויקט JavaScript, אתם צריכים להוסיף אותו ולוודא ש-Google Analytics מופעל בפרויקט Firebase:
אם אתם יוצרים פרויקט חדש ב-Firebase, זכרו להפעיל את Google Analytics
לפני שתסיימו ליצור את הפרויקט.
אם אתם משתמשים בפרויקט קיים ב-Firebase שלא הפעלתם בו את Google Analytics, אתם יכולים לעשות זאת דרך הכרטיסייה Integrations (שילובים) בקטע settings > Project settings (הגדרות הפרויקט).
כשתפעילו את Google Analytics בפרויקט, אפליקציות האינטרנט ב-Firebase יקושרו למקורות נתונים של Google Analytics שמשויכים לנכס אפליקציה + אתר.
הוספת Analytics SDK לאפליקציה
בהתאם לאופן האירוח של אפליקציית האינטרנט, יכול להיות שההגדרה תטופל באופן אוטומטי או שתצטרכו לעדכן את אובייקט ההגדרה של Firebase.
אם אפליקציית האינטרנט שלכם כבר משתמשת ב-Google Analytics, יכול להיות שתצטרכו לבצע הגדרה נוספת כמו שמתואר במאמר שימוש ב-Firebase עם תיוג gtag.js קיים.
בודקים שאובייקט ההגדרה של Firebase בקוד מכיל את הערך
measurementId. המזהה הזה נוצר באופן אוטומטי כשמפעילים את Analytics בפרויקט Firebase ורושמים אפליקציית אינטרנט, והוא נדרש כדי להשתמש ב-Analytics.
אם האפליקציה שלכם משתמשת ב-Firebase Hostingוגם משתמשת בכתובות URL שמורות עבור ערכות ה-SDK של Firebase:
המערכת של Firebase מטפלת באופן אוטומטי בהגדרת האפליקציה. כדי להשלים את ההגדרה, מוסיפים את הסקריפטים מהכרטיס האפליקציות שלך בהגדרות הפרויקט לתג <body> של האפליקציה, אם עדיין לא עשיתם זאת.
אם האפליקציה לא משתמשת בכתובות URL שמורות:
אם אתם עובדים עם אפליקציית אינטרנט קיימת, צריך לעדכן את אובייקט ההגדרה של Firebase בקוד כדי לוודא שהשדה measurementId קיים. אובייקט ההגדרה אמור להיראות כמו בדוגמה הבאה:
// For FirebaseJavaScript SDK v7.20.0 and later, `measurementId` is an optional fieldconstfirebaseConfig={apiKey:"API_KEY",authDomain:"PROJECT_ID.firebaseapp.com",databaseURL:"https://PROJECT_ID.firebaseio.com",projectId:"PROJECT_ID",storageBucket:"PROJECT_ID.firebasestorage.app",messagingSenderId:"SENDER_ID",appId:"APP_ID",measurementId:"G-GA_MEASUREMENT_ID"};
אם הפעלתם בעבר את Google Analytics באפליקציה באמצעות קטע הקוד של gtag.js, יכול להיות שתצטרכו לבצע הגדרה נוספת באפליקציה אם אתם מתכננים לבצע אחת מהפעולות הבאות:
להוסיף קריאות ל-Google Analytics מ-Firebase לדף, אבל גם מתכננים להמשיך להשתמש בקריאות ל-gtag() ישירות באותו דף.
רוצים להשתמש באותו מזהה מדידה גם בקריאות ישירות ל-gtag() וגם בנתוני Google Analytics שנשלחים ל-Firebase.
כדי לוודא שהאירועים שלכם זמינים לשימוש בכל שירותי Firebase, צריך לבצע את שלבי ההגדרה הנוספים הבאים:
מסירים את השורה gtag('config', 'GA_MEASUREMENT_ID');
כאשר GA_MEASUREMENT_ID הוא measurementId של
אפליקציית האינטרנט שלכם ב-Firebase. אם יש לכם מזהים אחרים של נכסי Analytics אחרים בדף, אתם לא צריכים להסיר את שורת ההגדרה שלהם.
חשוב להקפיד לקרוא לפונקציה firebase.analytics() לפני ששולחים אירועים באמצעות gtag().
אחרת, אירועים שנשלחים למזהה הזה באמצעות קריאות ל-gtag() לא ישויכו ל-Firebase ולא יהיו זמינים לטירגוט בשירותים אחרים של Firebase.
יש אירועים שמומלצים לכל האפליקציות, ויש אירועים שמומלצים לסוגים או לקטגוריות ספציפיים של עסקים. כדי שבדוחות יוצגו כמה שיותר פרטים וכדי להפיק תועלת מהתכונות ומהשילובים שיהיו זמינים בעתיד, כדאי לשלוח את האירועים המומלצים עם הפרמטרים שלהם שהוגדרו מראש. בקטע הזה מוסבר איך לרשום ביומן אירוע מוגדר מראש. מידע נוסף על רישום אירועים ביומן זמין במאמר בנושא רישום אירועים ביומן.
בדוגמה הבאה מוצג איך לרשום ביומן אירוע מומלץ כדי לציין שמשתמש קיבל התראה באפליקציה:
[[["התוכן קל להבנה","easyToUnderstand","thumb-up"],["התוכן עזר לי לפתור בעיה","solvedMyProblem","thumb-up"],["סיבה אחרת","otherUp","thumb-up"]],[["חסרים לי מידע או פרטים","missingTheInformationINeed","thumb-down"],["התוכן מורכב מדי או עם יותר מדי שלבים","tooComplicatedTooManySteps","thumb-down"],["התוכן לא עדכני","outOfDate","thumb-down"],["בעיה בתרגום","translationIssue","thumb-down"],["בעיה בדוגמאות/בקוד","samplesCodeIssue","thumb-down"],["סיבה אחרת","otherDown","thumb-down"]],["עדכון אחרון: 2025-08-31 (שעון UTC)."],[],[],null,["iOS+ Android Web Flutter \n\n\u003cbr /\u003e\n\nThis quickstart shows you how to add Google Analytics to your app and\nbegin logging events.\n\nGoogle Analytics collects usage and behavior data for your app. The SDK\nlogs two primary types of information:\n\n- **Events:** What is happening in your app, such as user actions, system events, or errors.\n- **User properties:** Attributes you define to describe segments of your user base, such as language preference or geographic location.\n\nAnalytics automatically logs some\n[events](https://support.google.com/firebase/answer/6317485) and\n[user properties](https://support.google.com/firebase/answer/6317486);\nyou don't need to add any code to enable them.\n\nBefore you begin\n\nIf you haven't already, [add Firebase to your JavaScript\nproject](/docs/web/setup) and make sure that Google Analytics is enabled\nin your Firebase project:\n\n- If you're creating a new Firebase project, enable Google Analytics\n during the project creation workflow.\n\n- If you're using an existing Firebase project that doesn't have\n Google Analytics enabled, go to the\n [*Integrations*](//console.firebase.google.com/project/_/settings/integrations)\n tab of your settings \\\u003e *Project settings* to enable it.\n\n| **Warning:** Any Firebase project created before July 31, 2019 must be upgraded to the full [Google Analytics 4 experience](https://support.google.com/analytics/answer/10089681) if it hasn't already. (Banners display in the Analytics dashboard if an upgrade is required.) The associated Terms of Service must be [accepted by February 15, 2022](https://support.google.com/analytics/answer/10960488) to ensure data collection continues. See the Support FAQ on the upgrade for help [finding project Owners](/support/faq#analytics-upgrade-tos).\n\nWhen you enable Google Analytics in your project, your Firebase web apps\nare linked to Google Analytics data streams associated with an\n[App + Web property](//developers.google.com/analytics/devguides/collection/app-web/tag-guide).\n\nAdd the Analytics SDK to your app\n\nDepending on how your web application is hosted, your configuration may be\nhandled automatically or you may need to update your\n[Firebase configuration object](/docs/projects/learn-more#config-files-objects).\nIf your web app already uses Google Analytics, you may need to do additional\nsetup described in [Use Firebase with existing gtag.js tagging](#firebase-gtag).\n\n1. Check that your Firebase config object in your code contains\n `measurementId`. This ID is automatically created when you enable\n Analytics in your Firebase project and register a web app, and it's\n required to use Analytics.\n\n | **Note:** For apps using the **Firebase JavaScript SDK v7.20.0 and later** , Firebase dynamically fetches the `measurementId` when your app initializes Analytics. Having this ID in your config object is optional, but it does serve as a fallback in the rare case that the dynamic fetch fails.\n - **If your app uses Firebase Hosting *and* uses\n [reserved URLs](/docs/hosting/reserved-urls) for the Firebase SDKs**:\n\n Firebase automatically handles configuring your application. To complete\n setup, add the scripts from the ***Your apps*** card in your\n [Project settings](https://console.firebase.google.com/project/_/settings/general/)\n to the \\\u003cbody\\\u003e tag of your app, if you haven't already.\n - **If your app does not use reserved URLs** :\n **If you're working with an existing web app** , update the Firebase config\n object in your code to ensure the `measurementId` field is present. The\n config object should look similar to the following example:\n\n // For Firebase JavaScript SDK v7.20.0 and later, `measurementId` is an optional field\n const firebaseConfig = {\n apiKey: \"\u003cvar translate=\"no\"\u003eAPI_KEY\u003c/var\u003e\",\n authDomain: \"\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e.firebaseapp.com\",\n databaseURL: \"https://\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e.firebaseio.com\",\n projectId: \"\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e\",\n storageBucket: \"\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e`.firebasestorage.app`\",\n messagingSenderId: \"\u003cvar translate=\"no\"\u003eSENDER_ID\u003c/var\u003e\",\n appId: \"\u003cvar translate=\"no\"\u003eAPP_ID\u003c/var\u003e\",\n measurementId: \"G-\u003cvar translate=\"no\"\u003eGA_MEASUREMENT_ID\u003c/var\u003e\"\n };\n\n2. If you haven't already, [install the Firebase JS SDK and initialize Firebase](/docs/web/setup#add-sdk-and-initialize).\n\n3. Add the Analytics JS SDK and initialize Analytics:\n\nWeb\n\n\n| [Learn more](/docs/web/learn-more#modular-version) about the tree-shakeable modular web API and [upgrade](/docs/web/modular-upgrade) from the namespaced API.\n\n\u003cbr /\u003e\n\n```python\nimport { initializeApp } from \"firebase/app\";\nimport { getAnalytics } from \"firebase/analytics\";\n\n// TODO: Replace the following with your app's Firebase project configuration\n// See: https://firebase.google.com/docs/web/learn-more#config-object\nconst firebaseConfig = {\n // ...\n};\n\n// Initialize Firebase\nconst app = initializeApp(firebaseConfig);\n\n\n// Initialize Analytics and get a reference to the service\nconst analytics = getAnalytics(app);\n```\n\nWeb\n\n\n| [Learn more](/docs/web/learn-more#modular-version) about the tree-shakeable modular web API and [upgrade](/docs/web/modular-upgrade) from the namespaced API.\n\n\u003cbr /\u003e\n\n```python\nimport firebase from \"firebase/compat/app\";\nimport \"firebase/compat/analytics\";\n\n// TODO: Replace the following with your app's Firebase project configuration\n// See: https://firebase.google.com/docs/web/learn-more#config-object\nconst firebaseConfig = {\n // ...\n};\n\n// Initialize Firebase\nfirebase.initializeApp(firebaseConfig);\n\n\n// Initialize Analytics and get a reference to the service\nconst analytics = firebase.analytics();\n```\n\nUse Firebase with existing gtag.js tagging\n\nIf you previously had Google Analytics running in your app using the\n[gtag.js snippet](//developers.google.com/analytics/devguides/collection/gtagjs/),\nyour app may require additional setup if you plan to do one of the following:\n\n- Add Google Analytics calls from Firebase to the page but also plan to continue using `gtag()` calls directly on the same page.\n- Want to use the same measurement ID between both direct `gtag()` calls and Google Analytics data sent to Firebase.\n\nTo ensure your events are available for use by all Firebase services, complete\nthe following additional setup steps:\n\n- Remove the line `gtag('config', '`\u003cvar translate=\"no\"\u003eGA_MEASUREMENT_ID\u003c/var\u003e`');` where the `GA_MEASUREMENT_ID` is the `measurementId` of your Firebase web app. If you have other IDs for other Analytics properties on the page, you do not need to remove their config line.\n- Make sure you call `firebase.analytics()` before you send any events with `gtag()`.\n\nOtherwise, events sent to that ID with `gtag()` calls will not be associated\nwith Firebase and will not be available for targeting in other\nFirebase services.\n\nStart logging events\n\nAfter you have initialized the\n[Analytics service](/docs/reference/js/analytics), you can\nbegin to log events with the\n[`logEvent()`](/docs/reference/js/analytics#logevent)\nmethod.\n\nCertain events are\n[recommended for all apps](https://support.google.com/firebase/answer/6317498);\nothers are recommended for specific business types or verticals. You should send\nsuggested events along with their prescribed parameters, to ensure maximum\navailable detail in your reports and to benefit from future features and\nintegrations as they become available. This section demonstrates logging a\npre-defined event, for more information on logging events, see\n[Log events](/docs/analytics/events?platform=web).\n\nThe following example demonstrates how to log a recommended event to indicate a\nuser has received a notification in your app: \n\nWeb \n\n```javascript\nimport { getAnalytics, logEvent } from \"firebase/analytics\";\n\nconst analytics = getAnalytics();\nlogEvent(analytics, 'notification_received');https://github.com/firebase/snippets-web/blob/467eaa165dcbd9b3ab15711e76fa52237ba37f8b/snippets/analytics-next/index/analytics_log_event.js#L8-L11\n```\n\nWeb \n\n```javascript\nfirebase.analytics().logEvent('notification_received');https://github.com/firebase/snippets-web/blob/467eaa165dcbd9b3ab15711e76fa52237ba37f8b/analytics/index.js#L12-L12\n```\n\nNext steps\n\n- Understand [each Analytics report](/docs/analytics/reports).\n- Use the [DebugView](/docs/analytics/debugview) to verify your events.\n- Explore your data in the [Firebase console](//console.firebase.google.com/project/_/analytics/).\n- Explore the guides on [events](/docs/analytics/events?platform=web) and [user properties.](/docs/analytics/user-properties?platform=web)\n- Learn how to export your data to [BigQuery.](https://support.google.com/firebase/answer/7030014)"]]