DEV Community

Sergey Gustun
Sergey Gustun

Posted on

How make analytics for your app in React Native ?

Hi, I'am Sergey and i like React Native.

Recently me needed analytics for my app in React Native and i make small script.

  1. Go to https://appmetrica.yandex.ru , yeah this is Russian service for analytics, but he very cool.

  2. Registration and create 'metrica'

  3. Write this code

const API_KEY = 'WRITE_THIS_YOUR_KEY'
const BOTAN_URL = 'https://api.botan.io/track';
const DEFAULT_NAME = 'Message';

function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
  //better you use really UID in your app
}

export default function sendEvent(message, name = DEFAULT_NAME) {
    return fetch(`${BOTAN_URL}/?token=${API_KEY}&name=${name}&uid=${getRandomInt(0,9999)}`, {
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
          },
          method: "POST",
          body: JSON.stringify(message)
    })
    .then(res => res.json())
    .then(data => {
      return data
    })
}

Enter fullscreen mode Exit fullscreen mode
  1. Done!

How use this function?

componentDidMount() {
        getAllCategories()
        .then(data => {

            let categories = data.response.category

            sendEvent({},'loadCategories')

            this.setState({
                categories : categories,
                end_loading_categories : false
            })
        })
        .catch(() => {

            sendEvent({},'loadCategoriesError',() => {})

            this.setState({
                end_loading_categories : false
            })
        })
    }

Enter fullscreen mode Exit fullscreen mode

Here's how it looks on the charts.

Charts

Top comments (0)