-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add app install, foreground and background event and application enti…
…ty tracking (#1396)
- Loading branch information
1 parent
d6a87a0
commit 15c050c
Showing
20 changed files
with
648 additions
and
27 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
...ive-tracker/markdown/react-native-tracker.applifecycleconfiguration.appbuild.md
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,15 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [@snowplow/react-native-tracker](./react-native-tracker.md) > [AppLifecycleConfiguration](./react-native-tracker.applifecycleconfiguration.md) > [appBuild](./react-native-tracker.applifecycleconfiguration.appbuild.md) | ||
|
||
## AppLifecycleConfiguration.appBuild property | ||
|
||
Build name of the application e.g s9f2k2d or 1.1.0 beta | ||
|
||
Entity schema: `iglu:com.snowplowanalytics.mobile/application/jsonschema/1-0-0` | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
appBuild?: string; | ||
``` |
15 changes: 15 additions & 0 deletions
15
...e-tracker/markdown/react-native-tracker.applifecycleconfiguration.appversion.md
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,15 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [@snowplow/react-native-tracker](./react-native-tracker.md) > [AppLifecycleConfiguration](./react-native-tracker.applifecycleconfiguration.md) > [appVersion](./react-native-tracker.applifecycleconfiguration.appversion.md) | ||
|
||
## AppLifecycleConfiguration.appVersion property | ||
|
||
Version number of the application e.g 1.1.0 (semver or git commit hash). | ||
|
||
Entity schema if `appBuild` property is set: `iglu:com.snowplowanalytics.mobile/application/jsonschema/1-0-0` Entity schema if `appBuild` property is not set: `iglu:com.snowplowanalytics.snowplow/application/jsonschema/1-0-0` | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
appVersion?: string; | ||
``` |
15 changes: 15 additions & 0 deletions
15
.../markdown/react-native-tracker.applifecycleconfiguration.installautotracking.md
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,15 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [@snowplow/react-native-tracker](./react-native-tracker.md) > [AppLifecycleConfiguration](./react-native-tracker.applifecycleconfiguration.md) > [installAutotracking](./react-native-tracker.applifecycleconfiguration.installautotracking.md) | ||
|
||
## AppLifecycleConfiguration.installAutotracking property | ||
|
||
Whether to automatically track app install event on first run. | ||
|
||
Schema: `iglu:com.snowplowanalytics.mobile/application_install/jsonschema/1-0-0` | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
installAutotracking?: boolean; | ||
``` |
15 changes: 15 additions & 0 deletions
15
...arkdown/react-native-tracker.applifecycleconfiguration.lifecycleautotracking.md
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,15 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [@snowplow/react-native-tracker](./react-native-tracker.md) > [AppLifecycleConfiguration](./react-native-tracker.applifecycleconfiguration.md) > [lifecycleAutotracking](./react-native-tracker.applifecycleconfiguration.lifecycleautotracking.md) | ||
|
||
## AppLifecycleConfiguration.lifecycleAutotracking property | ||
|
||
Whether to automatically track app lifecycle events (app foreground and background events). Also adds a lifecycle context entity to all events. | ||
|
||
Foreground event schema: `iglu:com.snowplowanalytics.snowplow/application_foreground/jsonschema/1-0-0` Background event schema: `iglu:com.snowplowanalytics.snowplow/application_background/jsonschema/1-0-0` Context entity schema: `iglu:com.snowplowanalytics.mobile/application_lifecycle/jsonschema/1-0-0` | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
lifecycleAutotracking?: boolean; | ||
``` |
23 changes: 23 additions & 0 deletions
23
...react-native-tracker/markdown/react-native-tracker.applifecycleconfiguration.md
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,23 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [@snowplow/react-native-tracker](./react-native-tracker.md) > [AppLifecycleConfiguration](./react-native-tracker.applifecycleconfiguration.md) | ||
|
||
## AppLifecycleConfiguration interface | ||
|
||
Configuration for app lifecycle tracking | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
export interface AppLifecycleConfiguration | ||
``` | ||
|
||
## Properties | ||
|
||
| Property | Type | Description | | ||
| --- | --- | --- | | ||
| [appBuild?](./react-native-tracker.applifecycleconfiguration.appbuild.md) | string | <i>(Optional)</i> Build name of the application e.g s9f2k2d or 1.1.0 beta<!-- -->Entity schema: <code>iglu:com.snowplowanalytics.mobile/application/jsonschema/1-0-0</code> | | ||
| [appVersion?](./react-native-tracker.applifecycleconfiguration.appversion.md) | string | <i>(Optional)</i> Version number of the application e.g 1.1.0 (semver or git commit hash).<!-- -->Entity schema if <code>appBuild</code> property is set: <code>iglu:com.snowplowanalytics.mobile/application/jsonschema/1-0-0</code> Entity schema if <code>appBuild</code> property is not set: <code>iglu:com.snowplowanalytics.snowplow/application/jsonschema/1-0-0</code> | | ||
| [installAutotracking?](./react-native-tracker.applifecycleconfiguration.installautotracking.md) | boolean | <i>(Optional)</i> Whether to automatically track app install event on first run.<!-- -->Schema: <code>iglu:com.snowplowanalytics.mobile/application_install/jsonschema/1-0-0</code> | | ||
| [lifecycleAutotracking?](./react-native-tracker.applifecycleconfiguration.lifecycleautotracking.md) | boolean | <i>(Optional)</i> Whether to automatically track app lifecycle events (app foreground and background events). Also adds a lifecycle context entity to all events.<!-- -->Foreground event schema: <code>iglu:com.snowplowanalytics.snowplow/application_foreground/jsonschema/1-0-0</code> Background event schema: <code>iglu:com.snowplowanalytics.snowplow/application_background/jsonschema/1-0-0</code> Context entity schema: <code>iglu:com.snowplowanalytics.mobile/application_lifecycle/jsonschema/1-0-0</code> | | ||
|
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
10 changes: 10 additions & 0 deletions
10
...snowplow/react-native-tracker/issue-react-native-lifecycle-tracking_2024-12-02-08-40.json
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,10 @@ | ||
{ | ||
"changes": [ | ||
{ | ||
"packageName": "@snowplow/react-native-tracker", | ||
"comment": "Add app install, foreground and background event and application entity tracking (#1396)", | ||
"type": "none" | ||
} | ||
], | ||
"packageName": "@snowplow/react-native-tracker" | ||
} |
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
44 changes: 44 additions & 0 deletions
44
trackers/react-native-tracker/src/plugins/app_context/index.ts
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,44 @@ | ||
import { CorePluginConfiguration, SelfDescribingJson } from '@snowplow/tracker-core'; | ||
import { AppLifecycleConfiguration } from '../../types'; | ||
import { APPLICATION_CONTEXT_SCHEMA, MOBILE_APPLICATION_CONTEXT_SCHEMA } from '../../constants'; | ||
|
||
/** | ||
* Tracks the application context entity with information about the app version. | ||
* If appBuild is provided, a mobile application context is tracked, otherwise the Web equivalent is tracked. | ||
* | ||
* Entity schema if `appBuild` property is set: `iglu:com.snowplowanalytics.mobile/application/jsonschema/1-0-0` | ||
* Entity schema if `appBuild` property is not set: `iglu:com.snowplowanalytics.snowplow/application/jsonschema/1-0-0` | ||
*/ | ||
export function newAppContextPlugin({ appVersion, appBuild }: AppLifecycleConfiguration): CorePluginConfiguration { | ||
const contexts = () => { | ||
let entities: SelfDescribingJson[] = []; | ||
|
||
if (appVersion) { | ||
// Add application context to all events | ||
if (appBuild) { | ||
entities.push({ | ||
schema: MOBILE_APPLICATION_CONTEXT_SCHEMA, | ||
data: { | ||
version: appVersion, | ||
build: appBuild, | ||
}, | ||
}); | ||
} else { | ||
entities.push({ | ||
schema: APPLICATION_CONTEXT_SCHEMA, | ||
data: { | ||
version: appVersion, | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
return entities; | ||
}; | ||
|
||
return { | ||
plugin: { | ||
contexts, | ||
}, | ||
}; | ||
} |
37 changes: 37 additions & 0 deletions
37
trackers/react-native-tracker/src/plugins/app_install/index.ts
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,37 @@ | ||
import { buildSelfDescribingEvent, CorePluginConfiguration, TrackerCore } from '@snowplow/tracker-core'; | ||
import { AppLifecycleConfiguration, TrackerConfiguration } from '../../types'; | ||
import { APPLICATION_INSTALL_EVENT_SCHEMA } from '../../constants'; | ||
import AsyncStorage from '@react-native-async-storage/async-storage'; | ||
|
||
/** | ||
* Tracks an application install event on the first run of the app. | ||
* Stores the install event in AsyncStorage to prevent tracking on subsequent runs. | ||
* | ||
* Event schema: `iglu:com.snowplowanalytics.mobile/application_install/jsonschema/1-0-0` | ||
*/ | ||
export function newAppInstallPlugin( | ||
{ namespace, installAutotracking = false }: TrackerConfiguration & AppLifecycleConfiguration, | ||
core: TrackerCore | ||
): CorePluginConfiguration { | ||
if (installAutotracking) { | ||
// Track install event on first run | ||
const key = `snowplow_${namespace}_install`; | ||
setTimeout(async () => { | ||
const installEvent = await AsyncStorage.getItem(key); | ||
if (!installEvent) { | ||
core.track( | ||
buildSelfDescribingEvent({ | ||
event: { | ||
schema: APPLICATION_INSTALL_EVENT_SCHEMA, | ||
data: {}, | ||
}, | ||
}) | ||
); | ||
await AsyncStorage.setItem(key, new Date().toISOString()); | ||
} | ||
}, 0); | ||
} | ||
return { | ||
plugin: {}, | ||
}; | ||
} |
97 changes: 97 additions & 0 deletions
97
trackers/react-native-tracker/src/plugins/app_lifecycle/index.ts
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,97 @@ | ||
import { | ||
buildSelfDescribingEvent, | ||
CorePluginConfiguration, | ||
SelfDescribingJson, | ||
TrackerCore, | ||
} from '@snowplow/tracker-core'; | ||
import { AppLifecycleConfiguration, EventContext } from '../../types'; | ||
import { BACKGROUND_EVENT_SCHEMA, FOREGROUND_EVENT_SCHEMA, LIFECYCLE_CONTEXT_SCHEMA } from '../../constants'; | ||
import { AppState } from 'react-native'; | ||
|
||
export interface AppLifecyclePlugin extends CorePluginConfiguration { | ||
getIsInBackground: () => boolean | undefined; | ||
getBackgroundIndex: () => number | undefined; | ||
getForegroundIndex: () => number | undefined; | ||
} | ||
|
||
/** | ||
* Tracks foreground and background events automatically when the app state changes. | ||
* Also adds a lifecycle context to all events with information about the app visibility. | ||
*/ | ||
export async function newAppLifecyclePlugin( | ||
{ lifecycleAutotracking = true }: AppLifecycleConfiguration, | ||
core: TrackerCore | ||
): Promise<AppLifecyclePlugin> { | ||
let isInForeground = AppState.currentState !== 'background'; | ||
let foregroundIndex = isInForeground ? 1 : 0; | ||
let backgroundIndex = isInForeground ? 0 : 1; | ||
let subscription: ReturnType<typeof AppState.addEventListener> | undefined; | ||
|
||
if (lifecycleAutotracking) { | ||
// Subscribe to app state changes and track foreground/background events | ||
subscription = AppState.addEventListener('change', async (nextAppState) => { | ||
if (nextAppState === 'active' && !isInForeground) { | ||
trackForegroundEvent(); | ||
} | ||
if (nextAppState === 'background' && isInForeground) { | ||
trackBackgroundEvent(); | ||
} | ||
}); | ||
} | ||
|
||
const contexts = () => { | ||
let entities: SelfDescribingJson[] = []; | ||
|
||
if (lifecycleAutotracking) { | ||
// Add lifecycle context to all events | ||
entities.push({ | ||
schema: LIFECYCLE_CONTEXT_SCHEMA, | ||
data: { | ||
isVisible: isInForeground, | ||
index: isInForeground ? foregroundIndex : backgroundIndex, | ||
}, | ||
}); | ||
} | ||
|
||
return entities; | ||
}; | ||
|
||
const deactivatePlugin = () => { | ||
if (subscription) { | ||
subscription.remove(); | ||
subscription = undefined; | ||
} | ||
}; | ||
|
||
const trackForegroundEvent = (contexts?: EventContext[]) => { | ||
if (!isInForeground) { | ||
isInForeground = true; | ||
foregroundIndex += 1; | ||
} | ||
core.track( | ||
buildSelfDescribingEvent({ event: { schema: FOREGROUND_EVENT_SCHEMA, data: { foregroundIndex } } }), | ||
contexts | ||
); | ||
}; | ||
|
||
const trackBackgroundEvent = (contexts?: EventContext[]) => { | ||
if (isInForeground) { | ||
isInForeground = false; | ||
backgroundIndex += 1; | ||
} | ||
core.track( | ||
buildSelfDescribingEvent({ event: { schema: BACKGROUND_EVENT_SCHEMA, data: { backgroundIndex } } }), | ||
contexts | ||
); | ||
}; | ||
|
||
return { | ||
getIsInBackground: () => (lifecycleAutotracking ? !isInForeground : undefined), | ||
getBackgroundIndex: () => (lifecycleAutotracking ? backgroundIndex : undefined), | ||
getForegroundIndex: () => (lifecycleAutotracking ? foregroundIndex : undefined), | ||
plugin: { | ||
contexts, | ||
deactivatePlugin, | ||
}, | ||
}; | ||
} |
Oops, something went wrong.