Skip to content

Commit 19f6f8a

Browse files
committed
release 0.3.0
1 parent 5f097b4 commit 19f6f8a

10 files changed

Lines changed: 198 additions & 86 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.3.0
2+
3+
- Added a new `AptabaseProvider` and `useAptabase` hook to make usage easier
4+
15
## 0.2.2
26

37
- Fix bundled file names

README.md

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,45 @@ npm add @aptabase/react-native
1616

1717
First, you need to get your `App Key` from Aptabase, you can find it in the `Instructions` menu on the left side menu.
1818

19-
Initialize the SDK as early as possible, ideally before declaring the `App` component:
19+
Initialize the SDK by using the `AptabaseProvider` on your `App` component:
2020

2121
```js
22-
import { init } from "@aptabase/react-native";
23-
24-
init("<YOUR_APP_KEY>"); // 👈 this is where you enter your App Key
22+
export default function App() {
23+
return (
24+
<AptabaseProvider appKey="<YOUR_APP_KEY>">
25+
<Counter />
26+
</AptabaseProvider>
27+
);
28+
}
2529
```
2630

27-
Afterwards, you can start tracking events with `trackEvent`:
31+
Afterwards, you can start tracking events with `trackEvent` from `useAptabase` hook:
2832

2933
```js
30-
import { trackEvent } from "@aptabase/react-native";
31-
32-
trackEvent("app_started"); // An event with no properties
33-
trackEvent("screen_view", { name: "Settings" }); // An event with a custom property
34+
import { useAptabase } from "@aptabase/react-native";
35+
36+
export function Counter() {
37+
const { trackEvent } = useAptabase();
38+
const [count, setCount] = useState(0);
39+
40+
const increment = () => {
41+
setCount(count + 1);
42+
trackEvent("increment", { count });
43+
};
44+
45+
const decrement = () => {
46+
setCount(count - 1);
47+
trackEvent("decrement", { count });
48+
};
49+
50+
return (
51+
<View>
52+
<Button onPress={increment} title="Increment" />
53+
<Button onPress={decrement} title="Decrement" />
54+
<Text>Count is {count}</Text>
55+
</View>
56+
);
57+
}
3458
```
3559

3660
**Note for Expo apps:** Events sent during development while running on Expo Go will not have the `App Version` property because native modules are not available in Expo Go. However, when you build your app and run it on a real device, the `App Version` property will be available. Alternatively, you can also set the `appVersion` during the `init` call so that it's available during development as well.
@@ -46,4 +70,3 @@ A few important notes:
4670
## Preparing for Submission to Apple App Store
4771

4872
When submitting your app to the Apple App Store, you'll need to fill out the `App Privacy` form. You can find all the answers on our [How to fill out the Apple App Privacy when using Aptabase](https://aptabase.com/docs/apple-app-privacy) guide.
49-

examples/HelloWorldExpo/App.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
import { StatusBar } from "expo-status-bar";
2-
import { Button, StyleSheet, Text, View } from "react-native";
3-
import { init, trackEvent } from "@aptabase/react-native";
4-
5-
init("A-DEV-0000000000");
6-
trackEvent("app_started");
2+
import { StyleSheet, Text, View } from "react-native";
3+
import { AptabaseProvider } from "@aptabase/react-native";
4+
import { Counter } from "./Counter";
75

86
export default function App() {
9-
const onClick = () => {
10-
trackEvent("Hello");
11-
};
12-
137
return (
14-
<View style={styles.container}>
15-
<Text>Open up App.js to start working on your app!</Text>
16-
<Button onPress={onClick} title="Click Me" color="#841584" />
17-
<StatusBar style="auto" />
18-
</View>
8+
<AptabaseProvider appKey="A-US-0928558097">
9+
<View style={styles.container}>
10+
<Text>Open up App.js to start working on your app!</Text>
11+
<Counter />
12+
<StatusBar style="auto" />
13+
</View>
14+
</AptabaseProvider>
1915
);
2016
}
2117

examples/HelloWorldExpo/Counter.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { useState } from "react";
2+
import { Button, Text, View } from "react-native";
3+
import { useAptabase } from "@aptabase/react-native";
4+
5+
export function Counter() {
6+
const { trackEvent } = useAptabase();
7+
const [count, setCount] = useState(0);
8+
9+
const increment = () => {
10+
trackEvent("increment");
11+
setCount(count + 1);
12+
};
13+
14+
const decrement = () => {
15+
trackEvent("decrement");
16+
setCount(count - 1);
17+
};
18+
19+
return (
20+
<View>
21+
<Button onPress={increment} title="Increment" />
22+
<Button onPress={decrement} title="Decrement" />
23+
<Text>Count is {count}</Text>
24+
</View>
25+
);
26+
}

package-lock.json

