How to use React Dashboard analytics with an external site FOR FREE
Tags: react, serverless, netlify, javascript
Published: 2019-09-29 19:54:10

---
title: How to use React Dashboard analytics with an external site FOR FREE
published: true
description: 30+ smart components with included serverless analytics & user auth functions that works with Netlify.
tags: react, serverless, netlify, javascript
cover_image: https://cdn.sanity.io/images/axalcmta/posts/888cd6e5452663dddb161da4b6ae4bb3f28095b5-1343x705.png
---

With the latest release of [React Dashboard](https://reactdashboard.com), there is an included analytics function that lets you store your own analytics data. It doesn't store cookies or ip addresses, instead you get the necessities like your most popular pages, referrers, device, browser & country. This has a huge advantage as it's GDPR compliant.

The only requirements are the free version of Netlify & the sandbox database from mLab.

First create a database on mLab. You can choose the free sandbox version:
![mlab](https://cdn.sanity.io/images/axalcmta/posts/981cbb18d159f9e482ccc686f1ba1e304db4b0af-2672x1398.png?w=500&fit=max)

When the database is created, click on it and select the User tab. We need to create an admin user for this database:
![mlab](https://cdn.sanity.io/images/axalcmta/posts/bb4afc130bb1bcd54133a95a8dcefbfc584ef89d-2336x728.png?w=500&fit=max)

Then keep note of your mongodb url:
![mlab](https://cdn.sanity.io/images/axalcmta/posts/4cc2d34785483094197f2a4f4a1be98e176f3599-2270x336.png?w=500&fit=max)

`mongodb://<dbuser>:<dbpassword>@ds031978.mlab.com:31978/dbname`

Replace `<dbuser>` & `<dbpassword>` with the credentials of the user you just created above.

Now push the code in the React Dashboard folder to Github or Gitlab, then create a new site from Git on Netlify.
![netlify](https://cdn.sanity.io/images/axalcmta/posts/560fcca286f518f6ae0843b5d12dba8c8ee4aa6e-522x208.png?w=500&fit=max)

These are the required build steps. It should populate automatically.
![netlify](https://cdn.sanity.io/images/axalcmta/posts/d578199fb9f285c4c706c7c3c7468580a7080e86-1594x536.png?w=500&fit=max)

Then there are 3 required environment variables:
![netlify](https://cdn.sanity.io/images/axalcmta/posts/740f668eada7f28a792ead7b8a7b5195b848f5cb-1630x774.png?w=500&fit=max)

Fill the `MONGODB_URL` with the mlab url above, containing the user credentials.

Then on the external site you want to track analytics, run this API request on page load. This can be done with vanilla javascript, jQuery, React or any frontend framework you prefer.

I created a little api request library that you can copy and paste into your code. It uses Axios, so you'll need to run npm install --save axios inside your project directory.

```js
   import axios from 'axios'
    
    const createAxios = (token) => {
    
      const instance = axios.create({
        headers: {
          Accept: 'application/json'
        }
      });
    
      return instance;
    }
    
    
    export const POST = (url, data, token) => createAxios(token).post(url, data, token);
    export const PATCH = (url, data, token) => createAxios(token).patch(url, data);
    export const PUT = (url, data, token) => createAxios(token).put(url, data);
    export const DELETE = (url, params, token) => createAxios(token).delete(url, { params: params || { foo: 'bar' } });
    export const GET = (url, params, token) => createAxios(token).get(url, { params });
```


Then lets use a React component as an example:
```js
import React from 'react'
    
import {POST} from '../lib/api'
    
  
const Index = () => {
    
  React.useEffect(() => {
    
    POST('https://<yournetlifyurl.com>/.netlify/functions/create-visitor', {
      referrer: document.referrer,
      path: window.location.pathname
    })
    
    
  }, [])
    
  return (
    <div><h1>Analytics Example</h1></div>
  )
}
    
export default Index
```

It's that simple, and you can start tracking your analytics - free, without any tracking cookies. Check out React Dashboard at [https://reactdashboard.com](https://reactdashboard.com)