From f5ab25205adc0d37207b440a0b236c6e3f65bd67 Mon Sep 17 00:00:00 2001 From: Shahroz Khan Date: Wed, 6 Nov 2024 03:39:35 +0500 Subject: [PATCH] sdk config updated for sample app --- .github/workflows/build-sample-apps.yml | 2 +- apps/amiapp_flutter/.env.example | 2 +- apps/amiapp_flutter/lib/src/customer_io.dart | 18 +- apps/amiapp_flutter/lib/src/data/config.dart | 161 +++++++++++------- .../lib/src/screens/settings.dart | 60 +++---- 5 files changed, 136 insertions(+), 107 deletions(-) diff --git a/.github/workflows/build-sample-apps.yml b/.github/workflows/build-sample-apps.yml index ef247da..f10e08b 100644 --- a/.github/workflows/build-sample-apps.yml +++ b/.github/workflows/build-sample-apps.yml @@ -112,7 +112,7 @@ jobs: run: | cp ".env.example" ".env" sd 'SITE_ID=.*' "SITE_ID=${{ secrets[format('CUSTOMERIO_{0}_WORKSPACE_SITE_ID', matrix.sample-app)] }}" ".env" - sd 'API_KEY=.*' "API_KEY=${{ secrets[format('CUSTOMERIO_{0}_WORKSPACE_API_KEY', matrix.sample-app)] }}" ".env" + sd 'CDP_API_KEY=.*' "CDP_API_KEY=${{ secrets[format('CUSTOMERIO_{0}_WORKSPACE_CDP_API_KEY', matrix.sample-app)] }}" ".env" - name: Setup workspace credentials in iOS environment files run: | diff --git a/apps/amiapp_flutter/.env.example b/apps/amiapp_flutter/.env.example index 0bbd9a5..6678d50 100644 --- a/apps/amiapp_flutter/.env.example +++ b/apps/amiapp_flutter/.env.example @@ -1,2 +1,2 @@ SITE_ID=siteid -API_KEY=apikey +CDP_API_KEY=cdpapikey diff --git a/apps/amiapp_flutter/lib/src/customer_io.dart b/apps/amiapp_flutter/lib/src/customer_io.dart index 4605a70..313854a 100644 --- a/apps/amiapp_flutter/lib/src/customer_io.dart +++ b/apps/amiapp_flutter/lib/src/customer_io.dart @@ -55,23 +55,23 @@ class CustomerIOSDK extends ChangeNotifier { } final InAppConfig? inAppConfig; - if (_sdkConfig?.siteId != null) { - inAppConfig = InAppConfig(siteId: _sdkConfig!.siteId); + if (_sdkConfig?.migrationSiteId != null) { + inAppConfig = InAppConfig(siteId: _sdkConfig!.migrationSiteId ?? ''); } else { inAppConfig = null; } return CustomerIO.initialize( config: CustomerIOConfig( - cdpApiKey: '${_sdkConfig?.siteId}:${_sdkConfig?.apiKey}', - migrationSiteId: _sdkConfig?.siteId, + cdpApiKey: '${_sdkConfig?.cdnHost}:${_sdkConfig?.cdpApiKey}', + migrationSiteId: _sdkConfig?.migrationSiteId, region: Region.us, logLevel: logLevel, autoTrackDeviceAttributes: - _sdkConfig?.deviceAttributesTrackingEnabled, - apiHost: _sdkConfig?.trackingUrl, - cdnHost: _sdkConfig?.trackingUrl, - flushAt: _sdkConfig?.backgroundQueueMinNumOfTasks, - flushInterval: _sdkConfig?.backgroundQueueSecondsDelay?.toInt(), + _sdkConfig?.autoTrackDeviceAttributes, + apiHost: _sdkConfig?.apiHost, + cdnHost: _sdkConfig?.cdnHost, + flushAt: _sdkConfig?.flushAt, + flushInterval: _sdkConfig?.flushInterval?.toInt(), inAppConfig: inAppConfig, ), ); diff --git a/apps/amiapp_flutter/lib/src/data/config.dart b/apps/amiapp_flutter/lib/src/data/config.dart index 3c45552..c162d60 100644 --- a/apps/amiapp_flutter/lib/src/data/config.dart +++ b/apps/amiapp_flutter/lib/src/data/config.dart @@ -1,58 +1,87 @@ +import 'package:customer_io/config/in_app_config.dart'; +import 'package:customer_io/config/push_config.dart'; +import 'package:customer_io/customer_io_enums.dart'; import 'package:flutter_dotenv/flutter_dotenv.dart'; import 'package:shared_preferences/shared_preferences.dart'; class CustomerIOSDKConfig { - String siteId; - String apiKey; - String? trackingUrl; - double? backgroundQueueSecondsDelay; - int? backgroundQueueMinNumOfTasks; - bool screenTrackingEnabled; - bool deviceAttributesTrackingEnabled; - bool debugModeEnabled; + final String cdpApiKey; + final String? migrationSiteId; + final Region? region; + final bool? debugModeEnabled; + final bool? screenTrackingEnabled; + final bool? autoTrackDeviceAttributes; + final String? apiHost; + final String? cdnHost; + final int? flushAt; + final int? flushInterval; + final InAppConfig? inAppConfig; + final PushConfig pushConfig; CustomerIOSDKConfig({ - required this.siteId, - required this.apiKey, - this.trackingUrl = "https://track-sdk.customer.io/", - this.backgroundQueueSecondsDelay = 30.0, - this.backgroundQueueMinNumOfTasks = 10, - this.screenTrackingEnabled = true, - this.deviceAttributesTrackingEnabled = true, - this.debugModeEnabled = true, - }); + required this.cdpApiKey, + this.migrationSiteId, + this.region, + this.debugModeEnabled, + this.screenTrackingEnabled, + this.autoTrackDeviceAttributes, + this.apiHost, + this.cdnHost, + this.flushAt, + this.flushInterval, + this.inAppConfig, + PushConfig? pushConfig, + }) : pushConfig = pushConfig ?? PushConfig(); - factory CustomerIOSDKConfig.fromEnv() => CustomerIOSDKConfig( - siteId: dotenv.env[_PreferencesKey.siteId]!, - apiKey: dotenv.env[_PreferencesKey.apiKey]!); + factory CustomerIOSDKConfig.fromEnv() => + CustomerIOSDKConfig( + cdpApiKey: dotenv.env[_PreferencesKey.cdpApiKey]!, + migrationSiteId: dotenv.env[_PreferencesKey.migrationSiteId], + ); factory CustomerIOSDKConfig.fromPrefs(SharedPreferences prefs) { - final siteId = prefs.getString(_PreferencesKey.siteId); - final apiKey = prefs.getString(_PreferencesKey.apiKey); + final cdpApiKey = prefs.getString(_PreferencesKey.cdpApiKey); - if (siteId == null) { - throw ArgumentError('siteId cannot be null'); - } else if (apiKey == null) { - throw ArgumentError('apiKey cannot be null'); + if (cdpApiKey == null) { + throw ArgumentError('cdpApiKey cannot be null'); } return CustomerIOSDKConfig( - siteId: siteId, - apiKey: apiKey, - trackingUrl: prefs.getString(_PreferencesKey.trackingUrl), - backgroundQueueSecondsDelay: - prefs.getDouble(_PreferencesKey.backgroundQueueSecondsDelay), - backgroundQueueMinNumOfTasks: - prefs.getInt(_PreferencesKey.backgroundQueueMinNumOfTasks), - screenTrackingEnabled: - prefs.getBool(_PreferencesKey.screenTrackingEnabled) != false, - deviceAttributesTrackingEnabled: - prefs.getBool(_PreferencesKey.deviceAttributesTrackingEnabled) != - false, - debugModeEnabled: - prefs.getBool(_PreferencesKey.debugModeEnabled) != false, + cdpApiKey: cdpApiKey, + migrationSiteId: prefs.getString(_PreferencesKey.migrationSiteId), + region: prefs.getString(_PreferencesKey.region) != null + ? Region.values.firstWhere( + (e) => e.name == prefs.getString(_PreferencesKey.region)) + : null, + debugModeEnabled: prefs.getBool(_PreferencesKey.debugModeEnabled) != + false, + screenTrackingEnabled: prefs.getBool( + _PreferencesKey.screenTrackingEnabled) != false, + autoTrackDeviceAttributes: + prefs.getBool(_PreferencesKey.autoTrackDeviceAttributes), + apiHost: prefs.getString(_PreferencesKey.apiHost), + cdnHost: prefs.getString(_PreferencesKey.cdnHost), + flushAt: prefs.getInt(_PreferencesKey.flushAt), + flushInterval: prefs.getInt(_PreferencesKey.flushInterval), ); } + + Map toMap() { + return { + 'cdpApiKey': cdpApiKey, + 'migrationSiteId': migrationSiteId, + 'region': region?.name, + 'logLevel': debugModeEnabled, + 'screenTrackingEnabled': screenTrackingEnabled, + 'autoTrackDeviceAttributes': autoTrackDeviceAttributes, + 'apiHost': apiHost, + 'cdnHost': cdnHost, + 'flushAt': flushAt, + 'flushInterval': flushInterval, + 'inAppConfig': inAppConfig?.toMap(), + 'pushConfig': pushConfig.toMap(), + }; + } } extension ConfigurationPreferencesExtensions on SharedPreferences { @@ -66,10 +95,6 @@ extension ConfigurationPreferencesExtensions on SharedPreferences { return value != null ? setInt(key, value) : remove(key); } - Future setOrRemoveDouble(String key, double? value) { - return value != null ? setDouble(key, value) : remove(key); - } - Future setOrRemoveBool(String key, bool? value) { return value != null ? setBool(key, value) : remove(key); } @@ -77,39 +102,43 @@ extension ConfigurationPreferencesExtensions on SharedPreferences { Future saveSDKConfigState(CustomerIOSDKConfig config) async { bool result = true; result = result && - await setOrRemoveString(_PreferencesKey.siteId, config.siteId); - result = result && - await setOrRemoveString(_PreferencesKey.apiKey, config.apiKey); + await setOrRemoveString(_PreferencesKey.cdpApiKey, config.cdpApiKey); result = result && await setOrRemoveString( - _PreferencesKey.trackingUrl, config.trackingUrl); - result = result && - await setOrRemoveDouble(_PreferencesKey.backgroundQueueSecondsDelay, - config.backgroundQueueSecondsDelay); + _PreferencesKey.migrationSiteId, config.migrationSiteId); result = result && - await setOrRemoveInt(_PreferencesKey.backgroundQueueMinNumOfTasks, - config.backgroundQueueMinNumOfTasks); + await setOrRemoveString(_PreferencesKey.region, config.region?.name); result = result && - await setOrRemoveBool(_PreferencesKey.screenTrackingEnabled, - config.screenTrackingEnabled); + await setOrRemoveBool( + _PreferencesKey.debugModeEnabled, config.debugModeEnabled); result = result && - await setOrRemoveBool(_PreferencesKey.deviceAttributesTrackingEnabled, - config.deviceAttributesTrackingEnabled); + await setOrRemoveBool(_PreferencesKey.autoTrackDeviceAttributes, + config.autoTrackDeviceAttributes); result = result && await setOrRemoveBool( - _PreferencesKey.debugModeEnabled, config.debugModeEnabled); + _PreferencesKey.screenTrackingEnabled, config.screenTrackingEnabled); + result = result && + await setOrRemoveString(_PreferencesKey.apiHost, config.apiHost); + result = result && + await setOrRemoveString(_PreferencesKey.cdnHost, config.cdnHost); + result = + result && await setOrRemoveInt(_PreferencesKey.flushAt, config.flushAt); + result = result && + await setOrRemoveInt( + _PreferencesKey.flushInterval, config.flushInterval); return result; } } class _PreferencesKey { - static const siteId = 'SITE_ID'; - static const apiKey = 'API_KEY'; - static const trackingUrl = 'TRACKING_URL'; - static const backgroundQueueSecondsDelay = 'BACKGROUND_QUEUE_SECONDS_DELAY'; - static const backgroundQueueMinNumOfTasks = - 'BACKGROUND_QUEUE_MIN_NUMBER_OF_TASKS'; - static const screenTrackingEnabled = 'TRACK_SCREENS'; - static const deviceAttributesTrackingEnabled = 'TRACK_DEVICE_ATTRIBUTES'; + static const cdpApiKey = 'CDP_API_KEY'; + static const migrationSiteId = 'SITE_ID'; + static const region = 'REGION'; static const debugModeEnabled = 'DEBUG_MODE'; + static const screenTrackingEnabled = 'SCREEN_TRACKING'; + static const autoTrackDeviceAttributes = 'AUTO_TRACK_DEVICE_ATTRIBUTES'; + static const apiHost = 'API_HOST'; + static const cdnHost = 'CDN_HOST'; + static const flushAt = 'FLUSH_AT'; + static const flushInterval = 'FLUSH_INTERVAL'; } diff --git a/apps/amiapp_flutter/lib/src/screens/settings.dart b/apps/amiapp_flutter/lib/src/screens/settings.dart index 5899d82..30aab80 100644 --- a/apps/amiapp_flutter/lib/src/screens/settings.dart +++ b/apps/amiapp_flutter/lib/src/screens/settings.dart @@ -52,19 +52,19 @@ class _SettingsScreenState extends State { final cioConfig = widget._customerIOSDK.sdkConfig; _deviceTokenValueController = TextEditingController(); - _trackingURLValueController = - TextEditingController(text: cioConfig?.trackingUrl); + // _trackingURLValueController = + // TextEditingController(text: cioConfig?.trackingUrl); _siteIDValueController = TextEditingController( - text: widget.siteIdInitialValue ?? cioConfig?.siteId); + text: widget.siteIdInitialValue ?? cioConfig?.migrationSiteId); _apiKeyValueController = TextEditingController( - text: widget.apiKeyInitialValue ?? cioConfig?.apiKey); - _bqSecondsDelayValueController = TextEditingController( - text: cioConfig?.backgroundQueueSecondsDelay?.toTrimmedString()); - _bqMinNumberOfTasksValueController = TextEditingController( - text: cioConfig?.backgroundQueueMinNumOfTasks?.toString()); - _featureTrackScreens = cioConfig?.screenTrackingEnabled ?? true; - _featureTrackDeviceAttributes = - cioConfig?.deviceAttributesTrackingEnabled ?? true; + text: widget.apiKeyInitialValue ?? cioConfig?.cdpApiKey); + // _bqSecondsDelayValueController = TextEditingController( + // text: cioConfig?.backgroundQueueSecondsDelay?.toTrimmedString()); + // _bqMinNumberOfTasksValueController = TextEditingController( + // text: cioConfig?.backgroundQueueMinNumOfTasks?.toString()); + // _featureTrackScreens = cioConfig?.screenTrackingEnabled ?? true; + // _featureTrackDeviceAttributes = + // cioConfig?.deviceAttributesTrackingEnabled ?? true; _featureDebugMode = cioConfig?.debugModeEnabled ?? true; super.initState(); @@ -76,15 +76,15 @@ class _SettingsScreenState extends State { } final newConfig = CustomerIOSDKConfig( - siteId: _siteIDValueController.text.trim(), - apiKey: _apiKeyValueController.text.trim(), - trackingUrl: _trackingURLValueController.text.trim(), - backgroundQueueSecondsDelay: - _bqSecondsDelayValueController.text.trim().toDoubleOrNull(), - backgroundQueueMinNumOfTasks: - _bqMinNumberOfTasksValueController.text.trim().toIntOrNull(), + migrationSiteId: _siteIDValueController.text.trim(), + cdpApiKey: _apiKeyValueController.text.trim(), + // trackingUrl: _trackingURLValueController.text.trim(), + // backgroundQueueSecondsDelay: + // _bqSecondsDelayValueController.text.trim().toDoubleOrNull(), + // backgroundQueueMinNumOfTasks: + // _bqMinNumberOfTasksValueController.text.trim().toIntOrNull(), screenTrackingEnabled: _featureTrackScreens, - deviceAttributesTrackingEnabled: _featureTrackDeviceAttributes, + // deviceAttributesTrackingEnabled: _featureTrackDeviceAttributes, debugModeEnabled: _featureDebugMode, ); widget._customerIOSDK.saveConfigToPreferences(newConfig).then((success) { @@ -109,17 +109,17 @@ class _SettingsScreenState extends State { } setState(() { - _siteIDValueController.text = defaultConfig.siteId; - _apiKeyValueController.text = defaultConfig.apiKey; - _trackingURLValueController.text = defaultConfig.trackingUrl ?? ''; - _bqSecondsDelayValueController.text = - defaultConfig.backgroundQueueSecondsDelay?.toTrimmedString() ?? ''; - _bqMinNumberOfTasksValueController.text = - defaultConfig.backgroundQueueMinNumOfTasks?.toString() ?? ''; - _featureTrackScreens = defaultConfig.screenTrackingEnabled; - _featureTrackDeviceAttributes = - defaultConfig.deviceAttributesTrackingEnabled; - _featureDebugMode = defaultConfig.debugModeEnabled; + _siteIDValueController.text = defaultConfig.migrationSiteId ?? ''; + _apiKeyValueController.text = defaultConfig.cdpApiKey; + // _trackingURLValueController.text = defaultConfig.trackingUrl ?? ''; + // _bqSecondsDelayValueController.text = + // defaultConfig.backgroundQueueSecondsDelay?.toTrimmedString() ?? ''; + // _bqMinNumberOfTasksValueController.text = + // defaultConfig.backgroundQueueMinNumOfTasks?.toString() ?? ''; + // _featureTrackScreens = defaultConfig.screenTrackingEnabled; + // _featureTrackDeviceAttributes = + // defaultConfig.deviceAttributesTrackingEnabled; + _featureDebugMode = defaultConfig.debugModeEnabled ?? true; _saveSettings(context); }); }