all files / firebase/modules/analytics/ index.js

80% Statements 20/25
70.59% Branches 12/17
80% Functions 8/10
80% Lines 20/25
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151                                                                                                                                                                                                                                                                     
/**
 * @flow
 * Analytics representation wrapper
 */
import ModuleBase from '../../utils/ModuleBase';
import { getNativeModule } from '../../utils/native';
import { isString, isObject } from '../../utils';
 
import type App from '../core/app';
 
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',
];
 
export const MODULE_NAME = 'RNFirebaseAnalytics';
export const NAMESPACE = 'analytics';
 
export default class Analytics extends ModuleBase {
  constructor(app: App) {
    super(app, {
      moduleName: MODULE_NAME,
      multiApp: false,
      namespace: NAMESPACE,
    });
  }
 
  /**
   * Logs an app event.
   * @param  {string} name
   * @param params
   * @return {Promise}
   */
  logEvent(name: string, params: Object = {}): void {
    if (!isString(name)) {
      throw new Error(
        `analytics.logEvent(): First argument 'name' is required and must be a string value.`
      );
    }
 
    if (typeof params !== 'undefined' && !isObject(params)) {
      throw new Error(
        `analytics.logEvent(): Second optional argument 'params' must be an object if provided.`
      );
    }
 
    // check name is not a reserved event name
    Iif (ReservedEventNames.includes(name)) {
      throw new Error(
        `analytics.logEvent(): event name '${name}' is a reserved event name and can not be used.`
      );
    }
 
    // name format validation
    Iif (!AlphaNumericUnderscore.test(name)) {
      throw new Error(
        `analytics.logEvent(): Event name '${name}' is invalid. Names should contain 1 to 32 alphanumeric characters or underscores.`
      );
    }
 
    // maximum number of allowed params check
    Iif (params && Object.keys(params).length > 25)
      throw new Error(
        'analytics.logEvent(): 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.
 
    getNativeModule(this).logEvent(name, params);
  }
 
  /**
   * Sets whether analytics collection is enabled for this app on this device.
   * @param enabled
   */
  setAnalyticsCollectionEnabled(enabled: boolean): void {
    getNativeModule(this).setAnalyticsCollectionEnabled(enabled);
  }
 
  /**
   * Sets the current screen name, which specifies the current visual context in your app.
   * @param screenName
   * @param screenClassOverride
   */
  setCurrentScreen(screenName: string, screenClassOverride: string): void {
    getNativeModule(this).setCurrentScreen(screenName, screenClassOverride);
  }
 
  /**
   * Sets the minimum engagement time required before starting a session. The default value is 10000 (10 seconds).
   * @param milliseconds
   */
  setMinimumSessionDuration(milliseconds: number = 10000): void {
    getNativeModule(this).setMinimumSessionDuration(milliseconds);
  }
 
  /**
   * Sets the duration of inactivity that terminates the current session. The default value is 1800000 (30 minutes).
   * @param milliseconds
   */
  setSessionTimeoutDuration(milliseconds: number = 1800000): void {
    getNativeModule(this).setSessionTimeoutDuration(milliseconds);
  }
 
  /**
   * Sets the user ID property.
   * @param id
   */
  setUserId(id: string): void {
    getNativeModule(this).setUserId(id);
  }
 
  /**
   * Sets a user property to a given value.
   * @param name
   * @param value
   */
  setUserProperty(name: string, value: string): void {
    getNativeModule(this).setUserProperty(name, value);
  }
 
  /**
   * Sets a user property to a given value.
   * @RNFirebaseSpecific
   * @param object
   */
  setUserProperties(object: Object): void {
    Object.keys(object).forEach(property => {
      getNativeModule(this).setUserProperty(property, object[property]);
    });
  }
}
 
export const statics = {};