Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mixpanel migration #1560

Merged
merged 10 commits into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -65,6 +65,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 @@ -408,6 +409,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 @@ -738,12 +740,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 @@ -836,22 +841,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 @@ -1195,16 +1202,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