gtag_analytics 0.1.7+2

  • Readme
  • Changelog
  • Example
  • Installing
  • 69

gtag_analytics #

Build Status

A library for Google Analytics tracking through the gtag function.

Usage #

Add the gtag Google Analytics tracking code to your HTML:

<!-- Global Site Tag (gtag.js) - Google Analytics -->
<script async src=""></script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'GA_TRACKING_ID');

In Dart, import this package.

import 'package:gtag_analytics/gtag_analytics.dart';

Create an instance of the GoogleAnalytics class:

final ga = new GoogleAnalytics();

Use the instance to send data:

ga.sendException("Exception: $e", fatal: true);
ga.sendSignUp(method: "email");

Read more about these events and their fields in the official documentation.

Only throwing in development, not in production #

While analytics is important, you probably don't want it to break your whole app when something is wrong in the measurement code. And since calling out to gtag can go wrong for a number of reasons (forgotten tracking snippet in HTML, gtag not defined in time, etc.), this package provides a "keep on trucking" mode.

You can ignore errors by constructing the GoogleAnalytics class like this:

final ga = new GoogleAnalytics(failSilently: true);

The more realistic approach is to use an environment variable to switch this behavior on and off depending on whether the code is running in development or in production mode:

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

Then make sure you're building your production code with production=true, like this:

pub build --define production=true

You could also go the other way around, and use something like development=true. That way, you won't accidentally deploy a version that fails on Google Analytics when you forget to provide the production=true option on pub build. (On the other hand, you might unknowingly be ignoring a valid error when you forget to provide the development=true option in development on pub serve.)

Testing #

Most of the functionality lives in the Google Analytics JavaScript code, but there are some tests that cover the wrapper. They will only work in a browser, so you'll have to run them with the -p option, like this:

pub run test -p chrome

Features and bugs #

Please file feature requests and bugs at the issue tracker. Pull requests welcome.

0.1.7+2 #

  • Dart 2 compatibility

0.1.6 #

  • Add Travis CI testing


// Copyright (c) 2017, filiph. All rights reserved. Use of this source code
// is governed by a BSD-style license that can be found in the LICENSE file.

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');

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  gtag_analytics: ^0.1.7+2

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:gtag_analytics/gtag_analytics.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Jan 25, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.4

Health suggestions

Fix lib/src/gtag_analytics.dart. (-3.45 points)

Analysis of lib/src/gtag_analytics.dart reported 7 hints, including:

line 13 col 33: Unnecessary new keyword.

line 26 col 37: Use = to separate a named parameter from its default value.

line 30 col 21: Unnecessary new keyword.

line 47 col 22: Unnecessary new keyword.

line 57 col 33: Use = to separate a named parameter from its default value.

Maintenance suggestions

Package is getting outdated. (-50.68 points)

The package was last published 78 weeks ago.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.20.1 <3.0.0
js ^0.6.1 0.6.1+1
meta ^1.1.2 1.1.8
Dev dependencies
test >=0.12.0 <2.0.0