2017-11-23 17:29:40 +00:00
|
|
|
/**
|
|
|
|
* @flow
|
|
|
|
* Analytics representation wrapper
|
|
|
|
*/
|
2018-01-05 17:20:02 +00:00
|
|
|
import ModuleBase from '../../utils/ModuleBase';
|
|
|
|
import { getNativeModule } from '../../utils/native';
|
2017-02-14 11:36:52 +00:00
|
|
|
|
2018-01-05 17:20:02 +00:00
|
|
|
import type App from '../core/firebase-app';
|
2017-11-28 14:19:49 +00:00
|
|
|
|
2017-02-14 11:36:52 +00:00
|
|
|
const AlphaNumericUnderscore = /^[a-zA-Z0-9_]+$/;
|
|
|
|
|
|
|
|
const ReservedEventNames = [
|
|
|
|
'app_clear_data',
|
|
|
|
'app_uninstall',
|
|
|
|
'app_update',
|
|
|
|
'error',
|
|
|
|
'first_open',
|
|
|
|
'in_app_purchase',
|
|
|
|
'notification_dismiss',
|
|
|
|
'notification_foreground',
|
|
|
|
'notification_open',
|
|
|
|
'notification_receive',
|
|
|
|
'os_update',
|
|
|
|
'session_start',
|
|
|
|
'user_engagement',
|
|
|
|
];
|
|
|
|
|
2018-01-03 20:00:38 +00:00
|
|
|
export const MODULE_NAME = 'RNFirebaseAnalytics';
|
|
|
|
export const NAMESPACE = 'analytics';
|
2017-08-17 16:58:28 +00:00
|
|
|
|
2018-01-03 20:00:38 +00:00
|
|
|
export default class Analytics extends ModuleBase {
|
2018-01-05 17:20:02 +00:00
|
|
|
constructor(app: App) {
|
|
|
|
super(app, {
|
2018-01-03 20:00:38 +00:00
|
|
|
moduleName: MODULE_NAME,
|
|
|
|
namespace: NAMESPACE,
|
|
|
|
});
|
2017-05-25 22:39:06 +00:00
|
|
|
}
|
|
|
|
|
2017-02-14 11:36:52 +00:00
|
|
|
/**
|
|
|
|
* Logs an app event.
|
|
|
|
* @param {string} name
|
|
|
|
* @param params
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
2017-03-10 14:43:09 +00:00
|
|
|
logEvent(name: string, params: Object = {}): void {
|
2017-02-14 11:36:52 +00:00
|
|
|
// check name is not a reserved event name
|
|
|
|
if (ReservedEventNames.includes(name)) {
|
|
|
|
throw new Error(`event name '${name}' is a reserved event name and can not be used.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
// name format validation
|
|
|
|
if (!AlphaNumericUnderscore.test(name)) {
|
|
|
|
throw new Error(`Event name '${name}' is invalid. Names should contain 1 to 32 alphanumeric characters or underscores.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
// maximum number of allowed params check
|
|
|
|
if (params && Object.keys(params).length > 25) throw new Error('Maximum number of parameters exceeded (25).');
|
|
|
|
|
|
|
|
// Parameter names can be up to 24 characters long and must start with an alphabetic character
|
|
|
|
// and contain only alphanumeric characters and underscores. Only String, long and double param
|
|
|
|
// types are supported. String parameter values can be up to 36 characters long. The "firebase_"
|
|
|
|
// prefix is reserved and should not be used for parameter names.
|
|
|
|
|
2018-01-05 17:20:02 +00:00
|
|
|
getNativeModule(this).logEvent(name, params);
|
2017-02-14 11:36:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets whether analytics collection is enabled for this app on this device.
|
|
|
|
* @param enabled
|
|
|
|
*/
|
|
|
|
setAnalyticsCollectionEnabled(enabled: boolean): void {
|
2018-01-05 17:20:02 +00:00
|
|
|
getNativeModule(this).setAnalyticsCollectionEnabled(enabled);
|
2017-02-14 11:36:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the current screen name, which specifies the current visual context in your app.
|
|
|
|
* @param screenName
|
|
|
|
* @param screenClassOverride
|
|
|
|
*/
|
|
|
|
setCurrentScreen(screenName: string, screenClassOverride: string): void {
|
2018-01-05 17:20:02 +00:00
|
|
|
getNativeModule(this).setCurrentScreen(screenName, screenClassOverride);
|
2017-02-14 11:36:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the minimum engagement time required before starting a session. The default value is 10000 (10 seconds).
|
|
|
|
* @param milliseconds
|
|
|
|
*/
|
|
|
|
setMinimumSessionDuration(milliseconds: number = 10000): void {
|
2018-01-05 17:20:02 +00:00
|
|
|
getNativeModule(this).setMinimumSessionDuration(milliseconds);
|
2017-02-14 11:36:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the duration of inactivity that terminates the current session. The default value is 1800000 (30 minutes).
|
|
|
|
* @param milliseconds
|
|
|
|
*/
|
|
|
|
setSessionTimeoutDuration(milliseconds: number = 1800000): void {
|
2018-01-05 17:20:02 +00:00
|
|
|
getNativeModule(this).setSessionTimeoutDuration(milliseconds);
|
2017-02-14 11:36:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the user ID property.
|
|
|
|
* @param id
|
|
|
|
*/
|
|
|
|
setUserId(id: string): void {
|
2018-01-05 17:20:02 +00:00
|
|
|
getNativeModule(this).setUserId(id);
|
2017-02-14 11:36:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a user property to a given value.
|
|
|
|
* @param name
|
|
|
|
* @param value
|
|
|
|
*/
|
|
|
|
setUserProperty(name: string, value: string): void {
|
2018-01-05 17:20:02 +00:00
|
|
|
getNativeModule(this).setUserProperty(name, value);
|
2017-02-14 11:36:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a user property to a given value.
|
2017-06-30 16:23:32 +00:00
|
|
|
* @RNFirebaseSpecific
|
2017-02-14 11:36:52 +00:00
|
|
|
* @param object
|
|
|
|
*/
|
|
|
|
setUserProperties(object: Object): void {
|
|
|
|
for (const property of Object.keys(object)) {
|
2018-01-05 17:20:02 +00:00
|
|
|
getNativeModule(this).setUserProperty(property, object[property]);
|
2017-02-14 11:36:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-23 17:29:40 +00:00
|
|
|
|
|
|
|
export const statics = {};
|