Firebase Analytics
@capacitor-community/firebase-analytics
Capacitor community plugin for native Firebase Analytics.
✏️✏️ This plugin now supports Capacitor 6 only. For Capacitor 5 and 4 please use version listed below ✏️✏️
Plugin versions
Capacitor version | Plugin version |
---|
6.x | 6.x |
5.x | 5.0.1 |
4.x | 1.0.1 |
Maintainers
Installation
Using npm:
npm install @capacitor-community/firebase-analytics@latest
Using yarn:
yarn add @capacitor-community/firebase-analytics@latest
Sync native files:
npx cap sync
Note: You may also need to run File > Sync Project with Gradle Files in order for Android Studio to recognize the import.
Configuration
No configuration is required for this plugin.
Examples
Click here for an example on how to implement this plugin.
You can also clone the repository:
git clone https://github.com/jcesarmobile/capacitor-firebase-analytics-example
Supported methods
Name | Android | iOS | Web |
---|
setUserId | ✅ | ✅ | ✅ |
setUserProperty | ✅ | ✅ | ✅ |
getAppInstanceId | ✅ | ✅ | ❌ |
setScreenName | ✅ | ✅ | ❌ |
reset | ✅ | ✅ | ❌ |
logEvent | ✅ | ✅ | ✅ |
setCollectionEnabled | ✅ | ✅ | ✅ |
setSessionTimeoutDuration | ✅ | ✅ | ✅ |
enable | ✅ | ✅ | ✅ |
disable | ✅ | ✅ | ✅ |
Usage
import { FirebaseAnalytics } from "@capacitor-community/firebase-analytics";
FirebaseAnalytics.initializeFirebase({
apiKey: "...",
authDomain: "...",
databaseURL: "...",
projectId: "...",
storageBucket: "...",
messagingSenderId: "...",
appId: "...",
measurementId: "...",
});
FirebaseAnalytics.setUserId({
userId: "john_doe_123",
});
FirebaseAnalytics.setUserProperty({
name: "favorite_food",
value: "pizza",
});
FirebaseAnalytics.getAppInstanceId();
FirebaseAnalytics.setScreenName({
screenName: "login",
nameOverride: "LoginScreen",
});
FirebaseAnalytics.reset();
FirebaseAnalytics.logEvent({
name: "select_content",
params: {
content_type: "image",
content_id: "P12453",
items: [{ name: "Kittens" }],
},
});
FirebaseAnalytics.setCollectionEnabled({
enabled: false,
});
FirebaseAnalytics.enable();
FirebaseAnalytics.disable();
FirebaseAnalytics.setSessionTimeoutDuration({
duration: 10000,
});
Setup
Navigate to the project settings page for your app on Firebase.
iOS
Download the GoogleService-Info.plist
file. In Xcode right-click on the yellow folder named "App" and select the Add files to "App"
.
Tip: if you drag and drop your file to this location, Xcode may not be able to find it.
Android
Download the google-services.json
file and copy it to android/app/
directory of your capacitor project.
Variables
This plugin will use the following project variables (defined in your app’s variables.gradle
file):
$firebaseAnalyticsVersion
version of com.google.firebase:firebase-analytics
(default: 21.2.2
)
iOS setup
ionic start my-cap-app --capacitor
cd my-cap-app
npm install --save @capacitor-community/firebase-analytics
mkdir www && touch www/index.html
sudo gem install cocoapods
(only once)npx cap add ios
npx cap sync ios
(every time you run npm install
)npx cap open ios
- sign your app at xcode (general tab)
- add
GoogleService-Info.plist
to the app folder in xcode
Enable debug view
- In Xcode, select Product > Scheme > Edit scheme
- Select Run from the left menu
- Select the Arguments tab
- In the Arguments Passed On Launch section, add
-FIRAnalyticsDebugEnabled
Tip: every time you change a native code you may need to clean up the cache (Product > Clean build folder) and then run the app again.
Android setup
ionic start my-cap-app --capacitor
cd my-cap-app
npm install --save @capacitor-community/firebase-analytics
mkdir www && touch www/index.html
npx cap add android
npx cap sync android
(every time you run npm install
)npx cap open android
- add
google-services.json
to your android/app
folder
Now you should be set to go. Try to run your client using ionic cap run android --livereload --address=0.0.0.0
.
Tip: every time you change a native code you may need to clean up the cache (Build > Clean Project | Build > Rebuild Project) and then run the app again.
Updating
For existing projects you can upgrade all capacitor related packages (including this plugin) with this single command
npx npm-upgrade '*capacitor*' && npm install
Migration
If you were previously using the capacitor-analytics
package from npm
-
Update NPM package:
npm uninstall --save capacitor-analytics
npm install --save-prod @capacitor-community/firebase-analytics@latest
-
Update the plugin initialization in Android's MainActivity.java
Remove the old plugin import:
-import io.stewan.capacitor.analytics.AnalyticsPlugin;
Update the init()
call to remove the old plugin import. You may be able to remove the entire init()
call if there is nothing else in there.
// Initializes the Bridge
this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
// Additional plugins you've installed go here
// Ex: add(TotallyAwesomePlugin.class);
- add(AnalyticsPlugin.class);
}});
-
Public API changes:
instance()
has been renamed to getAppInstanceId()
setScreen()
has been renamed to setScreenName()
setUserID()
has been renamed to setUserId()
setUserProp()
has been renamed to setUserProperty()
enable()
has been deprecated in favor of setCollectionEnabled()
disable()
has been deprecated in favor of setCollectionEnabled()
Further info
Contributors ✨
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!