Skip to content

Commit

Permalink
release 0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
goenning committed Sep 20, 2023
1 parent 5f097b4 commit 19f6f8a
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 86 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.3.0

- Added a new `AptabaseProvider` and `useAptabase` hook to make usage easier

## 0.2.2

- Fix bundled file names
Expand Down
43 changes: 33 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,45 @@ npm add @aptabase/react-native

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

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

```js
import { init } from "@aptabase/react-native";

init("<YOUR_APP_KEY>"); // 👈 this is where you enter your App Key
export default function App() {
return (
<AptabaseProvider appKey="<YOUR_APP_KEY>">
<Counter />
</AptabaseProvider>
);
}
```

Afterwards, you can start tracking events with `trackEvent`:
Afterwards, you can start tracking events with `trackEvent` from `useAptabase` hook:

```js
import { trackEvent } from "@aptabase/react-native";

trackEvent("app_started"); // An event with no properties
trackEvent("screen_view", { name: "Settings" }); // An event with a custom property
import { useAptabase } from "@aptabase/react-native";

export function Counter() {
const { trackEvent } = useAptabase();
const [count, setCount] = useState(0);

const increment = () => {
setCount(count + 1);
trackEvent("increment", { count });
};

const decrement = () => {
setCount(count - 1);
trackEvent("decrement", { count });
};

return (
<View>
<Button onPress={increment} title="Increment" />
<Button onPress={decrement} title="Decrement" />
<Text>Count is {count}</Text>
</View>
);
}
```

**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.
Expand All @@ -46,4 +70,3 @@ A few important notes:
## Preparing for Submission to Apple App Store

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.

24 changes: 10 additions & 14 deletions examples/HelloWorldExpo/App.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import { StatusBar } from "expo-status-bar";
import { Button, StyleSheet, Text, View } from "react-native";
import { init, trackEvent } from "@aptabase/react-native";

init("A-DEV-0000000000");
trackEvent("app_started");
import { StyleSheet, Text, View } from "react-native";
import { AptabaseProvider } from "@aptabase/react-native";
import { Counter } from "./Counter";

export default function App() {
const onClick = () => {
trackEvent("Hello");
};

return (
<View style={styles.container}>
<Text>Open up App.js to start working on your app!</Text>
<Button onPress={onClick} title="Click Me" color="#841584" />
<StatusBar style="auto" />
</View>
<AptabaseProvider appKey="A-US-0928558097">
<View style={styles.container}>
<Text>Open up App.js to start working on your app!</Text>
<Counter />
<StatusBar style="auto" />
</View>
</AptabaseProvider>
);
}

Expand Down
26 changes: 26 additions & 0 deletions examples/HelloWorldExpo/Counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useState } from "react";
import { Button, Text, View } from "react-native";
import { useAptabase } from "@aptabase/react-native";

