Skip to content
This repository has been archived by the owner on Jul 20, 2023. It is now read-only.

Commit

Permalink
Merge pull request #4 from Bowery-RES/web-2336-amplitude-metrics
Browse files Browse the repository at this point in the history
[WEB-2336] Amplitude metrics
  • Loading branch information
ArtemySinitsa authored Dec 24, 2019
2 parents 86cb149 + 681d5c5 commit c31f354
Show file tree
Hide file tree
Showing 13 changed files with 308 additions and 216 deletions.
64 changes: 41 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bowery-chrome-extension",
"version": "1.0.1",
"version": "1.1.0",
"description": "Bowery chrome extension for Streeteasy",
"scripts": {
"build": "node utils/build.js",
Expand Down Expand Up @@ -29,6 +29,7 @@
"svelte-select": "^3.1.1"
},
"dependencies": {
"amplitude-js": "^5.8.0",
"axios": "^0.19.0",
"chrome-extension-async": "^3.3.2",
"filemanager-webpack-plugin": "^2.0.5",
Expand Down
Binary file modified src/img/bowery_icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/img/bowery_icon_disabled.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
68 changes: 35 additions & 33 deletions src/js/background.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,42 @@
import '../img/bowery_icon.png';
import '../img/bowery_icon_disabled.png';
import 'chrome-extension-async';
import get from 'lodash/get';
import { validateToken } from '@lib/api';
import { BOWERY_APP_DOMAIN } from 'secrets';
import AuthService from '../services/AuthService'
import TrackingService from '../services/TrackingService'
import { EVENTS } from '../lib/constants'

async function getBoweryToken() {
const valid = await validateToken()
if (valid) {
return;
}

const window = await chrome.windows.create({
url: BOWERY_APP_DOMAIN,
type: 'popup',
focused: false,
});
const tabId = get(window, 'tabs[0].id');
const [jwToken] = await chrome.tabs.executeScript(tabId, { code: "localStorage.getItem('jwToken')" });
await chrome.windows.remove(window.id);
await chrome.storage.local.set({ token: jwToken });
if (!jwToken) {
throw new Error('You have to be logged in to the Bowery application.');
}
async function activationHandler({ tabId }) {
const tab = await chrome.tabs.get(tabId);
const url = tab.url || tab.pendingUrl;
if (url.match(/https:\/\/streeteasy.com\/building\/|https:\/\/streeteasy.com\/rental\//)) {
await chrome.browserAction.setIcon({ path: 'bowery_icon.png' });
chrome.browserAction.enable();
} else {
await chrome.browserAction.setIcon({ path: 'bowery_icon_disabled.png' });
chrome.browserAction.disable();
}
}

chrome.extension.onRequest.addListener(async ({ type, data }) => {
if (type === 'popup-opened') {
try {
const [activeTab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (!activeTab.url.match(/https:\/\/streeteasy.com\/building\/|https:\/\/streeteasy.com\/rental\//)) {
throw new Error('The extension pulls data only from Streeteasy.');
}
await getBoweryToken();
await chrome.tabs.executeScript({ file: 'parse-comp.bundle.js' });
} catch (error) {
chrome.extension.sendRequest({ error: error.message });
}
chrome.tabs.onActivated.addListener(activationHandler);
chrome.webNavigation.onBeforeNavigate.addListener(activationHandler);

chrome.runtime.onMessage.addListener(async ({ type }) => {
try {
switch (type) {
case EVENTS.INITIALIZE:
const authInfo = await AuthService.authenticate();
const user = authInfo.user;
TrackingService.identify(user)
TrackingService.logEvent('Chrome Extension Clicked');
chrome.tabs.executeScript({ file: 'parse-comp.bundle.js' });
break;
case EVENTS.COMP_ADDED:
TrackingService.logEvent('Chrome Extension Comp Added');
break;
default:
break;
}
} catch (error) {
chrome.runtime.sendMessage({ error: error });
}
});
Loading

0 comments on commit c31f354

Please sign in to comment.