reactnative-plugin-appice 1.7.16 → 1.7.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/example/App.js CHANGED
@@ -21,6 +21,9 @@ import { getInboxMessages, getMessageCounts, setUser, updatedInboxMessage } from
21
21
  import { handleUrl,receiveDeepLink } from './offerIdhandler';
22
22
  const AppICE = require('reactnative-plugin-appice');
23
23
  const HelloWorldApp = () => {
24
+
25
+
26
+
24
27
  const certs: never[] = [];
25
28
  if (AppICE) {
26
29
  console.log('AppICE is available');
@@ -37,17 +40,16 @@ const HelloWorldApp = () => {
37
40
  certs
38
41
  );
39
42
 
40
- addAppICEAPIListeners();
41
-
42
- AppState.addEventListener('change', (nextAppState) => {
43
- if (nextAppState === 'active') {
44
- AppICE.isDeviceReady(true);
45
- }
46
- else if (nextAppState === 'background'){
47
- AppICE.isDeviceReady(false);
48
- }
49
- });
50
-
43
+ addAppICEAPIListeners();
44
+
45
+ AppState.addEventListener('change', (nextAppState) => {
46
+ if (nextAppState === 'active') {
47
+ AppICE.registerLifeCycle()
48
+
49
+ AppICE.isDeviceReady(true);
50
+ }
51
+ });
52
+
51
53
  }
52
54
 
53
55
 
@@ -76,6 +78,7 @@ const HelloWorldApp = () => {
76
78
  <Button onPress={synchronizeInbox} title="synchronizeInbox" color="#841584" />
77
79
  <Button onPress={getUserDetails} title="getUserDetails" color="#841584" />
78
80
  <Button onPress={getUserData} title="getUserData" color="#841584" />
81
+
79
82
  </View>
80
83
  );
81
84
 
@@ -0,0 +1,16 @@
1
+ import React from 'react';
2
+ import { View, Text, Button } from 'react-native';
3
+ import { usePageTracking } from './usePageTracking';
4
+
5
+ const PageA = ({ navigation }: { navigation: any }) => {
6
+ usePageTracking('A', navigation);
7
+
8
+ return (
9
+ <View>
10
+ <Text>Page A</Text>
11
+ <Button title="Go to Page B" onPress={() => navigation.navigate('PageB')} />
12
+ </View>
13
+ );
14
+ };
15
+
16
+ export default PageA;
@@ -4,10 +4,10 @@ import org.apache.tools.ant.taskdefs.condition.Os
4
4
 
5
5
  buildscript {
6
6
  ext {
7
- buildToolsVersion = "31.0.0"
7
+ buildToolsVersion = "34.0.0"
8
8
  minSdkVersion = 21
9
- compileSdkVersion = 31
10
- targetSdkVersion = 31
9
+ compileSdkVersion = 34
10
+ targetSdkVersion = 34
11
11
 
12
12
  if (System.properties['os.arch'] == "aarch64") {
13
13
  // For M1 Users we need to use the NDK 24 which added support for aarch64
@@ -17,7 +17,7 @@
17
17
  "install": "^0.13.0",
18
18
  "react": "18.0.0",
19
19
  "react-native": "0.69.4",
20
- "reactnative-plugin-appice": "1.7.13",
20
+ "reactnative-plugin-appice": "1.7.16",
21
21
  "react-native-mmkv": "2.12.2"
22
22
  },
23
23
  "devDependencies": {
@@ -0,0 +1,88 @@
1
+ import { useState, useEffect, useRef } from 'react';
2
+ import { AppState, AppStateStatus } from 'react-native';
3
+ import AppICE from 'reactnative-plugin-appice';
4
+
5
+ /**
6
+ * usePageTracking
7
+ * PageView Event - The PageView event is used to Identify the moment a user views or lands
8
+ on a particular page or screen in your application.
9
+ * PageDuration Event - The PageDuration event measures the amount of time a user spends
10
+ on a particular page or screen
11
+
12
+ */
13
+
14
+ export const usePageTracking = (pageName: string, navigation: any) => {
15
+ const [startTime, setStartTime] = useState<number | null>(null);
16
+ const appState = useRef<AppStateStatus>(AppState.currentState);
17
+ const isPageFocused = useRef(false); // Track if the page is currently focused
18
+ const isForeground = useRef(true); // Track if the app is in the foreground
19
+
20
+ useEffect(() => {
21
+ // Function to trigger PageView event
22
+ const triggerPageView = () => {
23
+ if (!isPageFocused.current) { // Ensure the PageView is triggered only once
24
+ setStartTime(Date.now());
25
+ const dataObj = { view: pageName };
26
+ console.log(`PageView event triggered for page: ${pageName}`);
27
+ AppICE.tagEvent('PageView', dataObj);
28
+ isPageFocused.current = true;
29
+ }
30
+ };
31
+
32
+ // Function to trigger PageDuration event
33
+ const triggerPageDuration = () => {
34
+ if (isPageFocused.current && startTime) {
35
+ const endTime = Date.now();
36
+ const totalSeconds = Math.floor((endTime - startTime) / 1000);
37
+ const dataObj = { name: pageName, duration: totalSeconds };
38
+ console.log(`PageDuration event triggered for page: ${pageName}, duration: ${totalSeconds}s`);
39
+ AppICE.tagEvent('PageDuration', dataObj);
40
+ setStartTime(null); // Reset the start time
41
+ isPageFocused.current = false;
42
+ }
43
+ };
44
+
45
+ // Handle focus and blur events
46
+ const handleFocus = () => {
47
+ console.log(`Page ${pageName} focused`);
48
+ if (isForeground.current) {
49
+ triggerPageView(); // Trigger PageView only when the app is in the foreground
50
+ }
51
+ };
52
+
53
+ const handleBlur = () => {
54
+ console.log(`Page ${pageName} lost focus`);
55
+ triggerPageDuration(); // Trigger PageDuration when leaving the page
56
+ };
57
+
58
+ const unsubscribeFocus = navigation.addListener('focus', handleFocus);
59
+ const unsubscribeBlur = navigation.addListener('blur', handleBlur);
60
+
61
+ // Handle app state changes (background/foreground)
62
+ const handleAppStateChange = (nextAppState: AppStateStatus) => {
63
+ console.log(`AppState changed from ${appState.current} to ${nextAppState}`);
64
+ if (appState.current.match(/active|inactive/) && nextAppState === 'background') {
65
+ triggerPageDuration(); // App went to background
66
+ isForeground.current = false;
67
+ } else if (appState.current === 'background' && nextAppState === 'active') {
68
+ isForeground.current = true;
69
+ if (isPageFocused.current) {
70
+ console.log(`Re-triggering PageView for page: ${pageName} after returning from background`);
71
+ triggerPageView(); // Re-trigger PageView when coming back to foreground
72
+ }
73
+ }
74
+ appState.current = nextAppState; // Update app state
75
+ };
76
+
77
+ const appStateListener = AppState.addEventListener('change', handleAppStateChange);
78
+
79
+ // Cleanup listeners on unmount
80
+ return () => {
81
+ unsubscribeFocus();
82
+ unsubscribeBlur();
83
+ appStateListener.remove();
84
+ triggerPageDuration(); // Trigger duration if unmounted
85
+ };
86
+ }, [navigation, pageName, startTime]);
87
+
88
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "reactnative-plugin-appice",
3
- "version": "1.7.16",
3
+ "version": "1.7.17",
4
4
  "description": "appICE React Native SDK",
5
5
  "main": "index.js",
6
6
  "files": [