Skip to content

Commit

Permalink
chore: android plugin (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto authored Sep 18, 2024
1 parent b221e82 commit ba07b2e
Show file tree
Hide file tree
Showing 13 changed files with 181 additions and 599 deletions.
26 changes: 1 addition & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,9 @@
# posthog-react-native-session-replay

Session Replay
Session Replay for React Native (Android and iOS)

## Installation

```sh
npm install posthog-react-native-session-replay
```

## Usage


```js
import { multiply } from 'posthog-react-native-session-replay';

// ...

const result = await multiply(3, 7);
```


## Contributing

See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.

## License

MIT

---

Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob)
13 changes: 13 additions & 0 deletions RELEASING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Releasing
=========

1. Update the CHANGELOG.md with the version and date
2. Update the version in the package.json file
3. Choose a tag name (e.g. `3.0.0`), this is the version number of the release.
1. Preview releases follow the pattern `3.0.0-alpha.1`, `3.0.0-beta.1`, `3.0.0-RC.1`
4. Go to [GH Releases](https://github.com/PostHog/posthog-android/releases)
5. Choose a release name (e.g. `3.0.0`), and the tag you just created, ideally the same.
6. Write a description of the release.
7. Publish the release (npm publish).
8. Done

1 change: 1 addition & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,6 @@ dependencies {
//noinspection GradleDynamicVersion
implementation "com.facebook.react:react-native:+"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "com.posthog:posthog-android:3.7.3"
}

8 changes: 4 additions & 4 deletions android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PosthogReactNativeSessionReplay_kotlinVersion=1.7.0
PosthogReactNativeSessionReplay_kotlinVersion=1.8.0
PosthogReactNativeSessionReplay_minSdkVersion=21
PosthogReactNativeSessionReplay_targetSdkVersion=31
PosthogReactNativeSessionReplay_compileSdkVersion=31
PosthogReactNativeSessionReplay_ndkversion=21.4.7075529
PosthogReactNativeSessionReplay_targetSdkVersion=34
PosthogReactNativeSessionReplay_compileSdkVersion=34
PosthogReactNativeSessionReplay_ndkversion=25.1.8937393
2 changes: 1 addition & 1 deletion android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReadableMap;

import com.posthog.PostHog
import com.posthog.PostHogConfig
import com.posthog.android.PostHogAndroid
import com.posthog.android.PostHogAndroidConfig
import com.posthog.internal.PostHogSessionManager
import java.util.UUID

class PosthogReactNativeSessionReplayModule(reactContext: ReactApplicationContext) :
ReactContextBaseJavaModule(reactContext) {
Expand All @@ -19,6 +27,63 @@ class PosthogReactNativeSessionReplayModule(reactContext: ReactApplicationContex
promise.resolve(a * b)
}

@ReactMethod
fun start(sessionId: String, sdkOptions: ReadableMap, sdkReplayConfig: ReadableMap, decideReplayConfig: ReadableMap, promise: Promise) {
val uuid = UUID.fromString(sessionId)
PostHogSessionManager.setSessionId(uuid)

val context = this.reactApplicationContext
val apiKey = sdkOptions.getString("apiKey") ?: ""
val host = sdkOptions.getString("host") ?: PostHogConfig.DEFAULT_HOST
val debugValue = sdkOptions.getBoolean("debug")

val maskAllTextInputs = sdkReplayConfig.getBoolean("maskAllTextInputs")
val maskAllImages = sdkReplayConfig.getBoolean("maskAllImages")
val captureLog = sdkReplayConfig.getBoolean("captureLog")
val debouncerDelayMs = sdkReplayConfig.getInt("androidDebouncerDelayMs")

val endpoint = decideReplayConfig.getString("endpoint")

val config = PostHogAndroidConfig(apiKey, host).apply {
debug = debugValue
captureDeepLinks = false
captureApplicationLifecycleEvents = false
captureScreenViews = false
sessionReplay = true
sessionReplayConfig.screenshot = true
sessionReplayConfig.captureLogcat = captureLog
sessionReplayConfig.debouncerDelayMs = debouncerDelayMs.toLong()
sessionReplayConfig.maskAllImages = maskAllImages
sessionReplayConfig.maskAllTextInputs = maskAllTextInputs

if (!endpoint.isNullOrEmpty()) {
snapshotEndpoint = endpoint
}
}
PostHogAndroid.setup(context, config)

promise.resolve(null)
}

@ReactMethod
fun startSession(sessionId: String, promise: Promise) {
val uuid = UUID.fromString(sessionId)
PostHogSessionManager.setSessionId(uuid)
PostHog.startSession()
promise.resolve(null)
}

@ReactMethod
fun isEnabled(promise: Promise) {
promise.resolve(PostHog.isSessionReplayActive())
}

@ReactMethod
fun endSession(promise: Promise) {
PostHog.endSession()
promise.resolve(null)
}

companion object {
const val NAME = "PosthogReactNativeSessionReplay"
}
Expand Down
4 changes: 2 additions & 2 deletions example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ buildscript {
minSdkVersion = 23
compileSdkVersion = 34
targetSdkVersion = 34
ndkVersion = "26.1.10909125"
kotlinVersion = "1.9.24"
ndkVersion = "25.1.8937393"
kotlinVersion = "1.8.0"
}
repositories {
google()
Expand Down
64 changes: 60 additions & 4 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,68 @@
import { useState, useEffect } from 'react';
import { StyleSheet, View, Text } from 'react-native';
import { multiply } from 'posthog-react-native-session-replay';
import { StyleSheet, View, Text, Platform } from 'react-native';
import type PostHogReactNativeSessionReplay from 'posthog-react-native-session-replay';

export let OptionalReactNativeSessionReplay:
| typeof PostHogReactNativeSessionReplay
| undefined;

try {
OptionalReactNativeSessionReplay = Platform.select({
macos: undefined,
web: undefined,
default: require('posthog-react-native-session-replay'), // Only Android and iOS
});
} catch (e) {
// do nothing
console.warn(
'PostHog Debug',
`Error loading posthog-react-native-session-replay: ${e}`
);
}

export default function App() {
const [result, setResult] = useState<number | undefined>();
const [result, setResult] = useState<string | undefined>();

useEffect(() => {
multiply(3, 7).then(setResult);
if (OptionalReactNativeSessionReplay) {
setResult('ok');
// OptionalReactNativeSessionReplay.isEnabled().then((isEnabled) => {
// console.warn('PostHog Debug', `isEnabled: ${isEnabled}`);
// setResult(isEnabled.valueOf().toString());
// });
// OptionalReactNativeSessionReplay.startSession(
// 'e58ed763-928c-4155-bee9-fdbaaadc15f3'
// )
// .then(() => {
// setResult('ok');
// })
// .catch(() => {
// setResult('failed');
// });
// OptionalReactNativeSessionReplay.start(
// 'e58ed763-928c-4155-bee9-fdbaaadc15f3',
// {
// apiKey: 'phc_QFbR1y41s5sxnNTZoyKG2NJo2RlsCIWkUfdpawgb40D',
// host: 'https://us.i.posthog.com',
// },
// {},
// {}
// )
// .then(() => {
// OptionalReactNativeSessionReplay?.isEnabled().then((isEnabled) => {
// console.warn('PostHog Debug', `isEnabled: ${isEnabled}`);
// setResult(`isEnabled: ${isEnabled}`);
// });
// })
// .then(() => {
// setResult('ok');
// })
// .catch(() => {
// setResult('failed');
// });
} else {
console.warn('PostHog Debug', `meh`);
}
}, []);

return (
Expand Down
5 changes: 0 additions & 5 deletions lefthook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,3 @@ pre-commit:
types:
glob: "*.{js,ts, jsx, tsx}"
run: npx tsc
commit-msg:
parallel: true
commands:
commitlint:
run: npx commitlint --edit
17 changes: 5 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "posthog-react-native-session-replay",
"version": "0.0.4",
"version": "0.0.11",
"description": "Session Replay",
"source": "./src/index.tsx",
"main": "./lib/commonjs/index.js",
Expand Down Expand Up @@ -51,25 +51,23 @@
],
"repository": {
"type": "git",
"url": "git+https://github.com/marandaneto/posthog-react-native-session-replay.git"
"url": "git+https://github.com/PostHog/posthog-react-native-session-replay.git"
},
"author": "Manoel Aranda Neto <marandaneto@gmail.com> (https://github.com/marandaneto)",
"author": "PostHog <engineering@posthog.com> (https://github.com/PostHog)",
"license": "MIT",
"bugs": {
"url": "https://github.com/marandaneto/posthog-react-native-session-replay/issues"
"url": "https://github.com/PostHog/posthog-react-native-session-replay/issues"
},
"homepage": "https://github.com/marandaneto/posthog-react-native-session-replay#readme",
"homepage": "https://github.com/PostHog/posthog-react-native-session-replay#readme",
"publishConfig": {
"registry": "https://registry.npmjs.org/"
},
"devDependencies": {
"@commitlint/config-conventional": "^17.0.2",
"@evilmartians/lefthook": "^1.5.0",
"@react-native/eslint-config": "^0.73.1",
"@release-it/conventional-changelog": "^5.0.0",
"@types/jest": "^29.5.5",
"@types/react": "^18.2.44",
"commitlint": "^17.0.2",
"del-cli": "^5.1.0",
"eslint": "^8.51.0",
"eslint-config-prettier": "^9.0.0",
Expand Down Expand Up @@ -101,11 +99,6 @@
"<rootDir>/lib/"
]
},
"commitlint": {
"extends": [
"@commitlint/config-conventional"
]
},
"release-it": {
"git": {
"commitMessage": "chore: release ${version}",
Expand Down
2 changes: 1 addition & 1 deletion posthog-react-native-session-replay.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Pod::Spec.new do |s|
s.authors = package["author"]

s.platforms = { :ios => min_ios_version_supported }
s.source = { :git => "https://github.com/marandaneto/posthog-react-native-session-replay.git", :tag => "#{s.version}" }
s.source = { :git => "https://github.com/PostHog/posthog-react-native-session-replay.git", :tag => "#{s.version}" }

s.source_files = "ios/**/*.{h,m,mm,swift}"

Expand Down
17 changes: 12 additions & 5 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,16 @@ export function multiply(a: number, b: number): Promise<number> {

export function start(
sessionId: string,
options: { [key: string]: any },
sdkOptions: { [key: string]: any }
sdkOptions: { [key: string]: any },
sdkReplayConfig: { [key: string]: any },
decideReplayConfig: { [key: string]: any }
): Promise<void> {
return PosthogReactNativeSessionReplay.start(sessionId, options, sdkOptions);
return PosthogReactNativeSessionReplay.start(
sessionId,
sdkOptions,
sdkReplayConfig,
decideReplayConfig
);
}

export function startSession(sessionId: string): Promise<void> {
Expand All @@ -45,8 +51,9 @@ export function isEnabled(): Promise<boolean> {
export interface PostHogReactNativeSessionReplayModule {
start: (
sessionId: string,
options: { [key: string]: any },
sdkOptions: { [key: string]: any }
sdkOptions: { [key: string]: any }, // options from SDK such as apiKey
sdkReplayConfig: { [key: string]: any }, // config from SDK
decideReplayConfig: { [key: string]: any } // config from Decide API
) => Promise<void>;

startSession: (sessionId: string) => Promise<void>;
Expand Down
Loading

0 comments on commit ba07b2e

Please sign in to comment.