Flutter谷歌分析(旧版)插件google_analytics_legacy的使用
Flutter谷歌分析(旧版)插件google_analytics_legacy的使用
Flutter插件介绍
Flutter插件google_analytics_legacy
用于将事件发送到Google Analytics (Universal Analytics)。
使用说明
要使用此插件,请在pubspec.yaml
文件中添加google_analytics_legacy
作为依赖项。
Android 部署步骤
1 将您的google-service.json
复制到android/app
文件夹中。
2. 在build.gradle
中添加以下内容:
apply plugin: 'com.google.gms.google-services'
iOS 部署步骤
添加此库到pubspec.yaml
后,您就可以开始使用了。
更多信息
查看示例代码以了解更多信息。
示例代码
import 'package:google_analytics_legacy/googleAnalytics.dart';
void main(List<String> args) async {
final ga = await GoogleAnalytics.instance;
final tracker = await ga.newTracker("UA-xxxxxx");
// 屏幕视图
// https://developers.google.com/analytics/devguides/collection/android/v4/screens
{
final screenView = ScreenViewBuilder();
await tracker.setScreenName("Flutter");
await tracker.send(screenView);
}
// 事件
// https://developers.google.com/analytics/devguides/collection/android/v4/events
{
final event = EventBuilder();
event.category = "Action";
event.action = "Share";
await tracker.send(event);
}
// 增强型电子商务跟踪
// https://developers.google.com/analytics/devguides/collection/android/v4/enhanced-ecommerce
// 计量印象
{
final product = Product();
product.id = "P12345";
product.name = "Android Warhol T-Shirt";
product.category = "Apparel/T-Shirts";
product.brand = "Google";
product.variant = "Black";
product.position = 1;
product.setCustomDimension(1, "Member");
final builder = ScreenViewBuilder();
builder.addImpression(product, "Search Results");
await tracker.setScreenName("searchResults");
await tracker.send(builder);
}
// 计量操作
{
final product = Product();
product.id = "P12345";
product.name = "Android Warhol T-Shirt";
product.category = "Apparel/T-Shirts";
product.brand = "Google";
product.variant = "Black";
product.position = 1;
product.setCustomDimension( e, "Member");
final productAction = ProductAction(ProductAction.actionClick);
productAction.productActionList = "Search Results";
final builder = ScreenViewBuilder();
builder.addProduct(product);
builder.setProductAction(productAction);
builder.setCurrency("EUR");
await tracker.setScreenName("searchResults");
await tracker.send(builder);
}
// 结合印象和操作
{
// 相关产品的商品。
final relatedProduct = Product();
relatedProduct.id = "P12345";
relatedProduct.name = "Android Warhol T-Shirt";
relatedProduct.category = "Apparel/T-Shirts";
relatedProduct.brand = "Google";
relatedProduct.variant = "White";
relatedProduct.position = 1;
// 正在查看的商品。
final viewedProduct = Product();
viewedProduct.id = "P12345";
viewedProduct.name = "Android Warhol T-Shirt";
viewedProduct.category = "Apparel/T-Shirts";
viewedProduct.brand = "Google";
viewedProduct.variant = "Black";
viewedProduct.position = an;
final productAction = ProductAction(ProductAction.actionDetail);
final builder = ScreenViewBuilder();
builder.addImpression(relatedProduct, "Related Products");
builder.addProduct(viewedProduct);
builder.setProductAction(productAction);
builder.setCurrency("EUR");
await tracker.setScreenName("product");
await tracker.send(builder);
}
// 计量交易
{
final product = Product();
product.id = "P12345";
product.name = "Android Warhol T-Shirt";
product.category = "Apparel/T-Shirts";
product.brand = "Google";
product.variant = "Black";
product.price = 29.20;
product.couponCode = "APPARELSALE";
product.quantity = 1;
final productAction = ProductAction(ProductAction.actionPurchase);
productAction.transactionId = "T12345";
productAction.transactionAffiliation = "Google Store - Online";
productAction.transactionRevenue = 37.39;
productAction.transactionTax = 2.85;
productAction.transactionShipping = 5.34;
productAction.transactionCouponCode = "SUMMER2013";
final builder = ScreenViewBuilder();
builder.addProduct(product);
builder.setProductAction(productAction);
builder.setCurrency("EUR");
await tracker.setScreenName("transaction");
await tracker.send(builder);
}
}
更多关于Flutter谷歌分析(旧版)插件google_analytics_legacy的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter谷歌分析(旧版)插件google_analytics_legacy的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是如何在Flutter项目中集成和使用google_analytics_legacy
插件的示例代码。请注意,由于google_analytics_legacy
插件是为了与旧版Google Analytics(分析)服务兼容而设计的,而Google Analytics现在推荐使用Firebase Analytics,因此在实际项目中,如果可能的话,应考虑迁移到Firebase Analytics。不过,以下代码展示了如何使用google_analytics_legacy
插件。
1. 添加依赖
首先,在你的pubspec.yaml
文件中添加google_analytics_legacy
依赖:
dependencies:
flutter:
sdk: flutter
google_analytics_legacy: ^1.0.0 # 请检查最新版本号
然后运行flutter pub get
来安装依赖。
2. 初始化Google Analytics
在你的应用入口文件(通常是main.dart
)中,初始化Google Analytics服务。你需要提供你的Google Analytics跟踪ID(例如:UA-XXXXXXXX-X
)。
import 'package:flutter/material.dart';
import 'package:google_analytics_legacy/google_analytics_legacy.dart';
void main() {
// 初始化Google Analytics
Analytics analytics = Analytics(
analyticsId: "UA-XXXXXXXX-X", // 替换为你的Google Analytics跟踪ID
enableExceptionReporting: true, // 是否启用异常报告
);
// 启动应用时初始化Analytics
analytics.startTrackerWithId(analytics.analyticsId);
runApp(MyApp(analytics: analytics));
}
class MyApp extends StatelessWidget {
final Analytics analytics;
MyApp({required this.analytics});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomeScreen(analytics: analytics),
);
}
}
3. 发送屏幕视图事件
在你的应用页面中,可以发送屏幕视图事件来跟踪用户访问的页面。
import 'package:flutter/material.dart';
import 'package:google_analytics_legacy/google_analytics_legacy.dart';
class HomeScreen extends StatelessWidget {
final Analytics analytics;
HomeScreen({required this.analytics});
@override
Widget build(BuildContext context) {
// 发送屏幕视图事件
analytics.trackScreen(screenName: "/home");
return Scaffold(
appBar: AppBar(
title: Text('Home Screen'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
// 发送自定义事件
analytics.trackEvent(
category: "button",
action: "click",
label: "home_button",
value: 1,
);
Navigator.push(
context,
MaterialPageRoute(builder: (context) => OtherScreen(analytics: analytics)),
);
},
child: Text('Go to Other Screen'),
),
),
);
}
}
class OtherScreen extends StatelessWidget {
final Analytics analytics;
OtherScreen({required this.analytics});
@override
Widget build(BuildContext context) {
// 发送屏幕视图事件
analytics.trackScreen(screenName: "/other");
return Scaffold(
appBar: AppBar(
title: Text('Other Screen'),
),
body: Center(
child: Text('This is the other screen'),
),
);
}
}
4. 发送其他事件
你可以根据需要发送其他类型的事件,例如用户交互事件。
// 发送一个自定义事件
analytics.trackEvent(
category: "user_action",
action: "share",
label: "post_123",
value: null, // 如果不需要值,可以设置为null
);
注意
- 请确保你的Google Analytics账户已正确设置,并且你拥有正确的跟踪ID。
- 由于
google_analytics_legacy
插件是为旧版Google Analytics设计的,因此Google可能不再积极维护此插件。如果可能,考虑迁移到Firebase Analytics。 - 在实际发布应用之前,请彻底测试你的分析集成,以确保数据正确无误地发送到Google Analytics。
希望这能帮助你在Flutter项目中集成和使用google_analytics_legacy
插件!