搜索插件、代码仓与 APP 分类

GitHub 代码仓dart-archive/usage

用法

适用于命令行、Web 和 Flutter 应用的 Google Analytics 封装。

用法 项目封面
星标
148
复刻
40
最近推送(UTC)
2024年6月20日
项目状态
已归档

原始 README

以下为英文项目原文快照,最新内容请访问 GitHub。

pub package package publisher

DEPRECATED

This package is no longer maintained. https://github.com/dart-archive/usage/issues/185 has details and discussion, but briefly:

  • this package uses the older Universal Analytics protocol to record hits to Google Analytics
  • Universal Analytics properties will stop processing new hits on July 1, 2023
  • this library does not support the newer GA4 protocol
  • we (the Dart team) originally built this package for our own use, but we're no longer consuming it

Clients who want to record hits to Google Analytics could:

  • for web clients, wrapping the gtags.js library is a good solution
  • for native clients, sending hits to the new protocol is not much more difficult than sending HTTP POSTs to the right endpoint

See also https://github.com/dart-archive/usage/issues/185 other potential remediations.

For web apps

To use this library as a web app, import the usage_html.dart library and instantiate the AnalyticsHtml class.

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 in 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 effect 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 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 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.

Contributing

Tests can be run using pub run test.

Dart Archive GitHub avatar
GitHub 作者

Dart Archive ↗

Legacy projects kept around for posterity – See github.com/dart-lang for current work

语言DartHTML

使用的依赖

依赖清单 4 项

  • meta^1.7.0
  • path^1.8.0
  • dart_flutter_team_lints开发依赖^1.0.0
  • test开发依赖^1.16.0

如何选择适合项目的代码仓?

先按使用场景筛选,再比较项目活跃情况,最后到原始仓库核对使用要求。

  1. 通过仓库名、作者或描述搜索,再按技术分类缩小范围。
  2. 结合星标、复刻与最近推送比较项目;星标反映社区关注,不代表质量保证。
  3. 使用前在 GitHub 核对 README、许可证与 Issues。

来源与维护

“A repository is the most basic element of GitHub.”

GitHub Docs — About repositories ↗