mirror of https://github.com/jitsi/jitsi-meet
The amplitude-js library gained React Native support so there is no need to keep separate implementations.pull/8297/head jitsi-meet_5367
parent
5ecb5717c7
commit
687a6c31ee
@ -1,122 +0,0 @@ |
||||
/* |
||||
* Copyright @ 2019-present 8x8, Inc. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.jitsi.meet.sdk; |
||||
|
||||
import android.annotation.SuppressLint; |
||||
import android.content.Context; |
||||
import android.content.SharedPreferences; |
||||
import android.provider.Settings; |
||||
import android.text.TextUtils; |
||||
|
||||
import com.facebook.react.bridge.ReactApplicationContext; |
||||
import com.facebook.react.bridge.ReactContextBaseJavaModule; |
||||
import com.facebook.react.bridge.ReactMethod; |
||||
import com.facebook.react.bridge.ReadableMap; |
||||
|
||||
import com.amplitude.api.Amplitude; |
||||
import com.facebook.react.module.annotations.ReactModule; |
||||
|
||||
import org.jitsi.meet.sdk.log.JitsiMeetLogger; |
||||
import org.json.JSONException; |
||||
import org.json.JSONObject; |
||||
|
||||
/** |
||||
* Implements the react-native module for the Amplitude integration. |
||||
*/ |
||||
@ReactModule(name = AmplitudeModule.NAME) |
||||
class AmplitudeModule |
||||
extends ReactContextBaseJavaModule { |
||||
|
||||
public static final String NAME = "Amplitude"; |
||||
public static final String JITSI_PREFERENCES = "jitsi-preferences"; |
||||
public static final String AMPLITUDE_DEVICE_ID_KEY = "amplitudeDeviceId"; |
||||
|
||||
public AmplitudeModule(ReactApplicationContext reactContext) { |
||||
super(reactContext); |
||||
} |
||||
|
||||
/** |
||||
* Initializes the Amplitude SDK. |
||||
* |
||||
* @param instanceName The name of the Amplitude instance. Should |
||||
* be used only for multi-project logging. |
||||
* @param apiKey The API_KEY of the Amplitude project. |
||||
*/ |
||||
@ReactMethod |
||||
@SuppressLint("HardwareIds") |
||||
public void init(String instanceName, String apiKey) { |
||||
Amplitude.getInstance(instanceName).initialize(getCurrentActivity(), apiKey); |
||||
|
||||
// Set the device ID to something consistent.
|
||||
SharedPreferences sharedPreferences = getReactApplicationContext().getSharedPreferences(JITSI_PREFERENCES, Context.MODE_PRIVATE); |
||||
String android_id = sharedPreferences.getString(AMPLITUDE_DEVICE_ID_KEY, ""); |
||||
if (!TextUtils.isEmpty(android_id)) { |
||||
Amplitude.getInstance(instanceName).setDeviceId(android_id); |
||||
} else { |
||||
String amplitudeId = Amplitude.getInstance(instanceName).getDeviceId(); |
||||
SharedPreferences.Editor editor = sharedPreferences.edit(); |
||||
editor.putString(JITSI_PREFERENCES, amplitudeId).apply(); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Sets the user ID for an Amplitude instance. |
||||
* |
||||
* @param instanceName The name of the Amplitude instance. |
||||
* @param userId The new value for the user ID. |
||||
*/ |
||||
@ReactMethod |
||||
public void setUserId(String instanceName, String userId) { |
||||
Amplitude.getInstance(instanceName).setUserId(userId); |
||||
} |
||||
|
||||
/** |
||||
* Sets the user properties for an Amplitude instance. |
||||
* |
||||
* @param instanceName The name of the Amplitude instance. |
||||
* @param userProps JSON string with user properties to be set. |
||||
*/ |
||||
@ReactMethod |
||||
public void setUserProperties(String instanceName, ReadableMap userProps) { |
||||
if (userProps != null) { |
||||
Amplitude.getInstance(instanceName).setUserProperties( |
||||
new JSONObject(userProps.toHashMap())); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Log an analytics event. |
||||
* |
||||
* @param instanceName The name of the Amplitude instance. |
||||
* @param eventType The event type. |
||||
* @param eventPropsString JSON string with the event properties. |
||||
*/ |
||||
@ReactMethod |
||||
public void logEvent(String instanceName, String eventType, String eventPropsString) { |
||||
try { |
||||
JSONObject eventProps = new JSONObject(eventPropsString); |
||||
Amplitude.getInstance(instanceName).logEvent(eventType, eventProps); |
||||
} catch (JSONException e) { |
||||
JitsiMeetLogger.e(e, "Error logging event"); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public String getName() { |
||||
return NAME; |
||||
} |
||||
} |
@ -1,61 +0,0 @@ |
||||
/* |
||||
* Copyright @ 2018-present 8x8, Inc. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0 |
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
#import <React/RCTBridgeModule.h> |
||||
|
||||
#import "Amplitude.h" |
||||
#import "LogUtils.h" |
||||
|
||||
|
||||
@interface AmplitudeModule : NSObject<RCTBridgeModule> |
||||
@end |
||||
|
||||
@implementation AmplitudeModule |
||||
|
||||
RCT_EXPORT_MODULE(Amplitude) |
||||
|
||||
+ (BOOL)requiresMainQueueSetup { |
||||
return NO; |
||||
} |
||||
|
||||
RCT_EXPORT_METHOD(init:(NSString*)instanceName API_KEY:(NSString*)apiKey) { |
||||
[[Amplitude instanceWithName:instanceName] initializeApiKey:apiKey]; |
||||
} |
||||
|
||||
RCT_EXPORT_METHOD(setUserId:(NSString*)instanceName userId: (NSString *) userId) { |
||||
[[Amplitude instanceWithName:instanceName] setUserId:userId]; |
||||
} |
||||
|
||||
RCT_EXPORT_METHOD(setUserProperties:(NSString*)instanceName userPropsString:(NSDictionary*)userProps) { |
||||
if (userProps != nil) { |
||||
[[Amplitude instanceWithName:instanceName] setUserProperties:userProps]; |
||||
} |
||||
} |
||||
|
||||
RCT_EXPORT_METHOD(logEvent:(NSString*)instanceName eventType:(NSString*)eventType eventPropsString:(NSString*)eventPropsString) { |
||||
NSError *error; |
||||
NSData *eventPropsData = [eventPropsString dataUsingEncoding:NSUTF8StringEncoding]; |
||||
NSDictionary *eventProperties = [NSJSONSerialization JSONObjectWithData:eventPropsData |
||||
options:NSJSONReadingMutableContainers |
||||
error:&error]; |
||||
if (eventProperties == nil) { |
||||
DDLogError(@"[Amplitude] Error parsing event properties: %@", error); |
||||
} else { |
||||
[[Amplitude instanceWithName:instanceName] logEvent:eventType withEventProperties:eventProperties]; |
||||
} |
||||
} |
||||
|
||||
@end |
@ -1,115 +0,0 @@ |
||||
import { NativeModules } from 'react-native'; |
||||
|
||||
const { Amplitude: AmplitudeNative } = NativeModules; |
||||
|
||||
/** |
||||
* Wrapper for the Amplitude native module. |
||||
*/ |
||||
class Amplitude { |
||||
/** |
||||
* Create new Amplitude instance. |
||||
* |
||||
* @param {string} instanceName - The name of the Amplitude instance. Should |
||||
* be used only for multi-project logging. |
||||
*/ |
||||
constructor(instanceName) { |
||||
// It might not have been included in the build.
|
||||
if (!AmplitudeNative) { |
||||
throw new Error('Amplitude analytics is not supported'); |
||||
} |
||||
|
||||
this._instanceName = instanceName; |
||||
} |
||||
|
||||
/** |
||||
* Initializes the Amplitude SDK. |
||||
* |
||||
* @param {string} apiKey - The API_KEY of the Amplitude project. |
||||
* @returns {void} |
||||
*/ |
||||
init(apiKey) { |
||||
AmplitudeNative.init(this._instanceName, apiKey); |
||||
} |
||||
|
||||
/** |
||||
* Sets an identifier for the current user. |
||||
* |
||||
* @param {string} userId - The new user id. |
||||
* @param {string} opt_userId - Currently not used. |
||||
* @param {Object} opt_config - Currently not used. |
||||
* @param {Function} opt_callback - Currently not used. |
||||
* @returns {void} |
||||
*/ |
||||
setUserId(userId, opt_userId, opt_config, opt_callback) { // eslint-disable-line camelcase, no-unused-vars
|
||||
AmplitudeNative.setUserId(this._instanceName, userId); |
||||
} |
||||
|
||||
/** |
||||
* Sets user properties for the current user. |
||||
* |
||||
* @param {Object} userProperties - The user properties to be set. |
||||
* @returns {void} |
||||
*/ |
||||
setUserProperties(userProperties) { |
||||
AmplitudeNative.setUserProperties(this._instanceName, userProperties); |
||||
} |
||||
|
||||
/** |
||||
* Log an event with eventType and eventProperties. |
||||
* |
||||
* @param {string} eventType - The type of the event. |
||||
* @param {Object} eventProperties - The properties of the event. |
||||
* @returns {void} |
||||
*/ |
||||
logEvent(eventType, eventProperties) { |
||||
// The event properties are converted to JSON string because of known
|
||||
// performance issue when passing objects trough the RN bridge too
|
||||
// often (a few times a second).
|
||||
AmplitudeNative.logEvent( |
||||
this._instanceName, eventType, JSON.stringify(eventProperties)); |
||||
} |
||||
|
||||
} |
||||
|
||||
/** |
||||
* Cache of <tt>Amplitude</tt> instances by instanceName. |
||||
*/ |
||||
const instances = {}; |
||||
|
||||
/** |
||||
* The default (with instanceName - undefined) <tt>Amplitude</tt> instance. |
||||
*/ |
||||
let defaultInstance; |
||||
|
||||
export default { |
||||
/** |
||||
* Returns a <tt>Amplitude</tt> instance. |
||||
* |
||||
* @param {Object} options - Optional parameters. |
||||
* @param {string} options.host - The host property from the current URL. |
||||
* @param {string|undefined} options.instanceName - The name of the |
||||
* amplitude instance. Should be used only for multi-project logging. |
||||
* @returns {Amplitude} |
||||
*/ |
||||
getInstance(options = {}) { |
||||
let instance; |
||||
|
||||
const { host = '', instanceName = '' } = options; |
||||
|
||||
let internalInstanceName = host; |
||||
|
||||
if (instanceName !== '') { |
||||
internalInstanceName += `-${instanceName}`; |
||||
} |
||||
|
||||
if (internalInstanceName === '') { |
||||
instance = defaultInstance = defaultInstance || new Amplitude(); |
||||
} else { |
||||
instance = instances[internalInstanceName] |
||||
= instances[internalInstanceName] |
||||
|| new Amplitude(internalInstanceName); |
||||
} |
||||
|
||||
return instance; |
||||
} |
||||
}; |
@ -1,14 +0,0 @@ |
||||
import amplitude from 'amplitude-js'; |
||||
|
||||
export default { |
||||
/** |
||||
* Returns the AmplitudeClient instance. |
||||
* |
||||
* @param {Object} options - Optional parameters. |
||||
* @property {string} options.instanceName - The name of the AmplitudeClient instance. |
||||
* @returns {AmplitudeClient} |
||||
*/ |
||||
getInstance(options = {}) { |
||||
return amplitude.getInstance(options.instanceName); |
||||
} |
||||
}; |
@ -1,9 +1,23 @@ |
||||
import DefaultPreference from 'react-native-default-preference'; |
||||
import DeviceInfo from 'react-native-device-info'; |
||||
|
||||
/** |
||||
* Custom logic for setting the correct device id. |
||||
* |
||||
* @param {AmplitudeClient} amplitude - The amplitude instance. |
||||
* @returns {void} |
||||
*/ |
||||
export function fixDeviceID(amplitude) { // eslint-disable-line no-unused-vars
|
||||
export async function fixDeviceID(amplitude) { |
||||
await DefaultPreference.setName('jitsi-preferences'); |
||||
|
||||
const current = await DefaultPreference.get('amplitudeDeviceId'); |
||||
|
||||
if (current) { |
||||
amplitude.setDeviceId(current); |
||||
} else { |
||||
const uid = DeviceInfo.getUniqueId(); |
||||
|
||||
amplitude.setDeviceId(uid); |
||||
DefaultPreference.set('amplitudeDeviceId', uid); |
||||
} |
||||
} |
||||
|
@ -1,2 +1 @@ |
||||
export { default as amplitude } from './Amplitude'; |
||||
export * from './fixDeviceID'; |
||||
|
Loading…
Reference in new issue