diff --git a/README.md b/README.md index ff49ceb..efe1999 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,12 @@ import AppMetrica from 'react-native-appmetrica'; // Starts the statistics collection process. AppMetrica.activateWithApiKey('...KEY...'); +// OR +AppMetrica.activateWithConfig({ + apiKey: '...KEY...', + sessionTimeout: 120, + firstActivationAsUpdate: true, +}); // Sends a custom event message and additional parameters (optional). AppMetrica.reportEvent('My event'); diff --git a/android/src/main/java/com/doochik/RNAppMetrica/AppMetricaModule.java b/android/src/main/java/com/doochik/RNAppMetrica/AppMetricaModule.java index 9fa9d64..2fc19c6 100644 --- a/android/src/main/java/com/doochik/RNAppMetrica/AppMetricaModule.java +++ b/android/src/main/java/com/doochik/RNAppMetrica/AppMetricaModule.java @@ -42,6 +42,23 @@ public class AppMetricaModule extends ReactContextBaseJavaModule { } } + @ReactMethod + public void activateWithConfig(ReadableMap params) { + YandexMetricaConfig.Builder configBuilder = YandexMetricaConfig.newConfigBuilder(params.getString("apiKey")); + if (params.hasKey("sessionTimeout")) { + configBuilder.withSessionTimeout(params.getInt("sessionTimeout")); + } + if (params.hasKey("firstActivationAsUpdate")) { + configBuilder.handleFirstActivationAsUpdate(params.getBoolean("firstActivationAsUpdate")); + } + YandexMetrica.activate(getReactApplicationContext().getApplicationContext(), configBuilder.build()); + Activity activity = getCurrentActivity(); + if (activity != null) { + Application application = activity.getApplication(); + YandexMetrica.enableActivityAutoTracking(application); + } + } + @ReactMethod public void reportError(String message) { try { diff --git a/index.js b/index.js index 96d1b8b..3b08d00 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,12 @@ import { NativeModules } from 'react-native'; const { AppMetrica } = NativeModules; +type ActivationConfig = { + apiKey: string, + sessionTimeout?: number, + firstActivationAsUpdate?: boolean, +}; + export default { /** @@ -13,6 +19,14 @@ export default { AppMetrica.activateWithApiKey(apiKey); }, + /** + * Starts the statistics collection process using config. + * @param {object} params + */ + activateWithConfig(params: ActivationConfig) { + AppMetrica.activateWithConfig(params); + }, + /** * Sends a custom event message and additional parameters (optional). * @param {string} message diff --git a/ios/RCTAppMetrica/RCTAppMetrica/RCTAppMetrica.m b/ios/RCTAppMetrica/RCTAppMetrica/RCTAppMetrica.m index 74f68e8..208651c 100644 --- a/ios/RCTAppMetrica/RCTAppMetrica/RCTAppMetrica.m +++ b/ios/RCTAppMetrica/RCTAppMetrica/RCTAppMetrica.m @@ -2,7 +2,7 @@ #import @implementation RCTAppMetrica { - + } RCT_EXPORT_MODULE(); @@ -13,6 +13,17 @@ RCT_EXPORT_METHOD(activateWithApiKey:(NSString *)apiKey) [YMMYandexMetrica activateWithConfiguration:configuration]; } +RCT_EXPORT_METHOD(activateWithConfig:(NSDictionary *)config) { + YMMYandexMetricaConfiguration *configuration = [[YMMYandexMetricaConfiguration alloc] initWithApiKey:config[@"apiKey"]]; + if (config[@"sessionTimeout"] != (id)[NSNull null]) { + [configuration setSessionTimeout:[config[@"sessionTimeout"] intValue]]; + } + if (config[@"firstActivationAsUpdate"] != (id)[NSNull null]) { + [configuration setHandleFirstActivationAsUpdate:[config[@"firstActivationAsUpdate"] boolValue]]; + } + [YMMYandexMetrica activateWithConfiguration:configuration]; +} + RCT_EXPORT_METHOD(reportEvent:(NSString *)message) { [YMMYandexMetrica reportEvent:message onFailure:NULL];