Skip to content

Instantly share code, notes, and snippets.

@masiamj
Created January 24, 2022 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masiamj/c7fa67ff310e3f0611ae6946bdd32c5a to your computer and use it in GitHub Desktop.
Save masiamj/c7fa67ff310e3f0611ae6946bdd32c5a to your computer and use it in GitHub Desktop.
import _ from 'lodash'
import * as Network from 'expo-network'
import * as Segment from 'expo-analytics-segment'
import ExpoMixpanelAnalytics from '@eeon/expo-mixpanel-analytics'
import { env } from '../../../constants/environment'
const mixpanelClient = new ExpoMixpanelAnalytics(env.mixpanelAppToken)
const createDomainPrefixer = (domain) => (event) => `${domain}_${event}`
const createTabsEvents = (domain) => {
const dp = createDomainPrefixer(domain)
return {
HEATMAP: {
HEATMAP_SECURITY_PRESSED: dp('heatmap_security.pressed'),
HEATMAP_SORT_UPDATED: dp('heatmap_sort.updated'),
},
RATINGS: {
ADD_SECURITY_TO_WATCHLIST_PRESSED: dp(
'ratings_add_security_to_watchlist.pressed'
),
CREATE_FREE_ACCOUNT_PRESSED: dp(`ratings_create_free_account.pressed`),
SECURITY_PRESSED: dp('ratings_security.pressed'),
SORT_UPDATED: dp('ratings_sort.updated'),
UPGRADE_PRESSED: dp('ratings_upgrade.pressed'),
},
RAW_METRICS: {
ADD_SECURITY_TO_WATCHLIST_PRESSED: dp(
'raw_metrics_add_security_to_watchlist.pressed'
),
CREATE_FREE_ACCOUNT_PRESSED: dp(
`raw_metrics_create_free_account.pressed`
),
SECURITY_PRESSED: dp('raw_metrics_security.pressed'),
SORT_UPDATED: dp('raw_metrics_sort.updated'),
UPGRADE_PRESSED: dp('raw_metrics_upgrade.pressed'),
},
}
}
export const eventNames = {
/**
* Create a Watchlist
*/
CREATE_WATCHLIST: {
ADD_STOCK_PRESSED: 'create_watchlist_add_stock.pressed',
NAME_WATCHLIST_CANCEL_PRESSED:
'create_watchlist_name_watchlist_cancel.pressed',
SAVE_WATCHLIST_PRESSED: 'create_watchlist_save_watchlist.pressed',
SAVE_WATCHLIST_NAME_PRESSED: 'create_watchlist_save_watchlist_name.pressed',
TRENDING_SECURITY_PRESSED: 'create_watchlist_trending_security.pressed',
},
/**
* Dashboard
*/
DASHBOARD: {
TIMEFRAME_UPDATED: 'dashboard_timeframe.updated',
CREATE_ANOTHER_WATCHLIST_PRESSED:
'dashboard_create_another_watchlist.pressed',
CREATE_FIRST_WATCHLIST_PRESSED: 'dashboard_create_first_watchlist.pressed',
CUSTOMIZE_BUTTON_PRESSED: 'dashboard_customize_button.pressed',
INDEX_OR_SECTOR_PRESSED: 'dashboard_index_or_sector.pressed',
MOST_POPULAR_COMPANIES_SEE_MORE_PRESSED:
'dashboard_most_popular_companies_see_more.pressed',
MOST_POPULAR_SECURITY_PRESSED: 'dashboard_most_popular_security.pressed',
TRENDING_NEWS_STORY_PRESSED: 'dashboard_trending_news_story.pressed',
WATCHLIST_PRESSED: 'dashboard_watchlist.pressed',
WATCHLIST_SECURITY_PRESSED: 'dashboard_watchlist_security.pressed',
WATCHLIST_TIMEFRAME_UPDATED: 'dashboard_watchlist_timeframe.updated',
},
/**
* Discover
*/
DISCOVER: {
TOP_RATED: {
LIST_PRESSED: 'discover_top_rated_list.pressed',
},
OPPORTUNITIES: {
LIST_PRESSED: 'discover_opportunities_list.pressed',
},
INDUSTRIES: {
LIST_PRESSED: 'discover_industries_list.pressed',
},
},
/**
* Global
*/
SCREEN_VIEWED: 'screen.viewed',
/**
* Industries
*/
INDUSTRIES: createTabsEvents('industries'),
/**
* Hook Events
*/
LOGOUT: 'logout',
NEWS_FILTER_UPDATED: 'news_filter.updated',
USER_AUTHENTICATED: 'user.authenticated',
USER_AUTHENTICATED_VIA_GOOGLE: 'user.authenticated_via_google',
USER_PROFILE_UPDATED: 'user_profile.updated',
USER_REGISTERED: 'user.registered',
USER_REGISTERED_VIA_GOOGLE: 'user.registered_via_google',
USER_SUBSCRIPTION_CREATED: 'user_subscription.created',
USER_WATCHLIST_CREATED: 'user_watchlist.created',
USER_WATCHLIST_DELETED: 'user_watchlist.deleted',
USER_WATCHLIST_SECURITY_ADDED: 'user_watchlist_security.added',
USER_WATCHLIST_UPDATED: 'user_watchlist.updated',
/**
* Login
*/
LOGIN: {
LOGIN_WITH_GOOGLE_PRESSED: 'login_login_with_google.pressed',
NO_ACCOUNT_PRESSED: 'login_no_account.pressed',
SUBMIT_PRESSED: 'login_submit.pressed',
},
/**
* Most Popular Companies
*/
MOST_POPULAR_COMPANIES: createTabsEvents('most_popular_companies'),
/**
* My Account
*/
MY_ACCOUNT: {
MANAGE_SUBSCRIPTION_PRESSED: 'my_account_manage_subscription.pressed',
},
/**
* News
*/
NEWS: {
TRENDING_EVENT_PRESSED: 'news_trending_event.pressed',
OUR_RESEARCH_STORY_PRESSED: 'news_our_research_story.pressed',
},
/**
* Opportunities
*/
OPPORTUNITIES: createTabsEvents('opportunities'),
/**
* Pricing Plans
*/
PRICING_PLANS: {
BILLING_PERIOD_UPDATED: 'pricing_plans_billing_period.updated',
CHECKOUT_PRESSED: 'pricing_plans_checkout.pressed',
},
/**
* Search
*/
SEARCH: {
ADD_SECURITY_TO_WATCHLIST_PRESSED:
'search_add_security_to_watchlist.pressed',
SECURITY_PRESSED: 'search_security.pressed',
},
/**
* Security Picker
*/
SECURITY_PICKER: {
SEARCHED_SECURITY_PRESSED: 'security_picker_searched_security.pressed',
TRENDING_SECURITY_PRESSED: 'security_picker_trending_security.pressed',
},
/**
* Security Quote page
*/
SECURITY: {
ADD_SECURITY_TO_WATCHLIST_PRESSED: 'add_security_to_watchlist.pressed',
NEWS_STORY_PRESSED: 'security_news_story.pressed',
PEERS: createTabsEvents('security_peers'),
PEERS_RATINGS_CREATE_FREE_ACCOUNT_PRESSED:
'security_peers_ratings_create_free_account.pressed',
PEERS_RATINGS_UPGRADE_PRESSED: 'security_peers_ratings_upgrade.pressed',
RATINGS_CREATE_FREE_ACCOUNT_PRESSED:
'security_ratings_create_free_account.pressed',
RATINGS_UPGRADE_PRESSED: 'security_ratings_upgrade.pressed',
RATINGS_COMPARE_TOP_STOCKS_PRESSED:
'security_ratings_compare_top_stocks.pressed',
RATINGS_CREATE_FREE_ACCOUNT_PRESSED:
'security_ratings_create_free_account.pressed',
RATINGS_UPGRADE_PRESSED: 'security_ratings_upgrade.pressed',
STATS_RATING_PRESSED: 'security_stats_rating.pressed',
STATS_UPGRADE_TO_SEE_RATINGS_PRESSED:
'security_stats_upgrade_to_see_ratings.pressed',
TIMEFRAME_UPDATED: 'security_timeframe.updated',
},
/**
* Signup
*/
SIGNUP: {
LOGIN_PRESSED: 'signup_login.pressed',
REGISTER_WITH_GOOGLE_PRESSED: 'signup_register_with_google.pressed',
SUBMIT_PRESSED: 'signup_submit.pressed',
},
/**
* Top Rated
*/
TOP_RATED: createTabsEvents('top_rated'),
/**
* Trending Event
*/
TRENDING_EVENT: {
NEWS_STORY_PRESSED: 'trending_event_news_story.pressed',
SECURITY_PRESSED: 'trending_event_security.pressed',
},
/**
* Update a Watchlist
*/
UPDATE_WATCHLIST: {
ADD_STOCK_PRESSED: 'update_watchlist_add_stock.pressed',
DELETE_WATCHLIST_PRESSED: 'update_watchlist_delete_watchlist.pressed',
RENAME_WATCHLIST_PRESSED: 'update_watchlist_rename.pressed',
SAVE_WATCHLIST_PRESSED: 'update_watchlist_save_watchlist.pressed',
},
/**
* User Watchlist
*/
USER_WATCHLIST: {
...createTabsEvents('user_watchlist'),
HEATMAP_UPDATE_LIST_PRESSED: 'user_watchlist_heatmap_update_list.pressed',
NEWS_STORY_PRESSED: 'user_watchlist_news_story.pressed',
NEWS_STORY_SECURITY_PRESSED: 'user_watchlist_news_story_security.pressed',
},
}
const unsafeFieldsMap = {
firstName: '$first_name',
lastName: '$last_name',
email: '$email',
createdAt: '$created',
phone: '$phone',
}
const setIPAddress = async () => {
const $ip = await Network.getIpAddressAsync()
mixpanelClient.people_set({ $ip })
}
export const identifyUser = async (user) => {
try {
const userId = _.get(user, ['id'])
await mixpanelClient.identify(userId)
setIPAddress()
Segment.identify(userId, user)
} catch (_error) {}
}
/**
* Initializes Segment analytics
*/
export const initializeAnalytics = () => {
Segment.initialize({ iosWriteKey: env.segmentWriteKey })
}
/**
* @TODO Register the user on the server (DB Hook)
*/
export const registerUser = async (user) => {
try {
await mixpanelClient.register(user)
setIPAddress()
Segment.identify(user.id, user)
} catch (_error) {}
}
export const trackEvent = async (eventName, eventData = {}) => {
try {
mixpanelClient.track(eventName, eventData)
Segment.trackWithProperties(eventName, eventData)
} catch (_error) {}
}
export const trackScreen = (screenName, screenProps) => {
try {
Segment.screenWithProperties(screenName, screenProps)
} catch (_error) {}
}
const cleanUserFields = (user = {}) => {
const safeObject = _.entries(user).reduce((acc, [key, value]) => {
const safeField = _.get(unsafeFieldsMap, key)
if (safeField) {
acc[safeField] = value
}
return acc
}, {})
return safeObject
}
export const updateUser = async (userData = {}) => {
try {
await mixpanelClient.people_set(cleanUserFields(userData))
setIPAddress()
} catch (_error) {}
}
export const resetAnalytics = () => {
try {
mixpanelClient.reset()
} catch (_error) {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment