{"id":15649783,"url":"https://github.com/revolunet/react-analytics-widget","last_synced_at":"2025-04-30T07:49:56.514Z","repository":{"id":52800680,"uuid":"97532882","full_name":"revolunet/react-analytics-widget","owner":"revolunet","description":"Embed Google Analytics widgets in your React applications.","archived":false,"fork":false,"pushed_at":"2022-06-30T16:10:13.000Z","size":1230,"stargazers_count":37,"open_issues_count":9,"forks_count":12,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-30T07:49:51.409Z","etag":null,"topics":["analytics","google-analytics","react","react-component","widget"],"latest_commit_sha":null,"homepage":"https://revolunet.github.io/react-analytics-widget","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/revolunet.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-07-18T00:02:16.000Z","updated_at":"2023-07-17T03:56:04.000Z","dependencies_parsed_at":"2022-08-23T06:10:54.939Z","dependency_job_id":null,"html_url":"https://github.com/revolunet/react-analytics-widget","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/revolunet%2Freact-analytics-widget","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/revolunet%2Freact-analytics-widget/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/revolunet%2Freact-analytics-widget/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/revolunet%2Freact-analytics-widget/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/revolunet","download_url":"https://codeload.github.com/revolunet/react-analytics-widget/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251666227,"owners_count":21624291,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["analytics","google-analytics","react","react-component","widget"],"created_at":"2024-10-03T12:32:01.042Z","updated_at":"2025-04-30T07:49:56.490Z","avatar_url":"https://github.com/revolunet.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# react-analytics-widget\n\n[![npm package][npm-badge]][npm]\n\nEmbed Google Analytics widgets in your React applications.\n\n - The `GoogleProvider` container ensure user is logged on analytics\n - The `GoogleDataChart` component display any [DataChart configuraton](https://developers.google.com/analytics/devguides/reporting/embed/v1/component-reference#datachart)\n\n![](./demo.png)\n\nDemo : [https://revolunet.github.io/react-analytics-widget](https://revolunet.github.io/react-analytics-widget)\n\n## Requirements\n\nYou need to create a OAUTH client id in the [google developer console](https://console.developers.google.com/apis/credentials/oauthclient/960315238073-dv345fcj3tkikn506k9lrch73hk9259u.apps.googleusercontent.com?project=eastern-store-174123) and provide an [analytic view ID](https://ga-dev-tools.appspot.com/query-explorer/).\nAlternatively you can use server-side authentication tokens. You can find more info in this [example](https://ga-dev-tools.appspot.com/embed-api/server-side-authorization/).\n\n### Note:\nIf you provide values for both the `accessToken` and the `clientId` props, the latter will be ignored.\n\nAlso, add the Google SDK at the top of your page\n\n```js\n;(function(w, d, s, g, js, fjs) {\n  g = w.gapi || (w.gapi = {})\n  g.analytics = {\n    q: [],\n    ready: function(cb) {\n      this.q.push(cb)\n    }\n  }\n  js = d.createElement(s)\n  fjs = d.getElementsByTagName(s)[0]\n  js.src = \"https://apis.google.com/js/platform.js\"\n  fjs.parentNode.insertBefore(js, fjs)\n  js.onload = function() {\n    g.load(\"analytics\")\n  }\n})(window, document, \"script\")\n```\n\n## Usage\n### OAUTH authentication\n\n```js\n\nimport { GoogleProvider, GoogleDataChart } from 'react-analytics-widget'\n\nconst CLIENT_ID = 'x-x--x---x---x-xx--x-apps.googleusercontent.com';\n\n// graph 1 config\nconst last30days = {\n  reportType: \"ga\",\n  query: {\n    dimensions: \"ga:date\",\n    metrics: \"ga:pageviews\",\n    \"start-date\": \"30daysAgo\",\n    \"end-date\": \"yesterday\"\n  },\n  chart: {\n    type: \"LINE\",\n    options: {\n      // options for google charts\n      // https://google-developers.appspot.com/chart/interactive/docs/gallery\n      title: \"Last 30 days pageviews\"\n    }\n  }\n}\n\n// graph 2 config\nconst last7days = {\n  reportType: \"ga\",\n  query: {\n    dimensions: \"ga:date\",\n    metrics: \"ga:pageviews\",\n    \"start-date\": \"7daysAgo\",\n    \"end-date\": \"yesterday\"\n  },\n  chart: {\n    type: \"LINE\"\n  }\n}\n\n// analytics views ID\nconst views = {\n  query: {\n    ids: \"ga:87986986\"\n  }\n}\n\nconst Example = () =\u003e (\n  \u003cGoogleProvider clientId={CLIENT_ID}\u003e\n    \u003cGoogleDataChart views={views} config={last30days} /\u003e\n    \u003cGoogleDataChart views={views} config={last7days} /\u003e\n  \u003c/GoogleProvider\u003e\n)\n```\n\n### Server-side token authentication\n\n```js\n\nimport React, { Component } from 'react';\nimport { GoogleProvider, GoogleDataChart } from 'react-analytics-widget'\n\n// graph 1 config\nconst last30days = {\n  reportType: \"ga\",\n  query: {\n    dimensions: \"ga:date\",\n    metrics: \"ga:pageviews\",\n    \"start-date\": \"30daysAgo\",\n    \"end-date\": \"yesterday\"\n  },\n  chart: {\n    type: \"LINE\",\n    options: {\n      // options for google charts\n      // https://google-developers.appspot.com/chart/interactive/docs/gallery\n      title: \"Last 30 days pageviews\"\n    }\n  }\n}\n\n// graph 2 config\nconst last7days = {\n  reportType: \"ga\",\n  query: {\n    dimensions: \"ga:date\",\n    metrics: \"ga:pageviews\",\n    \"start-date\": \"7daysAgo\",\n    \"end-date\": \"yesterday\"\n  },\n  chart: {\n    type: \"LINE\"\n  }\n}\n\n// analytics views ID\nconst views = {\n  query: {\n    ids: \"ga:87986986\"\n  }\n}\n\nclass Example extends Component {\n  componentDidMount = () =\u003e {\n    const request = new Request('https://yourserver.example/auth/ganalytics/getToken', {\n      method: 'GET'\n    });\n    fetch(request)\n      .then(response =\u003e response.json())\n      .then(({ token }) =\u003e {\n        this.setState({ token }); // TODO: handle errors\n      });\n  }\n\n  render = () =\u003e (\n    \u003cGoogleProvider accessToken={this.state.token}\u003e\n      \u003cGoogleDataChart views={views} config={last30days} /\u003e\n      \u003cGoogleDataChart views={views} config={last7days} /\u003e\n    \u003c/GoogleProvider\u003e\n  )\n}\n```\n\n[npm-badge]: https://img.shields.io/npm/v/react-analytics-widget.png?style=flat-square\n[npm]: https://www.npmjs.org/package/react-analytics-widget\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frevolunet%2Freact-analytics-widget","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frevolunet%2Freact-analytics-widget","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frevolunet%2Freact-analytics-widget/lists"}