Skip to content

Commit

Permalink
Mixpanel migration (#1560)
Browse files Browse the repository at this point in the history
* bridge

* bridge

* correct native implementation and pass args to homepage

* people identify

* rm dep

* doc

* _peopleIdentify

Co-authored-by: Ibrahim Taveras <ibrahimtaveras00@gmail.com>
  • Loading branch information
estebanmino and ibrahimtaveras00 authored Jun 2, 2020
1 parent d500249 commit 40d3306
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.util.ArrayList;
import java.util.HashMap;
import com.facebook.react.bridge.Promise;

public class RCTAnalytics extends ReactContextBaseJavaModule {

Expand Down Expand Up @@ -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
Expand Down
28 changes: 19 additions & 9 deletions app/components/Views/BrowserTab/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -391,6 +392,7 @@ export class BrowserTab extends PureComponent {
wizardScrollAdjusted = false;
isReloading = false;
approvalRequest;
analyticsDistinctId;

/**
* Check that page metadata is available and call callback
Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -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 });
Expand Down Expand Up @@ -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);
};

Expand Down
19 changes: 19 additions & 0 deletions app/core/Analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ class Analytics {
});
};

/**
* Identify current user to mixpanel people
*/
_peopleIdentify = () => {
RCTAnalytics.peopleIdentify();
};

/**
* Creates a Analytics instance
*/
Expand All @@ -39,6 +46,7 @@ class Analytics {
this.listeners = [];
Analytics.instance = this;
RCTAnalytics.optIn(this.enabled);
this._peopleIdentify();
}
return Analytics.instance;
}
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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);
},
Expand Down
14 changes: 13 additions & 1 deletion ios/MetaMask/NativeModules/RCTAnalytics/RCTAnalytics.m
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 40d3306

Please sign in to comment.