From 83dee3ba56d8bab7ed9b796db70652ac72515f65 Mon Sep 17 00:00:00 2001 From: Daniel Weinmann Date: Sat, 6 Jan 2018 15:20:27 -0200 Subject: [PATCH] Adds React Native Amplitude with Expo target --- README.md | 1 + docs/SUMMARY.md | 1 + docs/targets/react-native-amplitude-expo.md | 20 +++++++++++++++++++ .../amplitude-expo/amplitude-expo.js | 12 +++++++++++ .../react-native/amplitude-expo/index.js | 1 + src/targets/react-native/index.d.ts | 1 + src/targets/react-native/index.js | 2 ++ 7 files changed, 38 insertions(+) create mode 100644 docs/targets/react-native-amplitude-expo.md create mode 100644 src/targets/react-native/amplitude-expo/amplitude-expo.js create mode 100644 src/targets/react-native/amplitude-expo/index.js diff --git a/README.md b/README.md index ef3f200..67badc8 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,7 @@ events off to a target. Read more about offline event collection in the - [Amplitude](https://rangle.github.io/redux-beacon/docs/targets/amplitude.html) - [_(React Native)_ Google Analytics](https://rangle.github.io/redux-beacon/docs/targets/react-native-google-analytics.html) - [_(React Native)_ Google Tag Manager](https://rangle.github.io/redux-beacon/docs/targets/react-native-google-tag-manager.html) + - [_(React Native)_ Amplitude with Expo](https://rangle.github.io/redux-beacon/docs/targets/react-native-amplitude-expo.html) - [_(Cordova)_ Google Analytics](https://rangle.github.io/redux-beacon/docs/targets/cordova-google-analytics.html) ## Docs diff --git a/docs/SUMMARY.md b/docs/SUMMARY.md index 22767f4..e9bca6c 100644 --- a/docs/SUMMARY.md +++ b/docs/SUMMARY.md @@ -15,6 +15,7 @@ * [Amplitude](docs/targets/amplitude.md) * [(React Native) Google Analytics](docs/targets/react-native-google-analytics.md) * [(React Native) Google Tag Manager](docs/targets/react-native-google-tag-manager.md) + * [(React Native) Amplitude with Expo](docs/targets/react-native-amplitude-expo.md) * [(Cordova) Google Analytics](docs/targets/cordova-google-analytics.md) * [Extensions](docs/extensions/index.md) * [logger](docs/extensions/logger.md) diff --git a/docs/targets/react-native-amplitude-expo.md b/docs/targets/react-native-amplitude-expo.md new file mode 100644 index 0000000..4521c9b --- /dev/null +++ b/docs/targets/react-native-amplitude-expo.md @@ -0,0 +1,20 @@ +# React Native Amplitude with Expo + +### Usage Instructions + +1. Sign up for Amplitude if you haven't already, and + [create a new project](https://amplitude.zendesk.com/hc/en-us/articles/207108137-Introduction-Getting-Started). + +2. Take note of your [Amplitude API key](https://amplitude.zendesk.com/hc/en-us/articles/207108137-Introduction-Getting-Started#getting-started). + +3. Create a new [Expo](https://expo.io/learn) project, or [use ExpoKit](https://docs.expo.io/versions/latest/guides/expokit.html) in your project. + +4. Import the target, then provide it when creating the middleware: + + ```js + import { Amplitude } from 'expo'; + import { AmplitudeExpo } from 'redux-beacon/targets/react-native'; + + const target = AmplitudeExpo('YOUR_API_KEY', AmplitudeExpo); + const analyticsMiddleware = createMiddleware(eventsMap, target); + ``` diff --git a/src/targets/react-native/amplitude-expo/amplitude-expo.js b/src/targets/react-native/amplitude-expo/amplitude-expo.js new file mode 100644 index 0000000..4955020 --- /dev/null +++ b/src/targets/react-native/amplitude-expo/amplitude-expo.js @@ -0,0 +1,12 @@ +export function AmplitudeExpo(apiKey, Amplitude) { + Amplitude.initialize(apiKey); + return function AmplitudeExpoTarget(events) { + events.forEach(targetEvent => { + const properties = Object.keys(targetEvent) + .filter(key => key !== 'event') + .reduce((result, key) => (result[key] = obj[key], result), {}); + + Amplitude.logEventWithProperties(targetEvent.event, properties); + }); + }; +} diff --git a/src/targets/react-native/amplitude-expo/index.js b/src/targets/react-native/amplitude-expo/index.js new file mode 100644 index 0000000..f890789 --- /dev/null +++ b/src/targets/react-native/amplitude-expo/index.js @@ -0,0 +1 @@ +export { AmplitudeExpo } from './amplitude-expo'; diff --git a/src/targets/react-native/index.d.ts b/src/targets/react-native/index.d.ts index f70205d..568117a 100644 --- a/src/targets/react-native/index.d.ts +++ b/src/targets/react-native/index.d.ts @@ -1,2 +1,3 @@ export function GoogleAnalytics(trackingId: string, GABridge: any): void; export function GoogleTagManager(trackingId: string, GTMBridge: any): void; +export function AmplitudeExpo(apiKey: string, Amplitude: any): void; diff --git a/src/targets/react-native/index.js b/src/targets/react-native/index.js index fc83c46..917fc2d 100644 --- a/src/targets/react-native/index.js +++ b/src/targets/react-native/index.js @@ -1,7 +1,9 @@ const { GoogleAnalytics } = require('./google-analytics'); const { GoogleTagManager } = require('./google-tag-manager'); +const { AmplitudeExpo } = require('./amplitude-expo'); module.exports = { GoogleAnalytics, GoogleTagManager, + AmplitudeExpo, };