track_wise_flutter 3.0.0
track_wise_flutter: ^3.0.0 copied to clipboard
TrackWise attribution flutter project.
example/lib/main.dart
import 'dart:async';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:track_wise_flutter/event/add_to_cart_event.dart';
import 'package:track_wise_flutter/event/add_to_wishlist_event.dart';
import 'package:track_wise_flutter/event/content_view_event.dart';
import 'package:track_wise_flutter/event/initiated_checkout_event.dart';
import 'package:track_wise_flutter/event/purchase_event.dart';
import 'package:track_wise_flutter/event/search_event.dart';
import 'package:track_wise_flutter/event/subscribe_event.dart';
import 'package:track_wise_flutter/event/track_wise_content.dart';
import 'package:track_wise_flutter/event/track_wise_userinfo.dart';
import 'package:track_wise_flutter/on_attribution_callback.dart';
import 'package:track_wise_flutter/track_wise.dart';
import 'package:track_wise_flutter/track_wise_attribution.dart';
import 'package:track_wise_flutter/track_wise_config.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAttributionCallback implements OnAttributionCallback {
@override
void onSuccess(TrackWiseAttribution attribution) {
/// Successfully obtained attribution result
debugPrint("onSuccess: ${attribution.attribution}");
}
@override
void onFailed(int errCode) {
/// Failed to obtain attribution result
debugPrint("onFailed: $errCode");
}
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
startSdk();
}
Future<void> startSdk() async {
final config = TrackWiseConfig(
accountId: "ACCOUNT_ID",
devToken: "DEV_TOKEN",
appleStoreId: "APPLE_STORE_ID",
/// [Optional] Used to identify the installation package source
packageSource: "PlayStore",
/// [Optional] If using Meta media channel, pass the Meta appId
metaAppId: "META_APP_ID",
/// [Optional] Defaults to false. When true is passed, it means that the developer wants to customize the user ID to associate the user with the attribution information.
/// Attribution will be reported only if and when the developer passes in a customized user ID.
/// When false is passed, the SDK will not generate a user ID internally.
waitForCustomerUserId: true,
/// [Optional] The timeout for waiting for iOS ATT Authorization.
waitATTTimeoutMs: 10000,
/// [Optional] Listen for attribution results
onAttributionCallback: _MyAttributionCallback(),
);
/// [Optional] enable log if you want to debug
TrackWise.enableLog();
await TrackWise.start(config);
/// [iOS] Check clipboard info, may trigger an pasteboard alert on the first time after install.
await TrackWise.checkClipboardInfo();
/// [Optional] Default is empty. Used to associate the developer's business logic account system with attribution information
TrackWise.setCustomerUserId("CUSTOMER_USER_ID");
/// [Optional] Detailed user information helps improve event conversion rates
final userInfo = TrackWiseUserInfo(
firstName: "FIRST_NAME",
lastName: "LAST_NAME",
countryName: "COUNTRY_NAME",
city: "CITY",
emails: ["EMAIL_1", "EMAIL_2"],
phones: ["PHONE_1", "PHONE_2"],
fbLoginId: "FB_LOGIN_ID",
);
TrackWise.setUserInfo(userInfo);
/// Track events
trackEvents();
/// You can also directly call and obtain the attribution information after the SDK is initialized.
/// It should be noted that the method for directly obtaining attribution results will return local cache,
/// so if the local cache has not been generated, the attribution result will be null.
var attribution = await TrackWise.getAttribution();
debugPrint("attribution: ${attribution.attribution}");
}
void trackEvents() {
trackContentViewEvent();
trackAddToWishlistEvent();
trackAddToCartEvent();
trackSearchEvent();
trackInitiatedCheckoutEvent();
trackPurchaseEvent();
trackSubscribeEvent();
}
/// When the user views a specific product details page
void trackContentViewEvent() {
final event = ContentViewEvent();
event.content = TrackWiseContent(
/// Product ID, required
contentId: "ProductId",
/// Product name, optional
contentName: "ProductName",
/// Currency unit. Required if product unit price is provided, otherwise optional
currency: "USD",
/// Product unit price, optional
price: 9.9,
/// Product quantity
quantity: 1,
);
TrackWise.trackEvent(event);
}
/// When the user adds a product to their wishlist
void trackAddToWishlistEvent() {
final event = AddToWishlistEvent();
event.content = TrackWiseContent(
/// Product ID, required
contentId: "ProductId",
/// Product name, optional
contentName: "ProductName",
/// Currency unit. Required if product unit price is provided, otherwise optional
currency: "USD",
/// Product unit price, optional
price: 9.9,
/// Product quantity
quantity: 1,
);
TrackWise.trackEvent(event);
}
/// When the user adds a product to the shopping cart
void trackAddToCartEvent() {
final event = AddToCartEvent();
event.content = TrackWiseContent(
/// Product ID, required
contentId: "ProductId",
/// Product name, optional
contentName: "ProductName",
/// Currency unit. Required if product unit price is provided, otherwise optional
currency: "USD",
/// Product unit price, optional
price: 9.9,
/// Product quantity
quantity: 1,
);
TrackWise.trackEvent(event);
}
/// When the user enters the search results page
void trackSearchEvent() {
final event = SearchEvent();
event
..searchString = "SearchString"
..contents = [
TrackWiseContent(
/// Product ID, required
contentId: "ProductId_1",
/// Product name, optional
contentName: "ProductName_1",
/// Currency unit. Required if product unit price is provided, otherwise optional
currency: "USD",
/// Product unit price, optional
price: 9.9,
/// Product quantity, optional
quantity: 1,
),
TrackWiseContent(
/// Product ID, required
contentId: "ProductId_2",
/// Product name, optional
contentName: "ProductName_2",
/// Currency unit. Required if product unit price is provided, otherwise optional
currency: "USD",
/// Product unit price, optional
price: 19.9,
/// Product quantity, optional
quantity: 2,
),
];
TrackWise.trackEvent(event);
}
/// When the user initiates checkout but does not complete the checkout process
void trackInitiatedCheckoutEvent() {
final event = InitiatedCheckoutEvent();
event
.contents = [
TrackWiseContent(
/// Product ID, required
contentId: "ProductId_1",
/// Product name, optional
contentName: "ProductName_1",
/// Currency unit, required
currency: "USD",
/// Product unit price, required
price: 9.9,
/// Product quantity, required
quantity: 1,
),
TrackWiseContent(
/// Product ID, required
contentId: "ProductId_2",
/// Product name, optional
contentName: "ProductName_2",
/// Currency unit, required
currency: "USD",
/// Product unit price, required
price: 19.9,
/// Product quantity, required
quantity: 2,
),
];
TrackWise.trackEvent(event);
}
/// When the user successfully completes an in-app purchase
void trackPurchaseEvent() {
final event = PurchaseEvent();
/// Real order ID, required
final randomOrderId = 'ORDER_${DateTime.now().millisecondsSinceEpoch}_${Random().nextInt(1000000)}';
event
..orderId = randomOrderId
/// Estimated revenue. If revenue is unknown, can be same as total product price, required
..revenue = 49.7
..contents = [
TrackWiseContent(
/// Product ID, required
contentId: "ProductId_1",
/// Product name, optional
contentName: "ProductName_1",
/// Currency unit, required
currency: "USD",
/// Product unit price, required
price: 9.9,
/// Product quantity, required
quantity: 1,
),
TrackWiseContent(
/// Product ID, required
contentId: "ProductId_2",
/// Product name, optional
contentName: "ProductName_2",
/// Currency unit, required
currency: "USD",
/// Product unit price, required
price: 19.9,
/// Product quantity, required
quantity: 2,
),
];
TrackWise.trackEvent(event);
}
/// When the user successfully subscribes
void trackSubscribeEvent() {
final event = SubscribeEvent();
/// Real order ID, required
final randomOrderId = 'ORDER_${DateTime.now().millisecondsSinceEpoch}_${Random().nextInt(1000000)}';
event
..orderId = randomOrderId
/// Estimated revenue. If revenue is unknown, can be same as total product price, required
..revenue = 9.9
..content = TrackWiseContent(
/// Product ID, required
contentId: "ProductId_1",
/// Product name, optional
contentName: "ProductName_1",
/// Currency unit, required
currency: "USD",
/// Product unit price, required
price: 9.9,
/// Product quantity, required
quantity: 1,
)
/// Subscription days, required
..subscribeDay = 7;
TrackWise.trackEvent(event);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('TrackWiseFlutterPlugin')),
body: const Center(),
),
);
}
}