Skip to content

Commit

Permalink
feat: update uninstall url (#23)
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Erichsen <patrick.a.erichsen@gmail.com>
  • Loading branch information
Patrick-Erichsen authored Nov 2, 2021
1 parent ea5698f commit 6163d7b
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 9 deletions.
2 changes: 1 addition & 1 deletion assets/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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}'"
Expand Down
22 changes: 17 additions & 5 deletions src/background/index.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -23,13 +31,17 @@ 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',
active: true,
});
}
});

chrome.runtime.onMessage.addListener((request) => {
if (isUninstallMsg(request)) {
chrome.runtime.setUninstallURL(request.uninstallUrl);
}
});
4 changes: 3 additions & 1 deletion src/content/hooks/useUrlChangeChrome.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -28,6 +28,8 @@ export const useUrlChangeChrome = (
onUrlChange(window.location.href);
}

tryIdentifyUserFromUrlParam(window.location.href);

return () => {
chrome.runtime.onMessage.removeListener(onUrlChangeWrapper);
};
Expand Down
5 changes: 5 additions & 0 deletions src/types/Chrome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ export interface ChromeUrlUpdate {
event: 'URL_UPDATE';
url: string;
}

export interface ChromeUninstallUrlUpdate {
event: 'UPDATE_UNINSTALL_URL';
uninstallUrl: string;
}
22 changes: 22 additions & 0 deletions src/utils/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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);

Expand Down

0 comments on commit 6163d7b

Please # to comment.