Skip to content

Commit ad411ed

Browse files
committed
Placed all scripts in main package json
1 parent 2fa1aa8 commit ad411ed

29 files changed

Lines changed: 657 additions & 146 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ oauth_key.json
33
node_modules/
44
sites/*/node_modules
55
sites/*/dist
6+
sites/*/build
67
sites/*/package-lock.json
78
sites/*/yarn.lock
89
sites/*/.env
910
packages/*/node_modules
1011
packages/*/dist
12+
packages/*/build
1113
packages/*/package-lock.json
1214
packages/*/yarn.lock
1315
packages/*/.env

README.md

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,132 @@ panels.
99
npm install google-analytics-embed-react
1010
```
1111

12+
## Examples
13+
14+
We implemented two examples to show you the usage of this library. One
15+
example is using Google Identity Service to authenticate users on
16+
frontend. Other Example is using google API client to authenticate your
17+
service account. You need to have at-least one Google Analytics view
18+
in your account to run those examples.
19+
20+
### Frontend Authentication Example
21+
22+
This example is using the latest Google Identity Service to
23+
authenticate. Because recommended way is deprecated by Google ([Migrate
24+
to Google Identity
25+
Services](https://developers.google.com/identity/oauth2/web/guides/migration-to-gis)).
26+
You can find the source files for this example in the
27+
`sites/react-frontend-auth` folder.
28+
29+
Steps to run the example:-
30+
31+
1. Create a project on Google API Console and create a OAuth2 client id.
32+
Download and save the JSON key file to the root folder of this project
33+
as `oauth_key.json`.
34+
35+
2. Install all dependencies from the root folder of the project.
36+
37+
```
38+
npm install --save-dev
39+
```
40+
41+
3. Build the React library.
42+
43+
```
44+
npm run reactlib:build
45+
```
46+
47+
4. Run the project
48+
49+
```
50+
npm run frontend_auth:start
51+
```
52+
53+
### Backend Authentication Example
54+
55+
In this example we are using the `googleapis` client library to
56+
authenticate from backend and sending the access token to frontend. You
57+
need a Google Service Account to run this example. You can find the
58+
source files for the backend example in the `sites/express-backend` and
59+
the frontend example in the `sites/react-backend-auth` folder.
60+
61+
Steps to run the example:-
62+
63+
1. Create a project on Google API Console and create a Service Account
64+
for the project. You need to add the email of that service account as
65+
a Viewer for your Google Analytics view. Download and save the JSON
66+
key file to the root folder of the project as
67+
`service_account_key.json`.
68+
69+
2. Install all dependencies from the root folder of the project.
70+
71+
```
72+
npm install --save-dev
73+
```
74+
75+
3. Build the React library.
76+
77+
```
78+
npm run reactlib:build
79+
```
80+
81+
4. Run the backend ExpressJS project
82+
83+
```
84+
npm run backend:start
85+
```
86+
87+
5. Open another terminal and run the frontend parallely.
88+
89+
```
90+
npm run backend_auth:start
91+
```
92+
93+
## Usage
94+
95+
You need to wrap your entire app with the `GoogleAnalyitcsProvider`
96+
component as the first step to use this library. This component will
97+
take the `accessToken` as the only mandatory prop. But it is also
98+
optional :smiley:. It means you do not need the accessToken in first
99+
rendering time. But you have to pass it later to display your graphs.
100+
101+
```jsx
102+
<GoogleAnalyticsProvider accessToken={myAccessToken}>
103+
{/* Your application components */}
104+
</GoogleAnalyticsProvider>
105+
```
106+
107+
Then render any chart component in your application.
108+
109+
```
110+
<GoogleAnalyticsPieChart
111+
query={{
112+
ids, // <-- Replace with the ids value for your view.
113+
'start-date': '90daysAgo',
114+
'end-date': 'today',
115+
metrics:
116+
'ga:pageviews,ga:uniquePageviews,ga:timeOnPage,ga:bounces,ga:entrances,ga:exits',
117+
sort: '-ga:pageviews',
118+
dimensions: 'ga:pagePath',
119+
'max-results': 10
120+
}}
121+
width={500}
122+
style={{ float: 'left' }}
123+
pieHole={0.4}
124+
/>
125+
```
126+
127+
## Component Reference
128+
129+
### GoogleAnalyticsProvider
130+
131+
This component will load the Google platform script and authenticate
132+
yourself with the `gapi.analytics` namespace.
133+
134+
#### Props
135+
136+
| Name | Type | Default | Optional | Description |
137+
| ------------------ | ------ | --------- | -------- | --------------- |
138+
| `accessToken` | string | undefined | No\* | Access token to |
139+
| authroize the user |
140+

package.json

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,42 @@
11
{
22
"name": "google-analytics-embed-react",
3+
"scripts": {
4+
"all:lint:fix": "npm run backend:lint:fix && npm run backend_auth:lint:fix && npm run frontend_auth:lint:fix && npm run reactlib:lint:fix && npm run typeslib:lint:fix",
5+
"all:lint:check": "npm run backend:lint:check && npm run backend_auth:lint:check && npm run frontend_auth:lint:check && npm run reactlib:lint:check && npm run typeslib:lint:check",
6+
"backend:lint:fix": "npm run lint:fix --workspace sites/express-backend",
7+
"backend:lint:check": "npm run lint:check --workspace sites/express-backend",
8+
"backend_auth:lint:fix": "npm run lint:fix --workspace sites/react-backend-auth",
9+
"backend_auth:lint:check": "npm run lint:check --workspace sites/react-backend-auth",
10+
"frontend_auth:lint:fix": "npm run lint:fix --workspace sites/react-frontend-auth",
11+
"frontend_auth:lint:check": "npm run lint:check --workspace sites/react-frontend-auth",
12+
"reactlib:lint:fix": "npm run lint:fix --workspace packages/google-analytics-embed-react",
13+
"reactlib:lint:check": "npm run lint:check --workspace packages/google-analytics-embed-react",
14+
"typeslib:lint:fix": "npm run lint:fix --workspace packages/google-analytics-embed-types",
15+
"typeslib:lint:check": "npm run lint:check --workspace packages/google-analytics-embed-types",
16+
"all:prettier:fix": "npm run backend:prettier:fix && npm run backend_auth:prettier:fix && npm run frontend_auth:prettier:fix && npm run reactlib:prettier:fix && npm run typeslib:prettier:fix",
17+
"all:prettier:check": "npm run backend:prettier:check && npm run backend_auth:prettier:check && npm run frontend_auth:prettier:check && npm run reactlib:prettier:check && npm run typeslib:prettier:check",
18+
"backend:prettier:fix": "npm run prettier:fix --workspace sites/express-backend",
19+
"backend:prettier:check": "npm run prettier:check --workspace sites/express-backend",
20+
"backend_auth:prettier:fix": "npm run prettier:fix --workspace sites/react-backend-auth",
21+
"backend_auth:prettier:check": "npm run prettier:check --workspace sites/react-backend-auth",
22+
"frontend_auth:prettier:fix": "npm run prettier:fix --workspace sites/react-frontend-auth",
23+
"frontend_auth:prettier:check": "npm run prettier:check --workspace sites/react-frontend-auth",
24+
"reactlib:prettier:fix": "npm run prettier:fix --workspace packages/google-analytics-embed-react",
25+
"reactlib:prettier:check": "npm run prettier:check --workspace packages/google-analytics-embed-react",
26+
"typeslib:prettier:fix": "npm run prettier:fix --workspace packages/google-analytics-embed-types",
27+
"typeslib:prettier:check": "npm run prettier:check --workspace packages/google-analytics-embed-types",
28+
"backend:start": "npm run start --workspace sites/express-backend",
29+
"backend_auth:start": "npm run start --workspace sites/react-backend-auth",
30+
"backend_auth:build": "npm run build --workspace sites/react-backend-auth",
31+
"frontend_auth:start": "npm run start --workspace sites/react-frontend-auth",
32+
"frontend_auth:build": "npm run build --workspace sites/react-frontend-auth",
33+
"reactlib:build": "npm run build --workspace packages/google-analytics-embed-react"
34+
},
335
"private": true,
436
"workspaces": {
537
"packages": [
638
"packages/*",
739
"sites/*"
840
]
9-
},
10-
"devDependencies": {
11-
"mkdirp": "^1.0.4"
1241
}
1342
}

packages/google-analytics-embed-react/.eslintrc.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
],
1111
"overrides": [
1212
],
13+
"settings": {
14+
"react": {
15+
"version": "17"
16+
}
17+
},
1318
"parserOptions": {
1419
"ecmaVersion": "latest",
1520
"sourceType": "module",

packages/google-analytics-embed-react/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pnpm-debug.log*
88
lerna-debug.log*
99

1010
node_modules
11-
build
11+
dist
1212

1313
# Editor directories and files
1414
.vscode/*
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
node_modules
2+
src
3+
.eslintrc.json
4+
.gitignore
5+
.prettierrc
6+
package-lock.json
7+
rollup.config.js
8+
tsconfig.json
9+
tslint.json
10+
.npmignore

packages/google-analytics-embed-react/package.json

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
{
22
"name": "google-analytics-embed-react",
3+
"description": "Bringing the Google Analytics Embed Components for your ReactJS application",
34
"version": "0.0.1",
45
"type": "module",
56
"main": "dist/index.js",
67
"module": "dist/index.esm.js",
78
"types": "dist/index.d.ts",
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/arimacdev/google-analytics-embed-react.git"
12+
},
13+
"author": {
14+
"name": "WhizSid",
15+
"url": "https://github.com/whizsid",
16+
"email": "whizsid@aol.com"
17+
},
818
"scripts": {
919
"build": "rollup -c --bundleConfigAsCjs",
1020
"prettier:check": "prettier --check src/*",
@@ -16,8 +26,8 @@
1626
"@rollup/plugin-commonjs": "^24.0.0",
1727
"@rollup/plugin-node-resolve": "^15.0.1",
1828
"@types/google.visualization": "^0.0.68",
19-
"@types/react": "*",
20-
"@types/react-dom": "*",
29+
"@types/react": "^18.0",
30+
"@types/react-dom": "^18.0",
2131
"@typescript-eslint/eslint-plugin": "^5.48.2",
2232
"eslint": "^8.32.0",
2333
"eslint-config-prettier": "^8.6.0",
@@ -26,7 +36,7 @@
2636
"eslint-plugin-n": "^15.6.1",
2737
"eslint-plugin-promise": "^6.1.1",
2838
"eslint-plugin-react": "^7.32.0",
29-
"google-analytics-embed-types": "file:../google-analytics-embed-types",
39+
"google-analytics-embed-types": "0.0.1",
3040
"prettier": "2.8",
3141
"rollup-plugin-peer-deps-external": "^2.2.4",
3242
"rollup-plugin-typescript2": "^0.34.1",

packages/google-analytics-embed-react/src/DataChart.tsx

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,19 @@ export type DataChartProps<T> = {
88
children?: React.ReactNode;
99
/** Styles to the container element */
1010
style?: React.CSSProperties;
11+
/** Class name to use on the container element */
12+
className?: string;
13+
/** Firing after the data chart successfully rendered */
14+
onSuccess?: (result: gapi.analytics.googleCharts.DataChartSuccessResult) => void;
15+
/**
16+
* Fired when an error occurs during the query or rendering process. If you
17+
* want to get the error message from the response object it will be at
18+
* response.error.message.
19+
*/
20+
onError?: (response: gapi.analytics.ErrorResponse) => void;
1121
} & T;
1222

13-
export default class DataChart<O> extends React.Component<DataChartProps<O>> {
14-
public static contextType = GoogleAnalyticsContext;
23+
class DataChart<O> extends React.Component<DataChartProps<O>> {
1524
private readonly elementRef: React.RefObject<HTMLDivElement>;
1625
private googleDataChart: gapi.analytics.googleCharts.DataChart | null;
1726
private readonly chartType: string;
@@ -23,11 +32,15 @@ export default class DataChart<O> extends React.Component<DataChartProps<O>> {
2332
this.chartType = type;
2433
}
2534

26-
componentDidUpdate() {
35+
componentDidMount(): void {
36+
this.componentDidUpdate();
37+
}
38+
39+
componentDidUpdate(): void {
2740
const gaState = this.context as GoogleAnalyticsState;
28-
const { query, children, style, ...chartOptions } = this.props;
41+
const { query, children, style, className, onSuccess, onError, ...chartOptions } = this.props;
2942
// Rendering the component only if a user authenticated
30-
if (gaState == 'AUTH_SUCCESS') {
43+
if (gaState === 'AUTH_SUCCESS') {
3144
// Updating the existing chart with new options if already rendered
3245
if (this.googleDataChart != null) {
3346
this.googleDataChart.set({
@@ -38,6 +51,7 @@ export default class DataChart<O> extends React.Component<DataChartProps<O>> {
3851
options: chartOptions as any
3952
}
4053
});
54+
this.googleDataChart.execute();
4155
} else {
4256
// Creating a chart if a chart instance not created
4357
this.googleDataChart = new gapi.analytics.googleCharts.DataChart({
@@ -48,6 +62,15 @@ export default class DataChart<O> extends React.Component<DataChartProps<O>> {
4862
options: chartOptions as any
4963
}
5064
});
65+
66+
if (onSuccess != null) {
67+
this.googleDataChart.on('success', onSuccess);
68+
}
69+
70+
if (onError != null) {
71+
this.googleDataChart.on('error', onError);
72+
}
73+
5174
this.googleDataChart.execute();
5275
}
5376
} else if (this.googleDataChart != null) {
@@ -58,9 +81,12 @@ export default class DataChart<O> extends React.Component<DataChartProps<O>> {
5881

5982
render(): React.ReactNode {
6083
return (
61-
<div ref={this.elementRef}>
84+
<div style={this.props.style} className={this.props.className} ref={this.elementRef}>
6285
{this.googleDataChart == null ? this.props.children : undefined}
6386
</div>
6487
);
6588
}
6689
}
90+
91+
DataChart.contextType = GoogleAnalyticsContext;
92+
export default DataChart;

0 commit comments

Comments
 (0)