-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fe 173 browser extension tracking requests (#82)
* feat: add manifest v3 features and toggle * chore: bump package version * chore: add more environment variables
- Loading branch information
1 parent
378c983
commit e2d4dc0
Showing
22 changed files
with
523 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
IS_FIREFOX=false | ||
MANIFEST_V2=false | ||
RUDDERSTACK_STAGING_KEY=1xxxxxxxxxx | ||
RUDDERSTACK_PRODUCTION_KEY=1xxxxxxxxxx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#!/usr/bin/env node | ||
|
||
const manifest = require(__dirname + '/../src/manifest.chrome.json'); | ||
const manifest = require(__dirname + '/../src/manifest.chrome.v3.json'); | ||
console.log(manifest.version); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,71 @@ | ||
function createQueueFunction() { | ||
function ga() { | ||
ga.q.push(arguments); | ||
} | ||
import * as storage from 'src/utils/storage'; | ||
import config from 'src/api/config'; | ||
import { toSnakeCase, toSnakeCaseObject } from '../utils/to-snake-case'; | ||
|
||
ga.q = []; | ||
ga.l = +new Date(); | ||
async function sendGoogleAnalyticsEvent(uid, category, action, fields = {}) { | ||
const params = new URLSearchParams({ | ||
v: 1, | ||
tid: config.googleAnalytics.id, | ||
uid, | ||
t: 'event', | ||
ec: category, | ||
ea: action, | ||
cd1: fields.dimension1, | ||
cd2: fields.dimension2, | ||
}); | ||
const url = `https://www.google-analytics.com/collect?${params.toString()}`; | ||
|
||
return ga; | ||
try { | ||
await fetch(url, { | ||
method: 'POST', | ||
}); | ||
} catch (error) { | ||
console.warn('googleanalytics.track error:', error); | ||
} | ||
} | ||
|
||
window.GoogleAnalyticsObject = 'ga'; | ||
const ga = window.ga = createQueueFunction(); | ||
async function sendRudderStackTrackEvent(uid, event, properties = {}) { | ||
const url = `${config.rudderStack.dataPlaneUrl}/v1/track`; | ||
const data = { | ||
userId: uid, | ||
event: toSnakeCase(event), | ||
properties: toSnakeCaseObject(properties), | ||
timestamp: new Date().toISOString(), | ||
}; | ||
|
||
ga('create', 'UA-63875543-2', 'auto'); | ||
ga('set', 'checkProtocolTask', null); | ||
try { | ||
await fetch(url, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Basic ${Buffer.from( | ||
config.rudderStack.key + ':' | ||
).toString('base64')}`, | ||
}, | ||
body: JSON.stringify(data), | ||
}); | ||
} catch (error) { | ||
console.warn('rudderanalytics.track error:', error); | ||
} | ||
} | ||
|
||
chrome.runtime.onMessage.addListener(function(data) { | ||
if (data.type !== 'track_event') { | ||
return; | ||
if (data.type === 'track_event') { | ||
storage | ||
.get('me') | ||
.then(({ me: { id } }) => | ||
sendGoogleAnalyticsEvent( | ||
id.toString(), | ||
data.category, | ||
data.action, | ||
data.fields | ||
) | ||
); | ||
} | ||
|
||
if (['task_created'].includes(data.type)) { | ||
storage.get('me').then(({ me: { id } }) => { | ||
sendRudderStackTrackEvent(id.toString(), data.type, data.properties); | ||
}); | ||
} | ||
ga('send', 'event', data.category, data.action, data.fields); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import qs from 'querystring'; | ||
import config from 'src/api/config'; | ||
|
||
const POPUP_WIDTH = config.popup.width; | ||
const POPUP_HEIGHT = config.popup.height; | ||
const BUTTON_MARGIN = config.popup.buttonMargin; | ||
const BUTTON_SIZE = config.popup.buttonSize; | ||
|
||
export function openPopupWindow(data) { | ||
const position = calculatePosition(data); | ||
const url = createURL(data.params); | ||
|
||
chrome.windows.create({ | ||
url, | ||
left: Math.round(position.x), | ||
top: Math.round(position.y), | ||
width: config.popup.width, | ||
height: config.popup.height, | ||
type: 'popup', | ||
}); | ||
} | ||
|
||
export function getIsManifestV2() { | ||
return MANIFEST_V2 === 'true' ? true : false; | ||
} | ||
|
||
export function createURL(params = {}) { | ||
const query = qs.stringify(params); | ||
|
||
return getIsManifestV2() | ||
? chrome.extension.getURL('popup.html?' + query) | ||
: chrome.runtime.getURL('popup.html?' + query); | ||
} | ||
|
||
export function addOnExtensionClickListener(callback) { | ||
if (getIsManifestV2()) { | ||
chrome.browserAction.onClicked.addListener(callback); | ||
return; | ||
} | ||
|
||
chrome.action.onClicked.addListener(callback); | ||
} | ||
|
||
export function executeScript(tabId, file) { | ||
if (getIsManifestV2()) { | ||
chrome.tabs.executeScript(tabId, { file }); | ||
return; | ||
} | ||
|
||
chrome.scripting.executeScript({ | ||
files: [file], | ||
target: { tabId }, | ||
}); | ||
} | ||
|
||
export function insertCSS(tabId, file) { | ||
if (getIsManifestV2()) { | ||
chrome.tabs.insertCSS(tabId, { file }); | ||
return; | ||
} | ||
|
||
chrome.scripting.insertCSS({ | ||
files: [file], | ||
target: { tabId }, | ||
}); | ||
} | ||
|
||
function calculatePosition(data) { | ||
if (data.anchor === 'screen') { | ||
return { | ||
x: (data.screen.width - POPUP_WIDTH) / 2, | ||
y: (data.screen.height - POPUP_HEIGHT) / 2, | ||
}; | ||
} else if (data.anchor === 'element') { | ||
return { | ||
x: | ||
data.element.x > data.screen.width / 2 | ||
? data.element.x - POPUP_WIDTH - BUTTON_MARGIN | ||
: data.element.x + BUTTON_SIZE + BUTTON_MARGIN, | ||
y: | ||
data.element.y > data.screen.height / 2 | ||
? data.element.y - POPUP_HEIGHT + BUTTON_SIZE | ||
: data.element.y, | ||
}; | ||
} else if (data.anchor === 'window') { | ||
return { | ||
x: data.window.x + (data.window.width - POPUP_WIDTH) / 2, | ||
y: data.window.y + (data.window.height - POPUP_HEIGHT) / 2, | ||
}; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.tw-button { | ||
vertical-align: -2px; | ||
vertical-align: -4px; | ||
margin-left: 4px; | ||
} |
Oops, something went wrong.