export function Counter() {
const { trackEvent } = useAptabase();
const [count, setCount] = useState(0);

const increment = () => {
trackEvent("increment");
setCount(count + 1);
};

const decrement = () => {
trackEvent("decrement");
setCount(count - 1);
};

return (
<View>
<Button onPress={increment} title="Increment" />
<Button onPress={decrement} title="Decrement" />
<Text>Count is {count}</Text>
</View>
);
}
34 changes: 32 additions & 2 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": "@aptabase/react-native",
"version": "0.2.2",
"version": "0.3.0",
"private": false,
"description": "React Native SDK for Aptabase: Open Source, Privacy-First and Simple Analytics for Mobile, Desktop and Web Apps",
"sideEffects": false,
Expand Down Expand Up @@ -39,6 +39,7 @@
],
"devDependencies": {
"@vitest/coverage-v8": "0.34.3",
"@types/react": "18.2.22",
"@types/node": "20.5.9",
"tsup": "7.2.0",
"vite": "4.4.9",
Expand Down
31 changes: 31 additions & 0 deletions src/context.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { init, trackEvent } from "./track";
import { createContext, useEffect, type ReactNode } from "react";
import { AptabaseOptions } from "./types";

type ContextProps = {};

export type AptabaseClient = {
trackEvent: typeof trackEvent;
};

const AptabaseContext = createContext<ContextProps>({});

type Props = {
appKey: string;
options?: AptabaseOptions;
children: ReactNode;
};

export function AptabaseProvider({ appKey, options, children }: Props) {
useEffect(() => {
init(appKey, options);
}, [appKey, options]);

return (
<AptabaseContext.Provider value={{}}>{children}</AptabaseContext.Provider>
);
}

export function useAptabase(): AptabaseClient {
return { trackEvent };
}
60 changes: 3 additions & 57 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,3 @@
import type { AptabaseOptions } from "./types";
import { getEnvironmentInfo } from "./env";
import { AppState, Platform } from "react-native";
import { AptabaseClient } from "./client";
import { FLUSH_INTERVAL } from "./constants";
import { validate } from "./validate";

export type { AptabaseOptions };

let _client: AptabaseClient | undefined;

/**
* Initializes the SDK with given App Key
* @param {string} appKey - Aptabase App Key
* @param {AptabaseOptions} options - Optional initialization parameters
*/
export function init(appKey: string, options?: AptabaseOptions) {
const [ok, msg] = validate(Platform.OS, appKey, options);
if (!ok) {
console.warn(`Aptabase: ${msg}. Tracking will be disabled.`);
return;
}

const env = getEnvironmentInfo();
_client = new AptabaseClient(appKey, env, options);

const flushInterval = options?.flushInterval ?? FLUSH_INTERVAL;
_client.startPolling(flushInterval);

if (!AppState.isAvailable) return;

AppState.addEventListener("change", (next) => {
_client?.stopPolling();

switch (next) {
case "active":
_client?.startPolling(flushInterval);
break;

case "background":
_client?.flush();
break;
}
});
}

/**
* Track an event using given properties
* @param {string} eventName - The name of the event to track
* @param {Object} props - Optional custom properties
*/
export function trackEvent(
eventName: string,
props?: Record<string, string | number | boolean>
) {
_client?.trackEvent(eventName, props);
}
export type { AptabaseOptions } from "./types";
export { AptabaseProvider, useAptabase } from "./context";
export { init, trackEvent } from "./track";
55 changes: 55 additions & 0 deletions src/track.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import type { AptabaseOptions } from "./types";
import { getEnvironmentInfo } from "./env";
import { AppState, Platform } from "react-native";
import { AptabaseClient } from "./client";
import { FLUSH_INTERVAL } from "./constants";
import { validate } from "./validate";

let _client: AptabaseClient | undefined;

/**
* Initializes the SDK with given App Key
* @param {string} appKey - Aptabase App Key
* @param {AptabaseOptions} options - Optional initialization parameters
*/
export function init(appKey: string, options?: AptabaseOptions) {
const [ok, msg] = validate(Platform.OS, appKey, options);
if (!ok) {
console.warn(`Aptabase: ${msg}. Tracking will be disabled.`);
return;
}

const env = getEnvironmentInfo();
_client = new AptabaseClient(appKey, env, options);

const flushInterval = options?.flushInterval ?? FLUSH_INTERVAL;
_client.startPolling(flushInterval);

if (!AppState.isAvailable) return;

AppState.addEventListener("change", (next) => {
_client?.stopPolling();

switch (next) {
case "active":
_client?.startPolling(flushInterval);
break;

case "background":
_client?.flush();
break;
}
});
}

/**
* Track an event using given properties
* @param {string} eventName - The name of the event to track
* @param {Object} props - Optional custom properties
*/
export function trackEvent(
eventName: string,
props?: Record<string, string | number | boolean>
) {
_client?.trackEvent(eventName, props);
}
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"importHelpers": true,
"outDir": "dist",
"strict": true,
"jsx": "preserve",
"jsx": "react-jsx",
"allowJs": true,
"sourceMap": true,
"resolveJsonModule": true,
Expand All @@ -20,5 +20,5 @@
},
"lib": ["esnext", "dom"]
},
"include": ["src/index.ts"]
"include": ["src/*.ts", "src/*.tsx"]
}

0 comments on commit 19f6f8a

Please sign in to comment.