WT 1.1.18+4 A/B Test sales page

This commit is contained in:
bossanyit
2021-06-05 15:39:12 +02:00
parent d5deaf48a9
commit 0ca3b71c03
48 changed files with 734 additions and 1223 deletions
+69 -85
View File
@@ -1,12 +1,11 @@
import 'dart:async';
import 'dart:io';
import 'dart:math' as math;
import 'package:aitrainer_app/model/cache.dart';
import 'package:aitrainer_app/model/product.dart';
import 'package:aitrainer_app/model/product_test.dart';
import 'package:aitrainer_app/model/purchase.dart';
import 'package:aitrainer_app/repository/description_repository.dart';
import 'package:aitrainer_app/repository/split_test_respository.dart';
import 'package:aitrainer_app/service/logging.dart';
import 'package:aitrainer_app/service/purchase_service.dart';
import 'package:aitrainer_app/util/enums.dart';
@@ -14,37 +13,43 @@ import 'package:aitrainer_app/util/purchases.dart';
import 'package:aitrainer_app/util/track.dart';
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:firebase_remote_config/firebase_remote_config.dart';
import 'package:purchases_flutter/offering_wrapper.dart';
part 'sales_event.dart';
part 'sales_state.dart';
class SalesBloc extends Bloc<SalesEvent, SalesState> with Logging {
List<ProductTest>? tests = [];
List<Product> product2Display = [];
List<String> productText2Display = ["WorkoutTest annual", "WorkoutTest montly"];
final SplitTestRepository splitTestRepository = SplitTestRepository();
int productSet = -1;
final DescriptionRepository descriptionRepository = DescriptionRepository();
SalesBloc() : super(SalesInitial());
String? salesText;
String? premiumFunctions = "";
String salesButtonText = "<h1>Workout Test Monthly</h1><p>localizedPrice</p><p><small>cancel any time</small></p>";
Product? offeredProduct;
Product? getProductByName(String name) {
Product? product;
if (product2Display.isNotEmpty) {
product2Display.forEach((element) {
if (element.type == name) {
product = element;
salesButtonText = salesButtonText.replaceFirst(RegExp(r'localizedPrice'), product!.localizedPrice!);
print("Localized Price ${product!.localizedPrice!} - Text: $salesButtonText");
}
});
void init() async {
if (Cache().userLoggedIn == null) {
throw Exception("Please log in");
}
return product;
salesText = splitTestRepository.getSplitTestValue("sales_page_text_a");
if (salesText == null || salesText!.isEmpty) {
salesText = descriptionRepository.getDescriptionByName("sales_page_text_a");
}
print("sales Text: $salesText");
getProductsTexts();
premiumFunctions = descriptionRepository.getDescriptionByName("premium_functions");
if (premiumFunctions == null || premiumFunctions!.isEmpty) {
premiumFunctions = "";
}
await RevenueCatPurchases().getOfferings();
this.getProductSet();
Track().track(TrackingEvent.sales_page);
}
@override
@@ -53,33 +58,11 @@ class SalesBloc extends Bloc<SalesEvent, SalesState> with Logging {
) async* {
try {
if (event is SalesLoad) {
log(" -- start SalesLoad");
yield SalesLoading();
log("Load Sales");
if (Cache().userLoggedIn == null) {
throw Exception("Please log in");
}
String descriptionName = "sales_page_text";
RemoteConfig? remoteConfig = Cache().remoteConfig;
if (remoteConfig != null) {
remoteConfig.fetchAndActivate();
Map config = remoteConfig.getAll();
RemoteConfigValue? value = config['sales_page_text_a'];
if (value != null) {
log("RemoteConfig sales_page_text value: ${value.asString()}");
if (value.asString() == "1") {
descriptionName = "sales_page_text_a";
}
}
}
await RevenueCatPurchases().getOfferings();
this.getProductSet();
salesText = descriptionRepository.getDescriptionByName(descriptionName);
log(salesText!);
salesButtonText = descriptionRepository.getDescriptionByName("sales_button_monthly");
offeredProduct = getProductByName("wt_sub_2_3");
Track().track(TrackingEvent.sales_page);
init();
yield SalesReady();
log(" -- finish SalesLoad");
} else if (event is SalesPurchase) {
if (Cache().hasPurchased) {
throw Exception("You have already a successfull subscription");
@@ -104,28 +87,34 @@ class SalesBloc extends Bloc<SalesEvent, SalesState> with Logging {
} else {
yield SalesError(message: "No selected product");
}
} else if (event is SalesChangeSubscription) {
yield SalesLoading();
print("offered product .. $offeredProduct");
if (offeredProduct != null) {
if (offeredProduct!.type == "wt_sub_2_3") {
print("go yearly");
salesButtonText = descriptionRepository.getDescriptionByName("sales_button_yearly");
offeredProduct = getProductByName("wt_sub_2_1");
} else {
print("go monthly");
salesButtonText = descriptionRepository.getDescriptionByName("sales_button_monthly");
offeredProduct = getProductByName("wt_sub_2_3");
}
}
yield SalesReady();
}
} on Exception catch (ex) {
yield SalesError(message: ex.toString());
}
}
void getProductsTexts() {
Product product;
if (product2Display.isNotEmpty) {
String salesButtonText;
product2Display.forEach((element) {
product = element;
if (product.sort == 3) {
salesButtonText = descriptionRepository.getDescriptionByName("sales_button_monthly");
productText2Display[1] = salesButtonText.replaceFirst(RegExp(r'localizedPrice'), product.localizedPrice!);
} else if (product.sort == 1) {
salesButtonText = descriptionRepository.getDescriptionByName("sales_button_yearly");
productText2Display[0] = salesButtonText.replaceFirst(RegExp(r'localizedPrice'), product.localizedPrice!);
}
});
}
print("product Text $productText2Display");
splitTestRepository.getSplitTestValue("product_set_2");
return;
}
Product? getSelectedProduct(int productId) {
Product? prod;
for (var product in this.product2Display) {
@@ -161,38 +150,37 @@ class SalesBloc extends Bloc<SalesEvent, SalesState> with Logging {
}
void getProductSet() {
int productId = 0;
//this.tests = Cache().productTests;
List<Product>? products = Cache().products;
if (products == null) {
return;
}
/* if (tests != null && tests!.isEmpty) {
var rand = math.Random.secure();
productSet = rand.nextInt(5) + 1;
} else {
trace("Previous ProductTest: " + tests![0].toJson().toString());
productId = tests![0].productId;
for (var elem in products) {
final Product product = elem;
if (product.productId == productId) {
productSet = product.productSet;
break;
}
}
} */
String productSetString = splitTestRepository.getSplitTestValue("product_set_2");
log("ProductSetString: $productSetString");
try {
productSet = int.parse(productSetString);
} on Exception catch (e) {
log("Define the right productset!");
productSet = 2;
}
//ProductTest productTest = ProductTest();
log("ProductSet: $productSet");
productSet = 2;
log("ProductSet: " + productSet.toString());
for (var elem in products) {
Product product = elem;
if (product.productSet == productSet) {
productId = product.productId;
final String platformProductId = Platform.isAndroid ? product.productIdAndroid! : product.productIdIos!;
String? platformProductId;
if (product.productIdAndroid == null || product.productIdIos == null) {
log("Define the product ID for the different Platforms!!");
} else {
platformProductId = Platform.isAndroid ? product.productIdAndroid! : product.productIdIos!;
}
if (platformProductId == null) {
log("Not defined platform product id!!");
platformProductId = "";
}
product.localizedPrice = getLocalizedPrice(platformProductId, product);
log("product with localized price: $product");
product2Display.add(product);
@@ -203,10 +191,6 @@ class SalesBloc extends Bloc<SalesEvent, SalesState> with Logging {
return a.sort < b.sort ? -1 : 1;
});
//productTest.productId = productId;
//productTest.customerId = Cache().userLoggedIn!.customerId!;
//productTest.dateView = DateTime.now();
//ProductTestApi().saveProductTest(productTest);
//Cache().productTests.add(productTest);
this.getProductsTexts();
}
}