Giter Site home page Giter Site logo

revolunet / react-analytics-widget Goto Github PK

View Code? Open in Web Editor NEW
37.0 4.0 11.0 1.2 MB

Embed Google Analytics widgets in your React applications.

Home Page: https://revolunet.github.io/react-analytics-widget

JavaScript 100.00%
react analytics widget google-analytics react-component

react-analytics-widget's Introduction

react-analytics-widget

npm package

Embed Google Analytics widgets in your React applications.

  • The GoogleProvider container ensure user is logged on analytics
  • The GoogleDataChart component display any DataChart configuraton

Demo : https://revolunet.github.io/react-analytics-widget

Requirements

You need to create a OAUTH client id in the google developer console and provide an analytic view ID. Alternatively you can use server-side authentication tokens. You can find more info in this example.

Note:

If you provide values for both the accessToken and the clientId props, the latter will be ignored.

Also, add the Google SDK at the top of your page

;(function(w, d, s, g, js, fjs) {
  g = w.gapi || (w.gapi = {})
  g.analytics = {
    q: [],
    ready: function(cb) {
      this.q.push(cb)
    }
  }
  js = d.createElement(s)
  fjs = d.getElementsByTagName(s)[0]
  js.src = "https://apis.google.com/js/platform.js"
  fjs.parentNode.insertBefore(js, fjs)
  js.onload = function() {
    g.load("analytics")
  }
})(window, document, "script")

Usage

OAUTH authentication

import { GoogleProvider, GoogleDataChart } from 'react-analytics-widget'

const CLIENT_ID = 'x-x--x---x---x-xx--x-apps.googleusercontent.com';

// graph 1 config
const last30days = {
  reportType: "ga",
  query: {
    dimensions: "ga:date",
    metrics: "ga:pageviews",
    "start-date": "30daysAgo",
    "end-date": "yesterday"
  },
  chart: {
    type: "LINE",
    options: {
      // options for google charts
      // https://google-developers.appspot.com/chart/interactive/docs/gallery
      title: "Last 30 days pageviews"
    }
  }
}

// graph 2 config
const last7days = {
  reportType: "ga",
  query: {
    dimensions: "ga:date",
    metrics: "ga:pageviews",
    "start-date": "7daysAgo",
    "end-date": "yesterday"
  },
  chart: {
    type: "LINE"
  }
}

// analytics views ID
const views = {
  query: {
    ids: "ga:87986986"
  }
}

const Example = () => (
  <GoogleProvider clientId={CLIENT_ID}>
    <GoogleDataChart views={views} config={last30days} />
    <GoogleDataChart views={views} config={last7days} />
  </GoogleProvider>
)

Server-side token authentication

import React, { Component } from 'react';
import { GoogleProvider, GoogleDataChart } from 'react-analytics-widget'

// graph 1 config
const last30days = {
  reportType: "ga",
  query: {
    dimensions: "ga:date",
    metrics: "ga:pageviews",
    "start-date": "30daysAgo",
    "end-date": "yesterday"
  },
  chart: {
    type: "LINE",
    options: {
      // options for google charts
      // https://google-developers.appspot.com/chart/interactive/docs/gallery
      title: "Last 30 days pageviews"
    }
  }
}

// graph 2 config
const last7days = {
  reportType: "ga",
  query: {
    dimensions: "ga:date",
    metrics: "ga:pageviews",
    "start-date": "7daysAgo",
    "end-date": "yesterday"
  },
  chart: {
    type: "LINE"
  }
}

// analytics views ID
const views = {
  query: {
    ids: "ga:87986986"
  }
}

class Example extends Component {
  componentDidMount = () => {
    const request = new Request('https://yourserver.example/auth/ganalytics/getToken', {
      method: 'GET'
    });
    fetch(request)
      .then(response => response.json())
      .then(({ token }) => {
        this.setState({ token }); // TODO: handle errors
      });
  }

  render = () => (
    <GoogleProvider accessToken={this.state.token}>
      <GoogleDataChart views={views} config={last30days} />
      <GoogleDataChart views={views} config={last7days} />
    </GoogleProvider>
  )
}

react-analytics-widget's People

Contributors

revolunet avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

react-analytics-widget's Issues

GoogleDataChart render issues

When config prop is changed, GoogleDataChart (wrt user input for start and end date) doesn't render charts from changed prop value. Is there a workaround for it?

Don,t appears chart in react-router project

thank you for your work.
I am working with it on react-router application.
But don,t show chart.
login is succesed, But GooleDatachart does not show data in chart.
there is only blank chart boder.
I think data don't translate to Googledatachart.

I give you regard again for your wonderful work

Access google analytics button vanishes

I am using this in my react app where I load one component on a button click and another on another button click. When I click on my analytics button for the first time, I can see "Access google analytics" button then I click on other button to load other component and if i load analytics component back, I am not able to see that button again. I have not clicked on that button yet still it magically vanishes

Callback to handel expired or invalidated tokens

I want a callback to know if my accessToken has expired or is incorrect form GoogleProvider component. When using server auth, it is important to refresh token when the previous token has expired. Is there a way to know if my accessToken is invalid or expired?

Fetch all Analytics View IDs

Hi,

Is there a method to fetch all the available Analytics View IDs under signed-in user. Can you help me with any documentation if its not possible with this library ?

Realtime data

Ever tried to get realtime data from GA?
I did some research but i think it used a diffrent api.

Loader Support

Is there a way we could add a loading message or image when GoogleDataChart component is being loaded ?

GoogleDataChart component does not mount again

Hi revolunet!

I came across with a bug while trying to integrate your widget on my app. As mentioned on the title, when navigating to my other components and navigating back to the page where the widget is, GoogleDataChart component does not seem to be mounted again.

Here's a quick preview:
new-tab

I added logs on your widget (for both componentDidMount on GoogleProvider and GoogleDataChart components) to see if they really get called or not.
On the first visit these are what gets called:
chrome-logs

Then, revisiting /reports... only GoogleProvider gets mounted
chrome-logs-2

I would greatly appreciate your input on this. Thanks!

API Key Auth

Is it possible to use API key authentication instead of the OAUTH token?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.