react-native-appmetrica/index.js

85 lines
2.2 KiB
JavaScript
Raw Normal View History

2017-09-30 15:41:58 +03:00
// @flow
2017-09-18 10:04:19 +03:00
import { NativeModules } from 'react-native';
const { AppMetrica } = NativeModules;
2018-08-30 12:13:18 +03:00
type ActivationConfig = {
apiKey: string,
sessionTimeout?: number,
firstActivationAsUpdate?: boolean,
};
2018-09-12 17:56:09 +03:00
type UserProfileAttributes = {
name?: ?string,
gender?: 'female' | 'male' | string | void,
age?: ?number,
birthDate?: Date | [number] | [number, number] | [number, number, number] | void,
notificationsEnabled?: boolean,
[string]: string | number | boolean,
};
2017-09-18 10:04:19 +03:00
export default {
/**
* Starts the statistics collection process.
* @param {string} apiKey
*/
2017-09-30 15:41:58 +03:00
activateWithApiKey(apiKey: string) {
2017-09-18 10:04:19 +03:00
AppMetrica.activateWithApiKey(apiKey);
},
2018-08-30 12:13:18 +03:00
/**
* Starts the statistics collection process using config.
* @param {object} params
*/
activateWithConfig(params: ActivationConfig) {
AppMetrica.activateWithConfig(params);
},
2017-09-18 10:04:19 +03:00
/**
* Sends a custom event message and additional parameters (optional).
* @param {string} message
* @param {object} [params=null]
*/
2017-09-30 15:41:58 +03:00
reportEvent(message: string, params: ?Object = null) {
2017-09-18 10:04:19 +03:00
AppMetrica.reportEvent(message, params);
},
2017-09-28 15:06:30 +03:00
/**
* Sends error with reason.
* @param {string} error
* @param {object} reason
*/
2017-09-30 15:41:58 +03:00
reportError(error: string, reason: Object) {
2017-09-28 15:06:30 +03:00
AppMetrica.reportError(error, reason);
},
2018-06-01 18:38:01 +03:00
/**
* Sets the ID of the user profile.
* @param {string} userProfileId
*/
setUserProfileID(userProfileId: string) {
AppMetrica.setUserProfileID(userProfileId);
},
2018-09-12 17:56:09 +03:00
/**
* Sets attributes of the user profile.
* @param {object} attributes
*/
reportUserProfile(attributes: UserProfileAttributes) {
const readyAttributes = {};
Object.keys(attributes).forEach(key => {
if (
key === 'birthDate' &&
typeof attributes.birthDate === 'object' &&
typeof attributes.birthDate.getTime === 'function'
) {
readyAttributes.birthDate = attributes.birthDate.getTime();
} else {
readyAttributes[key] = attributes[key];
}
});
AppMetrica.reportUserProfile(readyAttributes);
},
2017-09-18 10:04:19 +03:00
};