Skip to content

Commit 5fe49e3

Browse files
Add React Native tracker example (#60)
* Add React Native tracker example * Update dev version * Update version and add webview tracking * Update readme * Remove unused imports * Add link to example from main readme
1 parent 5b3aeec commit 5fe49e3

34 files changed

+16524
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44

55
- [Angular](angular/ngSnowplow)
66
- [React](react)
7+
- [React Native](react-native-example)
78
- [Next.js](nextjs)

react-native-example/.gitignore

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
node_modules/
2+
.expo/
3+
dist/
4+
npm-debug.*
5+
*.jks
6+
*.p8
7+
*.p12
8+
*.key
9+
*.mobileprovision
10+
*.orig.*
11+
web-build/
12+
13+
# macOS
14+
.DS_Store
15+
16+
# @generated expo-cli sync-2b81b286409207a5da26e14c78851eb30d8ccbdb
17+
# The following patterns were generated by expo-cli
18+
19+
expo-env.d.ts
20+
# @end expo-cli

react-native-example/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Snowplow React Native Tracker Example App
2+
3+
This app shows how the Snowplow React Native Tracker v4 can be used in React Native apps.
4+
5+
## Get started
6+
7+
1. Install dependencies
8+
9+
```bash
10+
npm install
11+
```
12+
13+
2. Start the app
14+
15+
```bash
16+
npx expo start
17+
```
18+
19+
In the output, you'll find options to open the app in a
20+
21+
- [development build](https://docs.expo.dev/develop/development-builds/introduction/)
22+
- [Android emulator](https://docs.expo.dev/workflow/android-studio-emulator/)
23+
- [iOS simulator](https://docs.expo.dev/workflow/ios-simulator/)
24+
- [Expo Go](https://expo.dev/go), a limited sandbox for trying out app development with Expo

react-native-example/app.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"expo": {
3+
"name": "rn-test",
4+
"slug": "rn-test",
5+
"version": "1.0.0",
6+
"orientation": "portrait",
7+
"icon": "./assets/images/icon.png",
8+
"scheme": "myapp",
9+
"userInterfaceStyle": "automatic",
10+
"splash": {
11+
"image": "./assets/images/splash.png",
12+
"resizeMode": "contain",
13+
"backgroundColor": "#ffffff"
14+
},
15+
"ios": {
16+
"supportsTablet": true
17+
},
18+
"android": {
19+
"adaptiveIcon": {
20+
"foregroundImage": "./assets/images/adaptive-icon.png",
21+
"backgroundColor": "#ffffff"
22+
}
23+
},
24+
"web": {
25+
"bundler": "metro",
26+
"output": "static",
27+
"favicon": "./assets/images/favicon.png"
28+
},
29+
"plugins": [
30+
"expo-router"
31+
],
32+
"experiments": {
33+
"typedRoutes": true
34+
}
35+
}
36+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Stack } from "expo-router";
2+
3+
export default function RootLayout() {
4+
return (
5+
<Stack>
6+
<Stack.Screen name="index" />
7+
</Stack>
8+
);
9+
}

react-native-example/app/index.tsx

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { Button, Text, View } from "react-native";
2+
import {
3+
trackPageView,
4+
trackScreenView,
5+
trackScrollChanged,
6+
trackStructEvent,
7+
trackProductViewEvent,
8+
trackTransactionEvent,
9+
} from "./tracking";
10+
import { useEffect } from "react";
11+
import { WebView } from "react-native-webview";
12+
import { getWebViewCallback } from "@snowplow/react-native-tracker";
13+
import { Dimensions } from "react-native";
14+
15+
const webViewEndpoint = "";
16+
17+
export default function Index() {
18+
useEffect(() => {
19+
trackScreenView("Home");
20+
});
21+
22+
return (
23+
<View
24+
style={{
25+
flex: 1,
26+
justifyContent: "center",
27+
alignItems: "center",
28+
}}
29+
>
30+
<Text style={{ fontWeight: "bold", fontSize: 20 }}>Basic Events</Text>
31+
<Button
32+
onPress={trackStructEvent}
33+
title="Track a structured event"
34+
></Button>
35+
<Button onPress={trackPageView} title="Track a page view event"></Button>
36+
<Button
37+
onPress={() => trackScreenView("Other")}
38+
title="Track a screen view event"
39+
></Button>
40+
<Button
41+
onPress={() => trackScrollChanged()}
42+
title="Track a scroll changed event"
43+
></Button>
44+
<Text style={{ fontWeight: "bold", fontSize: 20 }}>
45+
E-commerce Events
46+
</Text>
47+
<Button
48+
onPress={() => trackProductViewEvent()}
49+
title="Track a product view"
50+
></Button>
51+
<Button
52+
onPress={() => trackTransactionEvent()}
53+
title="Track a transaction"
54+
></Button>
55+
56+
{webViewEndpoint ? (
57+
<WebView
58+
onMessage={getWebViewCallback()}
59+
source={{ uri: webViewEndpoint }}
60+
style={{
61+
width: Dimensions.get("window").width,
62+
}}
63+
/>
64+
) : null}
65+
</View>
66+
);
67+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { newTracker, ReactNativeTracker } from "@snowplow/react-native-tracker";
2+
import { SnowplowEcommercePlugin, trackProductView, trackTransaction } from "@snowplow/browser-plugin-snowplow-ecommerce";
3+
4+
let tracker: ReactNativeTracker | undefined;
5+
export async function getTracker() {
6+
if (tracker) {
7+
return tracker;
8+
}
9+
tracker = await newTracker({
10+
namespace: "sp1",
11+
appId: "my-app",
12+
appVersion: "0.1.0",
13+
endpoint: "http://0.0.0.0:9090",
14+
installAutotracking: true,
15+
plugins: [SnowplowEcommercePlugin()],
16+
});
17+
return tracker;
18+
}
19+
20+
export async function trackScreenView(name: string) {
21+
const tracker = await getTracker();
22+
tracker.trackScreenViewEvent({ name });
23+
}
24+
25+
export async function trackPageView() {
26+
const tracker = await getTracker();
27+
tracker.trackPageViewEvent({
28+
pageUrl: "https://snowplow.io",
29+
pageTitle: "My Page",
30+
});
31+
}
32+
33+
export async function trackStructEvent() {
34+
const tracker = await getTracker();
35+
tracker.trackStructuredEvent({
36+
category: "cat",
37+
action: "act",
38+
label: "lbl",
39+
});
40+
}
41+
42+
export async function trackScrollChanged() {
43+
const tracker = await getTracker();
44+
tracker.trackScrollChangedEvent({
45+
yOffset: 100,
46+
xOffset: 200,
47+
});
48+
}
49+
50+
export function trackProductViewEvent() {
51+
trackProductView({
52+
id: "product-1",
53+
price: 100,
54+
currency: "USD",
55+
name: "Product 1",
56+
category: "Category 1",
57+
})
58+
}
59+
60+
export function trackTransactionEvent() {
61+
trackTransaction({
62+
currency: "USD",
63+
transaction_id: "transaction-1",
64+
revenue: 500,
65+
payment_method: "Visa",
66+
products: [
67+
{
68+
id: "product-1",
69+
price: 100,
70+
quantity: 1,
71+
currency: "USD",
72+
category: "Category 1",
73+
},
74+
{
75+
id: "product-2",
76+
price: 200,
77+
quantity: 2,
78+
currency: "USD",
79+
category: "Category 2",
80+
},
81+
],
82+
});
83+
}
91.1 KB
Binary file not shown.
17.1 KB
Loading
1.43 KB
Loading

0 commit comments

Comments
 (0)