Flutter数据转换插件convertedin_plugin的使用
Flutter数据转换插件convertedin_plugin的使用
convertedin_plugin
是一个用于Flutter的应用程序插件,它集成了Convertedin,这是一个专为电子商务业务设计的强大营销操作系统。该插件允许开发者轻松跟踪关键用户事件,并与Convertedin SDK进行交互,以便基于用户数据和洞察力进行个性化的多渠道营销。
功能
该插件使开发者能够:
- 使用平台凭据初始化Convertedin SDK。
- 跟踪各种用户事件,包括:
- 用户识别(通过电子邮件或电话)。
- 用户注册。
- 页面浏览跟踪。
- 内容查看跟踪。
- 添加到购物车跟踪。
- 结账启动跟踪。
- 购买跟踪。
- 自定义事件跟踪。
- 推送通知事件跟踪。
- 管理设备令牌以接收推送通知。
安装
要安装该插件,请将其添加到您的 pubspec.yaml
文件中:
dependencies:
convertedin_plugin: ^0.0.1
然后运行以下命令以获取依赖项:
flutter pub get
使用
导入插件
首先,在Dart文件中导入 convertedin_plugin
包:
import 'package:convertedin_plugin/convertedin_plugin.dart';
初始化SDK
通过提供 pixelId
和 storeUrl
来初始化SDK:
final convertedin = Convertedin();
void initializeSDK() async {
final result = await convertedin.initialize(
pixelId: "your-pixel-id",
storeUrl: "your-store-url",
);
print(result);
}
识别用户
通过电子邮件或电话号码来识别用户:
void identifyUser() async {
final result = await convertedin.identifyUser(email: "user@example.com");
print(result);
}
注册用户
跟踪用户注册事件:
void registerUser() async {
final result = await convertedin.registerUser("user@example.com");
print(result);
}
跟踪事件
1. 页面视图事件
跟踪网站上的页面视图:
void logPageView() async {
final result = await convertedin.pageViewEvent();
print(result);
}
2. 查看内容事件
跟踪用户查看内容,例如产品页面:
void logViewContent() async {
final result = await convertedin.viewContentEvent(
currency: "USD",
total: "100",
products: [
ConvertedInProduct(id: "product1", name: "Product 1", price: "50", quantity: 1),
ConvertedInProduct(id: "product2", name: "Product 2", price: "50", quantity: 1),
],
);
print(result);
}
3. 添加到购物车事件
跟踪用户将商品添加到购物车:
void logAddToCart() async {
final result = await convertedin.addToCartEvent(
currency: "USD",
total: "150",
products: [
ConvertedInProduct(id: "product1", name: "Product 1", price: "50", quantity: 1),
ConvertedInProduct(id: "product2", name: "Product 2", price: "100", quantity: 1),
],
);
print(result);
}
4. 结账启动事件
跟踪用户启动结账过程:
void logCheckoutInitiation() async {
final result = await convertedin.initiateCheckoutEvent(
currency: "USD",
total: "150",
products: [
ConvertedInProduct(id: "product1", name: "Product 1", price: "50", quantity: 1),
ConvertedInProduct(id: "product2", name: "Product 2", price: "100", quantity: 1),
],
);
print(result);
}
5. 购买事件
跟踪用户完成购买:
void logPurchaseEvent() async {
final result = await convertedin.purchaseEvent(
currency: "USD",
total: "150",
products: [
ConvertedInProduct(id: "product1", name: "Product 1", price: "50", quantity: 1),
ConvertedInProduct(id: "product2", name: "Product 2", price: "100", quantity: 1),
],
orderId: "order-12345",
);
print(result);
}
6. 自定义事件
跟踪带有自定义名称的事件:
void logCustomEvent() async {
final result = await convertedin.customEvent(
eventName: "Custom Event",
currency: "USD",
total: "150",
products: [
ConvertedInProduct(id: "product1", name: "Product 1", price: "50", quantity: 1),
ConvertedInProduct(id: "product2", name: "Product 2", price: "100", quantity: 1),
],
);
print(result);
}
推送通知管理
要使用Convertedin SDK的推送通知功能,您需要集成Firebase通知。
保存设备令牌
成功集成Firebase后,调用此函数将Firebase令牌发送给SDK以开始从您的仪表板接收通知。⚠️重要提示:每次Firebase令牌更新时,都必须调用 saveDeviceToken
函数以保存新的Firebase令牌。
void saveDeviceToken() async {
final result = await convertedin.saveDeviceToken("device-token");
print(result);
}
删除设备令牌
删除设备令牌以禁用推送通知:
void deleteDeviceToken() async {
final result = await convertedin.deleteDeviceToken();
}
处理推送通知点击
此事件通常在用户点击推送通知时触发,并应传递接收到的活动ID给函数:
void onPushNotificationClick() async {
final result = await convertedin.onPushNotificationClicked("campaign-id");
print(result);
}
可用事件
以下是可以通过插件跟踪的事件列表:
- SDK初始化:
initialize(pixelId, storeUrl)
- 识别用户:
identifyUser(email, phone, countryCode)
- 用户注册:
registerUser(email)
- 页面视图事件:
pageViewEvent()
- 查看内容事件:
viewContentEvent(currency, total, products)
- 添加到购物车事件:
addToCartEvent(currency, total, products)
- 结账启动事件:
initiateCheckoutEvent(currency, total, products)
- 购买事件:
purchaseEvent(currency, total, products, orderId)
- 自定义事件:
customEvent(eventName, currency, total, products)
- 保存设备令牌:
saveDeviceToken(token)
- 删除设备令牌:
deleteDeviceToken()
- 处理推送通知点击:
onPushNotificationClicked(campaignId)
完整示例
以下是一个完整的示例,展示了如何在Flutter应用中使用 convertedin_plugin
插件:
import 'package:convertedin_plugin/convertedin_plugin.dart';
import 'package:flutter/material.dart';
import 'package:convertedin_plugin/convertedin_product.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
[@override](/user/override)
Widget build(BuildContext context) {
return const MaterialApp(
debugShowCheckedModeBanner: false,
title: 'ConvertedIn Plugin Demo',
home: EventLoggerScreen(),
);
}
}
class EventLoggerScreen extends StatefulWidget {
const EventLoggerScreen({super.key});
[@override](/user/override)
State<EventLoggerScreen> createState() => _EventLoggerScreenState();
}
class _EventLoggerScreenState extends State<EventLoggerScreen> {
final convertedin = Convertedin();
// Replace these with your actual values
String pixelId = "your-pixel-id";
String storeUrl = "yourstore.com";
[@override](/user/override)
void initState() {
super.initState();
initializeSDK();
}
Future<void> initializeSDK() async {
try {
String? initResponse =
await convertedin.initialize(pixelId: pixelId, storeUrl: storeUrl);
debugPrint("SDK initialized: $initResponse");
} catch (e) {
debugPrint("Error initializing SDK: $e");
}
}
Future<void> logEvent(String eventType) async {
try {
List<ConvertedInProduct> products = [
ConvertedInProduct(id: 1, name: "Product A", quantity: 2),
ConvertedInProduct(id: 2, name: "Product B", quantity: 5),
];
String currency = "USD";
String total = "55";
String? response;
switch (eventType) {
case "pageView":
response = await convertedin.pageViewEvent();
break;
case "viewContent":
response = await convertedin.viewContentEvent(
currency: currency, total: total, products: products);
break;
case "addToCart":
response = await convertedin.addToCartEvent(
currency: currency, total: total, products: products);
break;
case "purchase":
response = await convertedin.purchaseEvent(
currency: currency,
total: total,
products: products,
orderId: "order123");
break;
case "customEvent":
response = await convertedin.customEvent(
"customEventName", currency, total, products);
break;
case "saveDeviceToken":
response = await convertedin.saveDeviceToken("device-token-123");
break;
case "deleteDeviceToken":
response = await convertedin.deleteDeviceToken();
break;
case "pushNotificationClicked":
response = await convertedin.onPushNotificationClicked("campaign123");
break;
case "registerUser":
response = await convertedin.registerUser("123@example.com");
break;
case "identifyUser":
response = await convertedin.identifyUser(
phone: "1234567890", countryCode: "+20");
break;
}
debugPrint("$eventType event logged: $response");
} catch (e) {
debugPrint("Error logging $eventType event: $e");
}
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("ConvertedIn Events"),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ElevatedButton(
onPressed: () => logEvent("pageView"),
child: const Text("Log Page View Event"),
),
ElevatedButton(
onPressed: () => logEvent("viewContent"),
child: const Text("Log View Content Event"),
),
ElevatedButton(
onPressed: () => logEvent("addToCart"),
child: const Text("Log Add to Cart Event"),
),
ElevatedButton(
onPressed: () => logEvent("purchase"),
child: const Text("Log Purchase Event"),
),
ElevatedButton(
onPressed: () => logEvent("customEvent"),
child: const Text("Log Custom Event"),
),
ElevatedButton(
onPressed: () => logEvent("saveDeviceToken"),
child: const Text("Save Device Token"),
),
ElevatedButton(
onPressed: () => logEvent("deleteDeviceToken"),
child: const Text("Delete Device Token"),
),
ElevatedButton(
onPressed: () => logEvent("pushNotificationClicked"),
child: const Text("Log Push Notification Clicked Event"),
),
ElevatedButton(
onPressed: () => logEvent("identifyUser"),
child: const Text("Log Identify User Event"),
),
ElevatedButton(
onPressed: () => logEvent("registerUser"),
child: const Text("Log Register User Event"),
),
],
),
),
),
);
}
}
更多关于Flutter数据转换插件convertedin_plugin的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter数据转换插件convertedin_plugin的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是一个关于如何在Flutter项目中使用convertedin_plugin
进行数据转换的示例代码。需要注意的是,由于convertedin_plugin
可能是一个假设的或不太常见的插件,具体的API和功能可能会有所不同。以下示例将基于一个假设的插件API进行说明。
首先,确保你已经在pubspec.yaml
文件中添加了convertedin_plugin
依赖:
dependencies:
flutter:
sdk: flutter
convertedin_plugin: ^x.y.z # 替换为实际版本号
然后,运行flutter pub get
来获取依赖。
接下来,在你的Flutter项目中,你可以按照以下方式使用convertedin_plugin
进行数据转换。以下示例展示了如何将字符串数据转换为JSON对象,然后再转换回字符串。
import 'package:flutter/material.dart';
import 'package:convertedin_plugin/convertedin_plugin.dart'; // 导入插件
import 'dart:convert'; // 用于JSON转换
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String originalData = '{"name": "John", "age": 30}';
Map<String, dynamic> convertedData;
String convertedBackToString;
@override
void initState() {
super.initState();
_convertData();
}
Future<void> _convertData() async {
// 假设插件有一个方法可以将字符串转换为Map
// 这里我们使用dart:convert库作为示例,因为convertedin_plugin的具体API未知
convertedData = jsonDecode(originalData);
// 假设插件有一个方法可以将Map转换回字符串
// 使用dart:convert库的jsonEncode作为示例
convertedBackToString = jsonEncode(convertedData);
// 如果convertedin_plugin有特定的转换方法,你可以这样调用(假设的API)
// convertedData = await ConvertedinPlugin.stringToMap(originalData);
// convertedBackToString = await ConvertedinPlugin.mapToString(convertedData);
setState(() {}); // 更新UI
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Data Conversion Demo'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Original Data (String):'),
Text(originalData, style: TextStyle(fontSize: 16)),
SizedBox(height: 16),
Text('Converted Data (Map):'),
if (convertedData != null)
Text(convertedData.toString(), style: TextStyle(fontSize: 16)),
SizedBox(height: 16),
Text('Converted Back to String:'),
if (convertedBackToString != null)
Text(convertedBackToString, style: TextStyle(fontSize: 16)),
],
),
),
);
}
}
在这个示例中,我使用了dart:convert
库中的jsonDecode
和jsonEncode
方法来模拟数据转换,因为convertedin_plugin
的具体API未知。如果convertedin_plugin
提供了类似的功能,你可以替换掉示例中的jsonDecode
和jsonEncode
调用,使用插件提供的API。
请确保查看convertedin_plugin
的官方文档以获取准确的API和使用方法。如果插件提供了特定的转换方法,你可以按照文档中的说明进行调用。