Lines changed: 32 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@aptabase/react-native",
3-
"version": "0.2.2",
3+
"version": "0.3.0",
44
"private": false,
55
"description": "React Native SDK for Aptabase: Open Source, Privacy-First and Simple Analytics for Mobile, Desktop and Web Apps",
66
"sideEffects": false,
@@ -39,6 +39,7 @@
3939
],
4040
"devDependencies": {
4141
"@vitest/coverage-v8": "0.34.3",
42+
"@types/react": "18.2.22",
4243
"@types/node": "20.5.9",
4344
"tsup": "7.2.0",
4445
"vite": "4.4.9",

src/context.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { init, trackEvent } from "./track";
2+
import { createContext, useEffect, type ReactNode } from "react";
3+
import { AptabaseOptions } from "./types";
4+
5+
type ContextProps = {};
6+
7+
export type AptabaseClient = {
8+
trackEvent: typeof trackEvent;
9+
};
10+
11+
const AptabaseContext = createContext<ContextProps>({});
12+
13+
type Props = {
14+
appKey: string;
15+
options?: AptabaseOptions;
16+
children: ReactNode;
17+
};
18+
19+
export function AptabaseProvider({ appKey, options, children }: Props) {
20+
useEffect(() => {
21+
init(appKey, options);
22+
}, [appKey, options]);
23+
24+
return (
25+
<AptabaseContext.Provider value={{}}>{children}</AptabaseContext.Provider>
26+
);
27+
}
28+
29+
export function useAptabase(): AptabaseClient {
30+
return { trackEvent };
31+
}

src/index.ts

Lines changed: 3 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,3 @@
1-
import type { AptabaseOptions } from "./types";
2-
import { getEnvironmentInfo } from "./env";
3-
import { AppState, Platform } from "react-native";
4-
import { AptabaseClient } from "./client";
5-
import { FLUSH_INTERVAL } from "./constants";
6-
import { validate } from "./validate";
7-
8-
export type { AptabaseOptions };
9-
10-
let _client: AptabaseClient | undefined;
11-
12-
/**
13-
* Initializes the SDK with given App Key
14-
* @param {string} appKey - Aptabase App Key
15-
* @param {AptabaseOptions} options - Optional initialization parameters
16-
*/
17-
export function init(appKey: string, options?: AptabaseOptions) {
18-
const [ok, msg] = validate(Platform.OS, appKey, options);
19-
if (!ok) {
20-
console.warn(`Aptabase: ${msg}. Tracking will be disabled.`);
21-
return;
22-
}
23-
24-
const env = getEnvironmentInfo();
25-
_client = new AptabaseClient(appKey, env, options);
26-
27-
const flushInterval = options?.flushInterval ?? FLUSH_INTERVAL;
28-
_client.startPolling(flushInterval);
29-
30-
if (!AppState.isAvailable) return;
31-
32-
AppState.addEventListener("change", (next) => {
33-
_client?.stopPolling();
34-
35-
switch (next) {
36-
case "active":
37-
_client?.startPolling(flushInterval);
38-
break;
39-
40-
case "background":
41-
_client?.flush();
42-
break;
43-
}
44-
});
45-
}
46-
47-
/**
48-
* Track an event using given properties
49-
* @param {string} eventName - The name of the event to track
50-
* @param {Object} props - Optional custom properties
51-
*/
52-
export function trackEvent(
53-
eventName: string,
54-
props?: Record<string, string | number | boolean>
55-
) {
56-
_client?.trackEvent(eventName, props);
57-
}
1+
export type { AptabaseOptions } from "./types";
2+
export { AptabaseProvider, useAptabase } from "./context";
3+
export { init, trackEvent } from "./track";

src/track.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import type { AptabaseOptions } from "./types";
2+
import { getEnvironmentInfo } from "./env";
3+
import { AppState, Platform } from "react-native";
4+
import { AptabaseClient } from "./client";
5+
import { FLUSH_INTERVAL } from "./constants";
6+
import { validate } from "./validate";
7+
8+
let _client: AptabaseClient | undefined;
9+
10+
/**
11+
* Initializes the SDK with given App Key
12+
* @param {string} appKey - Aptabase App Key
13+
* @param {AptabaseOptions} options - Optional initialization parameters
14+
*/
15+
export function init(appKey: string, options?: AptabaseOptions) {
16+
const [ok, msg] = validate(Platform.OS, appKey, options);
17+
if (!ok) {
18+
console.warn(`Aptabase: ${msg}. Tracking will be disabled.`);
19+
return;
20+
}
21+
22+
const env = getEnvironmentInfo();
23+
_client = new AptabaseClient(appKey, env, options);
24+
25+
const flushInterval = options?.flushInterval ?? FLUSH_INTERVAL;
26+
_client.startPolling(flushInterval);
27+
28+
if (!AppState.isAvailable) return;
29+
30+
AppState.addEventListener("change", (next) => {
31+
_client?.stopPolling();
32+
33+
switch (next) {
34+
case "active":
35+
_client?.startPolling(flushInterval);
36+
break;
37+
38+
case "background":
39+
_client?.flush();
40+
break;
41+
}
42+
});
43+
}
44+
45+
/**
46+
* Track an event using given properties
47+
* @param {string} eventName - The name of the event to track
48+
* @param {Object} props - Optional custom properties
49+
*/
50+
export function trackEvent(
51+
eventName: string,
52+
props?: Record<string, string | number | boolean>
53+
) {
54+
_client?.trackEvent(eventName, props);
55+
}

tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"importHelpers": true,
77
"outDir": "dist",
88
"strict": true,
9-
"jsx": "preserve",
9+
"jsx": "react-jsx",
1010
"allowJs": true,
1111
"sourceMap": true,
1212
"resolveJsonModule": true,
@@ -20,5 +20,5 @@
2020
},
2121
"lib": ["esnext", "dom"]
2222
},
23-
"include": ["src/index.ts"]
23+
"include": ["src/*.ts", "src/*.tsx"]
2424
}

0 commit comments

Comments
 (0)