diff --git a/assets/manifest.json b/assets/manifest.json index 2440006..50dd458 100644 --- a/assets/manifest.json +++ b/assets/manifest.json @@ -2,7 +2,7 @@ "name": "Offie - Airbnb Wifi Reviews", "description": "View Airbnb Wifi and workspace info from the search results page.", "manifest_version": 3, - "version": "0.0.5", + "version": "0.0.6", "icons": { "16": "img/offie-logo-16.png", "48": "img/offie-logo-48.png", diff --git a/package.json b/package.json index 2b3e032..54a86d8 100644 --- a/package.json +++ b/package.json @@ -5,10 +5,10 @@ "private": true, "scripts": { "test": "npx jest", - "build": "rm -rf dist/ && webpack --mode=production --env production", + "build": "rm -rf prod-build/ && webpack --mode=production --env production", "start": "webpack --watch --mode=development --env development", "prettier": "prettier 'src/**/*.{ts,tsx}'", - "clean": "rm -rf dist/ node_modules", + "clean": "rm -rf prod-build/ node_modules", "prettier:fix": "prettier --write 'src/**/*.{ts,tsx}'", "lint": "eslint 'src/**/*.{ts,tsx}'", "lint:fix": "eslint --fix 'src/**/*.{ts,tsx}'" diff --git a/src/background/index.ts b/src/background/index.ts index 7743998..85a02fd 100644 --- a/src/background/index.ts +++ b/src/background/index.ts @@ -1,9 +1,17 @@ -import mixpanel from 'mixpanel-browser'; import { rollbar, initAnalytics, logUrlChange } from '../utils'; -import { ChromeUrlUpdate } from '../types/Chrome'; +import { ChromeUninstallUrlUpdate, ChromeUrlUpdate } from '../types/Chrome'; initAnalytics(); +export const isUninstallMsg = ( + msg: unknown | ChromeUninstallUrlUpdate +): msg is ChromeUninstallUrlUpdate => { + return ( + (msg as ChromeUninstallUrlUpdate).event && + (msg as ChromeUninstallUrlUpdate).event === 'UPDATE_UNINSTALL_URL' + ); +}; + chrome.tabs.onUpdated.addListener((tabId, { url }) => { if (url) { logUrlChange(url); @@ -23,9 +31,7 @@ chrome.runtime.onInstalled.addListener(() => { } if (process.env.NODE_ENV === 'production') { - chrome.runtime.setUninstallURL( - `https://offie.co/uninstall?id=${mixpanel.get_distinct_id()}` - ); + chrome.runtime.setUninstallURL('https://offie.co/uninstall'); chrome.tabs.create({ url: 'https://offie.co/welcome', @@ -33,3 +39,9 @@ chrome.runtime.onInstalled.addListener(() => { }); } }); + +chrome.runtime.onMessage.addListener((request) => { + if (isUninstallMsg(request)) { + chrome.runtime.setUninstallURL(request.uninstallUrl); + } +}); diff --git a/src/content/hooks/useUrlChangeChrome.ts b/src/content/hooks/useUrlChangeChrome.ts index c332377..04577b2 100644 --- a/src/content/hooks/useUrlChangeChrome.ts +++ b/src/content/hooks/useUrlChangeChrome.ts @@ -1,7 +1,7 @@ /* eslint-disable react-hooks/exhaustive-deps */ import { useEffect } from 'react'; import { ChromeUrlUpdate } from '../../types/Chrome'; -import * as analytics from '../../utils/analytics'; +import { tryIdentifyUserFromUrlParam } from '../../utils/analytics'; /** * Invoke the `onUrlChange` param when Chrome detects that @@ -28,6 +28,8 @@ export const useUrlChangeChrome = ( onUrlChange(window.location.href); } + tryIdentifyUserFromUrlParam(window.location.href); + return () => { chrome.runtime.onMessage.removeListener(onUrlChangeWrapper); }; diff --git a/src/types/Chrome.ts b/src/types/Chrome.ts index b37fac8..63d65dd 100644 --- a/src/types/Chrome.ts +++ b/src/types/Chrome.ts @@ -2,3 +2,8 @@ export interface ChromeUrlUpdate { event: 'URL_UPDATE'; url: string; } + +export interface ChromeUninstallUrlUpdate { + event: 'UPDATE_UNINSTALL_URL'; + uninstallUrl: string; +} diff --git a/src/utils/analytics.ts b/src/utils/analytics.ts index 8d53d65..2622764 100644 --- a/src/utils/analytics.ts +++ b/src/utils/analytics.ts @@ -6,6 +6,7 @@ import { } from '../content/utils/airbnb'; import { getParsedUrlSearch } from '../content/utils/misc'; import { rollbar } from './rollbar'; +import { ChromeUninstallUrlUpdate } from '../types/Chrome'; export const eventNames = { AIRBNB_SEARCH_URL: 'airbnbSearchUrl', @@ -23,6 +24,27 @@ export const initAnalytics = (): void => { } }; +export const tryIdentifyUserFromUrlParam = (url: string): void => { + const search = getParsedUrlSearch(url); + + const urlMixpanelId = search.mixpanel_id; + + if (typeof urlMixpanelId !== 'string') { + return; + } + + if (urlMixpanelId) { + mixpanel.identify(urlMixpanelId); + + const updateUninstallPayload: ChromeUninstallUrlUpdate = { + event: 'UPDATE_UNINSTALL_URL', + uninstallUrl: `https://offie.co/uninstall?id=${mixpanel.get_distinct_id()}`, + }; + + chrome.runtime.sendMessage(updateUninstallPayload); + } +}; + export const logUrlChange = (urlStr: string): void => { const search = getParsedUrlSearch(urlStr);