Flutter Gtag Analytics

Flutter Gtag Analytics:

In this totorial, you are going to learn integrating google Analytics tag ”  gtag Analytics ” in flutter App. Using this gtag Analytics you are going to track all the activities of the user who uses your app. This gtag helps you to track all the activity of your app usage and this gtag analytics helps to grow your business. Using this gtag we are tracking all the google analytics.

Google Analytics is important for any business and we can’t ignore it. And calling out to gtag can go wrong for a number of reasons, this package provides a “keep on trucking” mode for an app.

 

You can ignore errors by constructing the GoogleAnalytics a class  inside dart like this as shown below:

final ga = new GoogleAnalytics(failSilently: true);

We can use an environment variable to switch this behavior on and off depending on whether the code is running in development mode or in production mode.

final inProduction =
const String.fromEnvironment("production") == "true";
final ga = new GoogleAnalytics(failSilently: inProduction);

Try to rebuild your production code with production=true, as shown in below:

pub build --define production=true

Creating gtag Analytics in Flutter App:

Add dependency package:

Use the below code to add dependency package to pubspec.yaml file as shown below.

 

dependencies:
gtag_analytics: ^0.1.7+2

Install the package:

You can install the package from the command line using the below code.

$ pub get

Import the package:

After adding the dependency package to pubspec.yaml file you can now import the package into your dart code. without adding the dependency package if you import it into your dart code you will get package not found the error.

So now use the below code to import the package into your dart code. 

import 'package:gtag_analytics/gtag_analytics.dart';

Complete Example code for Flutter Gtag Analytics:

In below code we are creating the instance of google analytics using that we are sending the data to the google analytics. sendCustom() method sens the custom events to the google analytics

import 'package:gtag_analytics/gtag_analytics.dart';

void main() {
final inProduction = const String.fromEnvironment('production') == 'true';
final ga = new GoogleAnalytics(failSilently: inProduction);
ga.sendCustom('choose_action', category: 'play');
}

Congratulations You Have Learned Integrating gtag Analytics in Flutter….!!!

If It IS Helpful Share It With Your Friends….!!!

Leave a Reply

Categories