Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/filiph/gtag_analytics
A minimal Google Analytics interop library for Dart.
https://github.com/filiph/gtag_analytics
Last synced: 21 days ago
JSON representation
A minimal Google Analytics interop library for Dart.
- Host: GitHub
- URL: https://github.com/filiph/gtag_analytics
- Owner: filiph
- License: mit
- Created: 2017-10-09T04:53:23.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-08-06T09:35:30.000Z (over 5 years ago)
- Last Synced: 2024-12-30T05:35:39.071Z (23 days ago)
- Language: Dart
- Homepage: https://pub.dartlang.org/packages/gtag_analytics
- Size: 15.6 KB
- Stars: 3
- Watchers: 2
- Forks: 3
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# gtag_analytics
[![Build Status](https://travis-ci.org/filiph/gtag_analytics.svg?branch=master)](https://travis-ci.org/filiph/gtag_analytics)
A library for Google Analytics tracking through the `gtag` function.
## Usage
[Add the `gtag` Google Analytics tracking code][gtag] to your HTML:
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.sendPageView();
ga.sendException("Exception: $e", fatal: true);
ga.sendSignUp(method: "email");
ga.sendCustom("play_video");[Read more][ga_docs] about these events and their fields in the official
documentation.[ga_docs]: https://developers.google.com/analytics/devguides/collection/gtagjs/events
[gtag]: https://support.google.com/analytics/answer/7476135### 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][tracker].
[Pull requests][pr] welcome.[tracker]: https://github.com/filiph/gtag_analytics/issues
[pr]: https://github.com/filiph/gtag_analytics/pulls