Created
February 28, 2017 21:56
-
-
Save elof/412c4f62518d4e274f685d48406cb037 to your computer and use it in GitHub Desktop.
Using Keen IO to collect and query event data in React Native
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
/** | |
* Sample React Native App | |
* https://github.com/facebook/react-native | |
* @flow | |
*/ | |
import React, { Component } from 'react'; | |
import { | |
AppRegistry, | |
StyleSheet, | |
Text, | |
View, | |
Fetch | |
} from 'react-native'; | |
export default class AwesomeProject extends Component { | |
render() { | |
return ( | |
<View style={styles.container}> | |
<Text style={styles.welcome}> | |
Reactive native is pretty dang cool | |
</Text> | |
</View> | |
); | |
} | |
} | |
const styles = StyleSheet.create({ | |
container: { | |
flex: 1, | |
justifyContent: 'center', | |
alignItems: 'center', | |
backgroundColor: '#F5FCFF', | |
}, | |
welcome: { | |
fontSize: 20, | |
textAlign: 'center', | |
margin: 10, | |
}, | |
instructions: { | |
textAlign: 'center', | |
color: '#333333', | |
marginBottom: 5, | |
}, | |
}); | |
// This is how you can send an event to Keen using the fetch method | |
fetch('https://api.keen.io/3.0/projects/<YOUR_PROJECT_ID_GOES_HERE>/events/<CHOOSE_A_COLLECTION_NAME>?api_key=<YOUR_KEEN_IO_KEY_GOES_HERE>', { | |
method: 'POST', | |
headers: { | |
'Accept': 'application/json', | |
'Content-Type': 'application/json', | |
}, | |
body: JSON.stringify({ | |
firstParam: 'yourValue', | |
secondParam: 'yourOtherValue', | |
}) | |
}) | |
// This is how you can query Keen using the fetch method | |
fetch('https://api.keen.io/3.0/projects/<YOUR_PROJECT_ID_GOES_HERE>/queries/count?api_key=<YOUR_KEEN_IO_KEY_GOES_HERE>&event_collection=pageviews&timezone=UTC&timeframe=this_14_days&filters=%5B%5D') | |
.then((response) => response.json()) | |
.then((responseJson) => { | |
var queryResult = responseJson.result; | |
console.log(queryResult); | |
}) | |
.catch((error) => { | |
console.error(error); | |
}); | |
AppRegistry.registerComponent('AwesomeProject', () => AwesomeProject); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment