capacitor-community / firebase-analytics

Enable Firebase Analytics for Capacitor Apps
MIT License
160 stars 64 forks source link

Question: Recommended way to filter analytics if environment.production === false? #103

Closed stevebrowndotco closed 2 years ago

stevebrowndotco commented 3 years ago

I tried to do something like this:

import { environment } from "../environments/environment";

//....

FirebaseAnalytics.setCollectionEnabled({
   enabled: environment.production, // enable analytics only on production
});

...but I am flooded with error messages on each FirebaseAnalytics event

Thank you!

FranzSw commented 3 years ago

The easiest solution is (imo) to create a proxy service (a service / class that has the same functions that you need from FirebaseAnalytics) and check there. E.g. FirebaseAnalytics.logEvent(...) is going to be CustomAnalytics.logEvent(...) with

class CustomAnalytics {
    static logEvent(...) {
        if(!environment.production) return;
        FirebaseAnalytics.logEvent(...);
    }
}

This also enables you to switch to a different analytics provider without refactoring your whole code.

Another (common) solution is to have a development and production project & configuration.