flutter usage
usage is a wrapper around google analytics for command-line, web, and flutter apps.
for web apps
to use this library as a web app, import the usage_html.dart
library and instantiate the analyticshtml
class.
when you are creating a new property at google analytics make sure to select the mobile app option, not the website option.
for flutter apps
flutter applications can use the analyticsio
version of this library. they will need to specify the documents directory in the constructor in order to tell the library where to save the analytics preferences:
import 'package:flutter/services.dart';
import 'package:usage/usage_io.dart';
void main() {
final string ua = ...;
analytics ga = new analyticsio(ua, 'ga_test', '3.0',
documentsdirectory: pathprovider.getapplicationdocumentsdirectory());
...
}
for command-line apps
to use this library as a command-line app, import the usage_io.dart
library and instantiate the analyticsio
class.
note, for cli apps, the usage library will send analytics pings asynchronously.
this is useful it that it doesn’t block the app generally. it does have one side-effect, in that outstanding asynchronous requests will block termination of the vm until that request finishes. so, for short-lived cli tools, pinging google analytics can cause the tool to pause for several seconds before it terminates. this is often undesired – gathering analytics information shouldn’t negatively affect the tool’s ux.
one solution to this is to use the waitforlastping({duration timeout})
method on the analytics object. this will wait until all outstanding analytics requests have completed, or until the specified duration has elapsed. so, cli apps can do something like:
await analytics.waitforlastping(timeout: new duration(milliseconds: 200));
analytics.close();
or:
await analytics.waitforlastping(timeout: new duration(milliseconds: 200));
exit(0);
using the api
import the package (in this example we use the dart:io
version):
import 'package:usage/usage_io.dart';
and call some analytics code:
final string ua = ...;
analytics ga = new analyticsio(ua, 'ga_test', '3.0');
ga.analyticsopt = analyticsopt.optin;
ga.sendscreenview('home');
ga.sendexception('foo exception');
ga.sendscreenview('files');
ga.sendtiming('writetime', 100);
ga.sendtiming('readtime', 20);
when do we send analytics data?
you can use this library in an opt-in manner or an opt-out one. it defaults to opt-out – data will be sent to google analytics unless the user is explicitly opts-out. the mode can be adjusted by changing the value of the analytics.analyticsopt
field.
opt-out in opt-out mode, if the user does not explicitly opt-out of collecting analytics, the usage library will send usage data.
opt-in in opt-in mode, no data will be sent until the user explicitly opt-in to the collection. this includes screen views, events, timing information, and exceptions.
other info
for both classes, you need to provide a google analytics tracking id, the application name, and the application version.
note: this library is intended for use with the google analytics application / mobile app style tracking ids (as opposed to the web site style tracking ids).
for more information, please see the google analytics measurement protocol policy.
Comments are closed.