gtag_analytics 0.1.5

  • README.md
  • Example
  • Installing
  • Versions
  • 60

gtag_analytics

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="https://www.googletagmanager.com/gtag/js?id=GA_TRACKING_ID"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'GA_TRACKING_ID');
</script>

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.sendPageView();
ga.sendException("Exception: $e", fatal: true);
ga.sendSignUp(method: "email");
ga.sendCustom("play_video");

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.

example/gtag_analytics_example.dart

// 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:


dependencies:
  gtag_analytics: "^0.1.5"

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support 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';
  
Version Uploaded Documentation Archive
0.1.5 Oct 9, 2017 Go to the documentation of gtag_analytics 0.1.5 Download gtag_analytics 0.1.5 archive
0.1.4 Oct 9, 2017 Go to the documentation of gtag_analytics 0.1.4 Download gtag_analytics 0.1.4 archive
0.1.3 Oct 9, 2017 Go to the documentation of gtag_analytics 0.1.3 Download gtag_analytics 0.1.3 archive
0.1.2 Oct 9, 2017 Go to the documentation of gtag_analytics 0.1.2 Download gtag_analytics 0.1.2 archive
0.1.1 Oct 9, 2017 Go to the documentation of gtag_analytics 0.1.1 Download gtag_analytics 0.1.1 archive

Analysis

We analyzed this package on Jun 19, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0-dev.63.0
  • pana: 0.11.3

Scores

Popularity:
Describes how popular the package is relative to other packages. [more]
30 / 100
Health:
Code health derived from static analysis. [more]
97 / 100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80 / 100
Overall score:
Weighted score of the above. [more]
60
Learn more about scoring.

Platforms

Detected platforms: web, other

Primary library: package:gtag_analytics/gtag_analytics.dart with components: js.

Suggestions

  • Maintain CHANGELOG.md.

    Changelog entries help clients to follow the progress in your code.

  • The description is too short.

    Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

  • Package is pre-v1 release.

    While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction API.

  • Fix analysis and formatting issues.

    Analysis or formatting checks reported 2 hints.

    Run dartfmt to format lib/src/gtag_analytics.dart.

Dependencies

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