From 40d33069b8023467a4306ac1f4c8835bdc3f64aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20Mi=C3=B1o?= Date: Tue, 2 Jun 2020 15:52:05 -0400 Subject: [PATCH] Mixpanel migration (#1560) * bridge * bridge * correct native implementation and pass args to homepage * people identify * rm dep * doc * _peopleIdentify Co-authored-by: Ibrahim Taveras --- .../metamask/nativeModules/RCTAnalytics.java | 14 ++++++++-- app/components/Views/BrowserTab/index.js | 28 +++++++++++++------ app/core/Analytics.js | 19 +++++++++++++ .../NativeModules/RCTAnalytics/RCTAnalytics.m | 14 +++++++++- 4 files changed, 63 insertions(+), 12 deletions(-) diff --git a/android/app/src/main/java/io/metamask/nativeModules/RCTAnalytics.java b/android/app/src/main/java/io/metamask/nativeModules/RCTAnalytics.java index 2991022c56f..62c2409e175 100644 --- a/android/app/src/main/java/io/metamask/nativeModules/RCTAnalytics.java +++ b/android/app/src/main/java/io/metamask/nativeModules/RCTAnalytics.java @@ -20,6 +20,7 @@ import java.util.ArrayList; import java.util.HashMap; +import com.facebook.react.bridge.Promise; public class RCTAnalytics extends ReactContextBaseJavaModule { @@ -54,15 +55,24 @@ public void trackEvent(ReadableMap e) { } @ReactMethod - public void optIn(boolean val) { + public void getDistinctId(Promise promise) { + String distinctId = this.mixpanel.getDistinctId(); + promise.resolve(distinctId); + } + @ReactMethod + public void peopleIdentify() { + String distinctId = this.mixpanel.getDistinctId(); + this.mixpanel.getPeople().identify(distinctId); + } + @ReactMethod + public void optIn(boolean val) { if(val){ this.mixpanel.optInTracking(); }else{ this.mixpanel.optOutTracking(); } - } @ReactMethod diff --git a/app/components/Views/BrowserTab/index.js b/app/components/Views/BrowserTab/index.js index 51be8842d78..a3b35d062c3 100644 --- a/app/components/Views/BrowserTab/index.js +++ b/app/components/Views/BrowserTab/index.js @@ -64,6 +64,7 @@ import EntryScriptWeb3 from '../../../core/EntryScriptWeb3'; const { HOMEPAGE_URL, USER_AGENT, NOTIFICATION_NAMES } = AppConstants; const HOMEPAGE_HOST = 'home.metamask.io'; +const MM_MIXPANEL_TOKEN = process.env.MM_MIXPANEL_TOKEN; const styles = StyleSheet.create({ wrapper: { @@ -391,6 +392,7 @@ export class BrowserTab extends PureComponent { wizardScrollAdjusted = false; isReloading = false; approvalRequest; + analyticsDistinctId; /** * Check that page metadata is available and call callback @@ -721,12 +723,15 @@ export class BrowserTab extends PureComponent { const entryScriptWeb3 = await EntryScriptWeb3.get(); const analyticsEnabled = Analytics.getEnabled(); + const disctinctId = await Analytics.getDistinctId(); const homepageScripts = ` window.__mmFavorites = ${JSON.stringify(this.props.bookmarks)}; window.__mmSearchEngine = "${this.props.searchEngine}"; window.__mmMetametrics = ${analyticsEnabled}; - `; + window.__mmDistinctId = "${disctinctId}"; + window.__mmMixpanelToken = "${MM_MIXPANEL_TOKEN}"; + `; await this.setState({ entryScriptWeb3: entryScriptWeb3 + SPA_urlChangeListener, homepageScripts }); Engine.context.AssetsController.hub.on('pendingSuggestedAsset', suggestedAssetMeta => { @@ -819,22 +824,24 @@ export class BrowserTab extends PureComponent { } } - refreshHomeScripts() { + refreshHomeScripts = async () => { const analyticsEnabled = Analytics.getEnabled(); - + const disctinctId = await Analytics.getDistinctId(); const homepageScripts = ` window.__mmFavorites = ${JSON.stringify(this.props.bookmarks)}; window.__mmSearchEngine="${this.props.searchEngine}"; - window.__mmMetametrics = ${analyticsEnabled}; - window.postMessage('updateFavorites', '*'); - `; + window.__mmMetametrics = ${analyticsEnabled}; + window.__mmDistinctId = "${disctinctId}"; + window.__mmMixpanelToken = "${MM_MIXPANEL_TOKEN}"; + window.postMessage('updateFavorites', '*'); + `; this.setState({ homepageScripts }, () => { const { current } = this.webview; if (current) { current.injectJavaScript(homepageScripts); } }); - } + }; setTabActive() { this.setState({ activated: true }); @@ -1178,16 +1185,19 @@ export class BrowserTab extends PureComponent { } } const analyticsEnabled = Analytics.getEnabled(); - + const disctinctId = await Analytics.getDistinctId(); const homepageScripts = ` window.__mmFavorites = ${JSON.stringify(this.props.bookmarks)}; window.__mmSearchEngine="${this.props.searchEngine}"; - window.__mmMetametrics = ${analyticsEnabled}; + window.__mmMetametrics = ${analyticsEnabled}; + window.__mmDistinctId = "${disctinctId}"; + window.__mmMixpanelToken = "${MM_MIXPANEL_TOKEN}"; `; this.setState({ homepageScripts }); } }) ); + Analytics.trackEvent(ANALYTICS_EVENT_OPTS.DAPP_ADD_TO_FAVORITE); }; diff --git a/app/core/Analytics.js b/app/core/Analytics.js index f05512bac50..c28c74acad3 100644 --- a/app/core/Analytics.js +++ b/app/core/Analytics.js @@ -30,6 +30,13 @@ class Analytics { }); }; + /** + * Identify current user to mixpanel people + */ + _peopleIdentify = () => { + RCTAnalytics.peopleIdentify(); + }; + /** * Creates a Analytics instance */ @@ -39,6 +46,7 @@ class Analytics { this.listeners = []; Analytics.instance = this; RCTAnalytics.optIn(this.enabled); + this._peopleIdentify(); } return Analytics.instance; } @@ -79,6 +87,14 @@ class Analytics { this.listeners.push(listener); }; + /** + * Get current tracking id + */ + getDistinctId = async () => { + const id = await RCTAnalytics.getDistinctId(); + return id; + }; + /** * Track event * @@ -232,6 +248,9 @@ export default { subscribe(listener) { return instance && instance.subscribe(listener); }, + getDistinctId() { + return instance && instance.getDistinctId(); + }, trackEvent(event) { return instance && instance.trackEvent(event); }, diff --git a/ios/MetaMask/NativeModules/RCTAnalytics/RCTAnalytics.m b/ios/MetaMask/NativeModules/RCTAnalytics/RCTAnalytics.m index 1e7f927eb0f..26d9f200ea3 100644 --- a/ios/MetaMask/NativeModules/RCTAnalytics/RCTAnalytics.m +++ b/ios/MetaMask/NativeModules/RCTAnalytics/RCTAnalytics.m @@ -24,13 +24,25 @@ @implementation RCTAnalytics }); } - RCT_EXPORT_METHOD(trackEvent:(NSDictionary *)event) { [[Mixpanel sharedInstance] track: [self getCategory:event] properties:[self getInfo:event]]; } +RCT_EXPORT_METHOD(peopleIdentify) +{ + [[Mixpanel sharedInstance] identify:[[Mixpanel sharedInstance] distinctId]]; +} + +RCT_REMAP_METHOD(getDistinctId, + getDistinctIdWithResolver:(RCTPromiseResolveBlock)resolve + rejecter:(RCTPromiseRejectBlock)reject) +{ + resolve([[Mixpanel sharedInstance] distinctId]); +} + + RCT_REMAP_METHOD(getRemoteVariables, getRemoteVariableWithResolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)