Skip to content

Commit 348898f

Browse files
committed
add @aptabase/browser
1 parent 9900d6e commit 348898f

File tree

14 files changed

+295
-1
lines changed

14 files changed

+295
-1
lines changed
77.9 KB
Loading
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { init, trackEvent } from '@aptabase/browser';
2+
3+
init('A-DEV-0000000000');
4+
5+
trackEvent('background_service_started');
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "plasmo-browserext",
3+
"displayName": "Plasmo browserext",
4+
"version": "0.0.1",
5+
"description": "A basic Plasmo extension.",
6+
"author": "Plasmo Corp. <foss@plasmo.com>",
7+
"scripts": {
8+
"dev": "plasmo dev",
9+
"build": "plasmo build",
10+
"test": "plasmo test"
11+
},
12+
"dependencies": {
13+
"plasmo": "0.84.0",
14+
"react": "18.2.0",
15+
"react-dom": "18.2.0",
16+
"@aptabase/browser": "*"
17+
},
18+
"devDependencies": {
19+
"@types/chrome": "0.0.254",
20+
"@types/react": "18.2.45",
21+
"@types/react-dom": "18.2.17",
22+
"typescript": "5.3.3"
23+
},
24+
"manifest": {
25+
"permissions": [
26+
"storage"
27+
],
28+
"host_permissions": [
29+
"https://*/*"
30+
]
31+
}
32+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { trackEvent } from '@aptabase/browser';
2+
import { useState } from 'react';
3+
4+
function IndexPopup() {
5+
const [count, setCount] = useState(0);
6+
7+
function increment() {
8+
trackEvent('increment', { count: count + 1 });
9+
setCount((c) => c + 1);
10+
}
11+
12+
function decrement() {
13+
trackEvent('decrement', { count: count - 1 });
14+
setCount((c) => c - 1);
15+
}
16+
17+
return (
18+
<div>
19+
<button onClick={increment}>Increment</button>
20+
<button onClick={decrement}>decrement</button>
21+
</div>
22+
);
23+
}
24+
25+
export default IndexPopup;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "plasmo/templates/tsconfig.base",
3+
"exclude": ["node_modules"],
4+
"include": [".plasmo/index.d.ts", "./**/*.ts", "./**/*.tsx"],
5+
"compilerOptions": {
6+
"paths": {
7+
"~*": ["./*"]
8+
},
9+
"baseUrl": "."
10+
}
11+
}

packages/browser/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 0.1.0
2+
3+
- Initial version of the Aptabase SDK for Browser Extensions

packages/browser/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Sumbit Labs Ltd.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

packages/browser/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
![Aptabase](https://aptabase.com/og.png)
2+
3+
# Aptabase SDK for Browser Extensions
4+
5+
A tiny SDK (1 kB) to instrument your Browser/Chrome extensions with Aptabase, an Open Source, Privacy-First and Simple Analytics for Mobile, Desktop and Web Apps.
6+
7+
## Install
8+
9+
Install the SDK using npm or your preferred JavaScript package manager
10+
11+
```bash
12+
npm add @aptabase/browser
13+
```
14+
15+
## Usage
16+
17+
First you need to get your `App Key` from Aptabase, you can find it in the `Instructions` menu on the left side menu.
18+
19+
Initialize the SDK in your `Background Script` using your `App Key`:
20+
21+
```js
22+
import { init } from '@aptabase/browser';
23+
24+
init('<YOUR_APP_KEY>'); // 👈 this is where you enter your App Key
25+
```
26+
27+
The init function also supports an optional second parameter, which is an object with the `isDebug` property. Your data is seggregated between development and production environments, so it's important to set this value correctly. By default the SDK will use the `NODE_ENV` environment variable to determine if it's in development or production mode, which is only available in bundlers like Webpack, Rollup, etc. If you're not using a bundler, you can pass in the `isDebug` property manually.
28+
29+
Afterwards you can start tracking events with `trackEvent` from anywhere in your extension:
30+
31+
```js
32+
import { trackEvent } from '@aptabase/browser';
33+
34+
trackEvent('connect_click'); // An event with no properties
35+
trackEvent('play_music', { name: 'Here comes the sun' }); // An event with a custom property
36+
```
37+
38+
A few important notes:
39+
40+
1. The SDK will automatically enhance the event with some useful information, like the OS, the app version, and other things.
41+
2. You're in control of what gets sent to Aptabase. This SDK does not automatically track any events, you need to call `trackEvent` manually.
42+
- Because of this, it's generally recommended to at least track an event at startup
43+
3. You do not need to await the `trackEvent` function, it'll run in the background.
44+
4. Only strings and numeric values are allowed on custom properties

packages/browser/package.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "@aptabase/browser",
3+
"version": "0.1.0",
4+
"type": "module",
5+
"description": "Browser Extension SDK for Aptabase: Open Source, Privacy-First and Simple Analytics for Mobile, Desktop and Web Apps",
6+
"main": "./dist/index.cjs",
7+
"module": "./dist/index.js",
8+
"types": "./dist/index.d.ts",
9+
"exports": {
10+
".": {
11+
"require": "./dist/index.cjs",
12+
"import": "./dist/index.js",
13+
"types": "./dist/index.d.ts"
14+
}
15+
},
16+
"repository": {
17+
"type": "git",
18+
"url": "git+https://github.com/aptabase/aptabase-js.git",
19+
"directory": "packages/browser"
20+
},
21+
"bugs": {
22+
"url": "https://github.com/aptabase/aptabase-js/issues"
23+
},
24+
"homepage": "https://github.com/aptabase/aptabase-js",
25+
"license": "MIT",
26+
"scripts": {
27+
"build": "tsup",
28+
"prepublishOnly": "npm run build"
29+
},
30+
"files": [
31+
"README.md",
32+
"LICENSE",
33+
"dist",
34+
"package.json"
35+
]
36+
}

packages/browser/src/global.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare var aptabase: {
2+
init: Function;
3+
trackEvent: Function;
4+
};

0 commit comments

Comments
 (0)