Flutter移动开发集成插件one_target_mobile_sdk的使用
Flutter移动开发集成插件one_target_mobile_sdk的使用
引言
本文档将详细介绍如何在Flutter项目中集成并使用one_target_mobile_sdk
插件。通过本文,您可以快速了解如何初始化SDK、跟踪事件以及运行示例应用。
环境要求
在开始之前,请确保您的开发环境满足以下要求:
[✓] Flutter (Channel stable, 3.3.7, on macOS 13.0 22A380 darwin-arm, locale en-VN)
• Flutter version 3.3.7 on channel stable
• Framework revision e99c9c7cd9, 2022-11-01 16:59:00 -0700
• Engine revision 857bd6b74c
• Dart version 2.18.4
• DevTools version 2.15.0
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
• Platform android-33, build-tools 33.0.0
• Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)
[✓] Xcode - develop for iOS and macOS (Xcode 14.0.1)
• Build 14A400
• CocoaPods version 1.11.3
集成步骤
1. 添加依赖
在项目的pubspec.yaml
文件中添加one_target_mobile_sdk
插件,并指定最新版本号(例如:^x.y.z
)。
dependencies:
one_target_mobile_sdk: ^x.y.z
运行以下命令以安装依赖:
flutter pub get
2. 配置iOS项目
在ios/Podfile
中添加以下内容:
target 'OnetargetNotificationServiceExtension' do
use_frameworks!
pod 'OTMobileXCFramework', "1.0.2"
end
然后运行以下命令以更新依赖:
cd ios
pod install
完整的Podfile
示例:
platform :ios, '11.0'
ENV['COCOAPODS_DISABLE_STATS'] = 'true'
project 'Runner', {
'Debug' => :debug,
'Profile' => :release,
'Release' => :release,
}
def flutter_root
generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'Generated.xcconfig'), __FILE__)
unless File.exist?(generated_xcode_build_settings_path)
raise "#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure flutter pub get is executed first"
end
File.foreach(generated_xcode_build_settings_path) do |line|
matches = line.match(/FLUTTER_ROOT\=(.*)/)
return matches[1].strip if matches
end
raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Generated.xcconfig, then run flutter pub get"
end
require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root)
flutter_ios_podfile_setup
target 'Runner' do
use_frameworks!
pod 'OTMobileXCFramework'
use_modular_headers!
flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
end
post_install do |installer|
installer.pods_project.targets.each do |target|
flutter_additional_ios_build_settings(target)
end
end
target 'OnetargetNotificationServiceExtension' do
use_frameworks!
pod 'OTMobileXCFramework', "1.0.2"
end
3. 配置Android项目
在android/build.gradle
中添加以下内容:
buildscript {
ext.kotlin_version = '1.7.20'
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.3.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
在android/app/build.gradle
中配置compileSdkVersion
和minSdkVersion
:
compileSdkVersion 33
minSdkVersion 21
同时,在AndroidManifest.xml
中为<application>
标签添加tools:replace="android:label"
属性:
<application
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher"
android:label="one_target_mobile_sdk_example"
tools:replace="android:label">
</application>
使用SDK
Step 1: 初始化SDK
在main()
函数中调用G1SDK.instance.setupSDK()
方法进行初始化。
void main() async {
WidgetsFlutterBinding.ensureInitialized();
_setupTracking();
runApp(const MyApp());
}
void _setupTracking() {
G1SDK.instance.setupSDK(
"[your_workspace_id]", // 替换为您的Workspace ID
"[your_app_push_id]", // 替换为您的App Push ID
isShowLog: false,
isEnableIAM: true,
).then((isSetupSuccess) {
print("_setupTracking isSetupSuccess $isSetupSuccess");
});
}
参数说明:
workspace_id
: 您在OneTarget门户中创建的工作区ID。app_push_id
: OneTarget提供的推送Key。isShowLog
: 是否启用日志记录。isEnableIAM
: 是否启用IAM功能。
Step 2: 跟踪事件
调用G1SDK.instance.trackEvent()
方法跟踪用户行为。
void _trackEvent() {
var identify = {"ext_identify": "ext_identify_value"};
var profile = <Map>[];
profile.add({"ext_identify": "ext_identify_value"});
var eventName = "add_to_cart";
var eventData = {
"ecommerce.currency": "VND",
"ecommerce.trip_from": "HAN",
"ecommerce.trip_to": "SGN",
"ecommerce.trip_start_date": "2022-09-23",
"ecommerce.trip_return_date": "2022-09-23",
"ecommerce.trip_passengers": "1",
"ecommerce.trip_passengers_adult": "2",
"ecommerce.trip_passengers_children": "1",
"ecommerce.trip_passengers_infant": "0",
"ecommerce.trip_type": "roundTrip",
"ecommerce.items.0.item_id": "VJ197",
"ecommerce.items.0.item_name": "Flight:HAN:SGN",
"ecommerce.items.0.item_category": "FareOption",
"ecommerce.items.0.item_variant": "Eco",
"ecommerce.items.0.item_list_id": "Outbound:HAN:SGN",
"ecommerce.items.0.index": "1",
"ecommerce.items.0.quantity": "1",
"ecommerce.items.0.price": "39.000đ",
"ecommerce.items.0.flight_time": "06:20-08:30",
"ecommerce.items.0.flight_from": "HAN",
"ecommerce.items.0.flight_to": "SGN",
"ecommerce.items.0.flight_date": "2022-09-23",
"ecommerce.trip_route": "SGN-HN",
};
G1SDK.instance.trackEvent(
identify,
profile,
eventName,
eventData,
);
}
示例代码
以下是完整的示例代码,位于example/lib/main.dart
中:
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:one_target_mobile_sdk/one_target_mobile_sdk.dart';
import 'package:one_target_mobile_sdk_example/common/constant.dart';
import 'package:one_target_mobile_sdk_example/screens/home/home_screen.dart';
import 'common/utils.dart';
import 'controller/debug_controller.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
DebugController cDebug = Get.put(DebugController());
_setupTracking(cDebug);
runApp(const HomeScreen());
}
void _setupTracking(DebugController debugController) {
Constant.setEnv(Constant.environmentProd);
G1SDK.instance.setupSDK(
Constant.getWorkSpaceId(),
Constant.getOneTargetAppPushID(),
isShowLog: kDebugMode,
isEnableIAM: true,
onLog: (String log) {
Utils.log("onLog $log");
debugController.addLog(log);
},
env: Constant.getEnv(),
).then((isSetupSuccess) {
Utils.log("_setupTracking isSetupSuccess $isSetupSuccess");
});
}
运行示例应用
完成上述配置后,您可以运行示例应用以验证功能是否正常工作:
cd example
cd ios
flutter clean
flutter packages get
flutter run --verbose
更多关于Flutter移动开发集成插件one_target_mobile_sdk的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
one_target_mobile_sdk
是一个用于移动应用开发的插件,通常用于集成 OneTarget 提供的功能,如用户行为分析、推送通知、个性化推荐等。以下是如何在 Flutter 项目中集成和使用 one_target_mobile_sdk
的步骤。
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 one_target_mobile_sdk
的依赖。
dependencies:
flutter:
sdk: flutter
one_target_mobile_sdk: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来获取依赖。
2. 初始化 SDK
在你的 Flutter 应用中,通常需要在 main.dart
文件中初始化 one_target_mobile_sdk
。
import 'package:flutter/material.dart';
import 'package:one_target_mobile_sdk/one_target_mobile_sdk.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// 初始化 OneTarget SDK
await OneTargetMobileSdk.init(
appKey: 'YOUR_APP_KEY', // 替换为你的 App Key
appSecret: 'YOUR_APP_SECRET', // 替换为你的 App Secret
environment: Environment.production, // 选择环境:production 或 sandbox
);
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
3. 设置用户信息
如果你需要跟踪用户行为或发送个性化推送,可以设置用户信息。
OneTargetMobileSdk.setUserInfo(
userId: 'USER_ID', // 用户唯一标识
userProperties: {
'name': 'John Doe',
'email': 'john.doe@example.com',
// 其他自定义属性
},
);
4. 记录用户事件
你可以记录用户的行为事件,以便进行分析。
OneTargetMobileSdk.trackEvent(
eventName: 'purchase', // 事件名称
eventProperties: {
'product_id': '12345',
'price': 99.99,
// 其他自定义属性
},
);
5. 处理推送通知
如果你集成了推送通知功能,可以在 onMessage
回调中处理收到的通知。
OneTargetMobileSdk.onMessage((Map<String, dynamic> message) {
print('Received message: $message');
// 处理推送通知
});
6. 其他功能
one_target_mobile_sdk
可能还提供了其他功能,如 A/B 测试、个性化推荐等。你可以根据官方文档或 SDK 提供的 API 进行集成和使用。
7. 调试与日志
在开发过程中,你可以启用调试日志来帮助排查问题。
OneTargetMobileSdk.setDebugEnabled(true);
8. 发布应用
在发布应用之前,请确保你已经正确配置了生产环境的 App Key 和 App Secret,并禁用了调试日志。
OneTargetMobileSdk.setDebugEnabled(false);