-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
198 additions
and
86 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
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,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> | ||
); | ||
} |
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
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 }; | ||
} |
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,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"; |
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,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); | ||
} |
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