├── .nvmrc ├── provisioning ├── datasources │ ├── .gitkeep │ └── datasources.yml ├── README.md └── dashboards │ ├── default.yaml │ ├── v0.3.0.json │ ├── v0.2.2.json │ ├── v0.2.3.json │ └── default.json ├── .config ├── .cprc.json ├── webpack │ ├── constants.ts │ ├── publicPath.ts │ ├── utils.ts │ └── webpack.config.ts ├── .prettierrc.js ├── entrypoint.sh ├── .eslintrc ├── types │ └── custom.d.ts ├── tsconfig.json ├── jest │ ├── mocks │ │ └── react-inlinesvg.tsx │ └── utils.js ├── jest-setup.js ├── jest.config.js ├── supervisord │ └── supervisord.conf ├── Dockerfile └── README.md ├── .eslintrc ├── tsconfig.json ├── .vscode └── settings.json ├── jest-setup.js ├── img ├── api-check.png └── serviceaccount.png ├── src ├── img │ ├── query.png │ ├── setting.png │ ├── after-setting.png │ └── logo.svg ├── module.ts ├── QueryEditorCommon.tsx ├── plugin.json ├── DropZone.tsx ├── JWTConfig.tsx ├── types.ts ├── ConfigEditor.tsx ├── DataSource.ts ├── Filter.tsx └── QueryEditorUA.tsx ├── .dockerIgnore ├── .prettierrc.js ├── mage.go ├── pkg ├── gav3 │ ├── const.go │ ├── model.go │ ├── client.go │ ├── analytics.go │ └── grafana.go ├── main.go ├── setting │ └── setting.go ├── analytics.go ├── gav4 │ ├── model.go │ ├── const.go │ └── client.go ├── util │ └── util.go ├── model │ └── models.go └── datasource.go ├── jest.config.js ├── .editorconfig ├── config.ini ├── Magefile.go ├── FAQ.md ├── .gitignore ├── .github ├── ISSUE_TEMPLATE │ ├── feature_request.md │ └── bug_report.md └── workflows │ ├── release.yml │ ├── codeql-analysis.yml │ ├── playwright.yml │ └── ci.yaml ├── appveyor.yml ├── Dockerfile-debug ├── docker-compose.yaml ├── tests ├── config │ └── configEditor.spec.ts └── query │ └── queryEdiyor.spec.ts ├── .circleci └── config.yml ├── playwright.config.ts ├── package.json ├── CHANGELOG.md ├── README.md ├── go.mod └── LICENSE /.nvmrc: -------------------------------------------------------------------------------- 1 | 20 2 | -------------------------------------------------------------------------------- /provisioning/datasources/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.config/.cprc.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "4.16.0" 3 | } 4 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./.config/.eslintrc" 3 | } 4 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./.config/tsconfig.json" 3 | } 4 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "typescript.tsdk": "node_modules/typescript/lib" 3 | } 4 | -------------------------------------------------------------------------------- /jest-setup.js: -------------------------------------------------------------------------------- 1 | // Jest setup provided by Grafana scaffolding 2 | import './.config/jest-setup'; 3 | -------------------------------------------------------------------------------- /.config/webpack/constants.ts: -------------------------------------------------------------------------------- 1 | export const SOURCE_DIR = 'src'; 2 | export const DIST_DIR = 'dist'; 3 | -------------------------------------------------------------------------------- /img/api-check.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/blackcowmoo/grafana-google-analytics-datasource/HEAD/img/api-check.png -------------------------------------------------------------------------------- /src/img/query.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/blackcowmoo/grafana-google-analytics-datasource/HEAD/src/img/query.png -------------------------------------------------------------------------------- /src/img/setting.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/blackcowmoo/grafana-google-analytics-datasource/HEAD/src/img/setting.png -------------------------------------------------------------------------------- /img/serviceaccount.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/blackcowmoo/grafana-google-analytics-datasource/HEAD/img/serviceaccount.png -------------------------------------------------------------------------------- /src/img/after-setting.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/blackcowmoo/grafana-google-analytics-datasource/HEAD/src/img/after-setting.png -------------------------------------------------------------------------------- /.dockerIgnore: -------------------------------------------------------------------------------- 1 | .git 2 | .github 3 | .circleci 4 | .vscode 5 | coverage 6 | dist 7 | node_modules 8 | LICENSE 9 | README.md 10 | docker* 11 | Docker* -------------------------------------------------------------------------------- /.prettierrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | // Prettier configuration provided by Grafana scaffolding 3 | ...require('./.config/.prettierrc.js'), 4 | }; 5 | -------------------------------------------------------------------------------- /provisioning/README.md: -------------------------------------------------------------------------------- 1 | For more information see [Provision dashboards and data sources](https://grafana.com/tutorials/provision-dashboards-and-data-sources/) -------------------------------------------------------------------------------- /mage.go: -------------------------------------------------------------------------------- 1 | //go:build ignore 2 | // +build ignore 3 | 4 | package main 5 | 6 | import ( 7 | "github.com/magefile/mage/mage" 8 | "os" 9 | ) 10 | 11 | func main() { os.Exit(mage.Main()) } 12 | -------------------------------------------------------------------------------- /provisioning/dashboards/default.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: 1 2 | 3 | providers: 4 | - name: Default 5 | type: file 6 | options: 7 | path: /etc/grafana/provisioning/dashboards 8 | allowUiUpdates: true 9 | -------------------------------------------------------------------------------- /pkg/gav3/const.go: -------------------------------------------------------------------------------- 1 | package gav3 2 | 3 | const ( 4 | GaDefaultIdx = 1 5 | GaManageMaxResult = 1000 6 | GaReportMaxResult = 100000 7 | GaMetadataURL = "https://www.googleapis.com/analytics/v3/metadata/ga/columns?pp=1" 8 | ) 9 | -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | // force timezone to UTC to allow tests to work regardless of local timezone 2 | // generally used by snapshots, but can affect specific tests 3 | process.env.TZ = 'UTC'; 4 | 5 | module.exports = { 6 | // Jest configuration provided by Grafana scaffolding 7 | ...require('./.config/jest.config'), 8 | }; 9 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | indent_size = 2 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | max_line_length = 120 11 | 12 | [*.{js,ts,tsx,scss}] 13 | quote_type = single 14 | end_of_line = lf 15 | [*.md] 16 | trim_trailing_whitespace = false 17 | -------------------------------------------------------------------------------- /config.ini: -------------------------------------------------------------------------------- 1 | app_mode = development 2 | instance_name = grafana-oss 3 | 4 | [plugins] 5 | enable_alpha = true 6 | app_tls_skip_verify_insecure = false 7 | allow_loading_unsigned_plugins = blackcowmoo-googleanalytics-datasource 8 | 9 | [auth] 10 | login_cookie_name = grafana_oss_session 11 | 12 | [panels] 13 | disable_sanitize_html = false 14 | 15 | [log] 16 | level = debug -------------------------------------------------------------------------------- /provisioning/datasources/datasources.yml: -------------------------------------------------------------------------------- 1 | apiVersion: 1 2 | 3 | datasources: 4 | - name: ' Google Analytics' 5 | type: 'blackcowmoo-googleanalytics-datasource' 6 | access: proxy 7 | isDefault: false 8 | orgId: 1 9 | version: 1 10 | editable: true 11 | uid: lcc3108_test 12 | jsonData: 13 | path: '/resources' 14 | secureJsonData: 15 | apiKey: 'api-key' 16 | -------------------------------------------------------------------------------- /Magefile.go: -------------------------------------------------------------------------------- 1 | //go:build mage 2 | // +build mage 3 | 4 | package main 5 | 6 | import ( 7 | "fmt" 8 | // mage:import 9 | build "github.com/grafana/grafana-plugin-sdk-go/build" 10 | ) 11 | 12 | // Hello prints a message (shows that you can define custom Mage targets). 13 | func Hello() { 14 | fmt.Println("hello plugin developer!") 15 | } 16 | 17 | // Default configures the default target. 18 | var Default = build.BuildAll 19 | -------------------------------------------------------------------------------- /.config/.prettierrc.js: -------------------------------------------------------------------------------- 1 | /* 2 | * ⚠️⚠️⚠️ THIS FILE WAS SCAFFOLDED BY `@grafana/create-plugin`. DO NOT EDIT THIS FILE DIRECTLY. ⚠️⚠️⚠️ 3 | * 4 | * In order to extend the configuration follow the steps in .config/README.md 5 | */ 6 | 7 | module.exports = { 8 | endOfLine: 'auto', 9 | printWidth: 120, 10 | trailingComma: 'es5', 11 | semi: true, 12 | jsxSingleQuote: false, 13 | singleQuote: true, 14 | useTabs: false, 15 | tabWidth: 2, 16 | }; 17 | -------------------------------------------------------------------------------- /src/module.ts: -------------------------------------------------------------------------------- 1 | import { DataSourcePlugin } from '@grafana/data'; 2 | import { QueryEditorCommon } from 'QueryEditorCommon'; 3 | import { ConfigEditor } from './ConfigEditor'; 4 | import { DataSource } from './DataSource'; 5 | import { GADataSourceOptions, GAQuery } from './types'; 6 | 7 | export const plugin = new DataSourcePlugin(DataSource) 8 | .setConfigEditor(ConfigEditor) 9 | .setQueryEditor(QueryEditorCommon); 10 | -------------------------------------------------------------------------------- /.config/entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if [ "${DEV}" = "false" ]; then 4 | echo "Starting test mode" 5 | exec /run.sh 6 | fi 7 | 8 | echo "Starting development mode" 9 | 10 | if grep -i -q alpine /etc/issue; then 11 | exec /usr/bin/supervisord -c /etc/supervisord.conf 12 | elif grep -i -q ubuntu /etc/issue; then 13 | exec /usr/bin/supervisord -c /etc/supervisor/supervisord.conf 14 | else 15 | echo 'ERROR: Unsupported base image' 16 | exit 1 17 | fi 18 | 19 | -------------------------------------------------------------------------------- /pkg/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "os" 5 | 6 | "github.com/grafana/grafana-plugin-sdk-go/backend/datasource" 7 | "github.com/grafana/grafana-plugin-sdk-go/backend/log" 8 | 9 | // window timezone issue #101 10 | _ "time/tzdata" 11 | ) 12 | 13 | func main() { 14 | if err := datasource.Manage("blackcowmoo-googleanalytics-datasource", NewDataSource, datasource.ManageOpts{}); err != nil { 15 | log.DefaultLogger.Error(err.Error()) 16 | os.Exit(1) 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /FAQ.md: -------------------------------------------------------------------------------- 1 | ## Configuration error 2 | 3 | 1. [google api check](https://console.cloud.google.com/apis/dashboard) 4 | ![img](https://github.com/blackcowmoo/Grafana-Google-Analytics-DataSource/blob/master/img/api-check.png?raw=true) 5 | 2. [service account check](https://console.cloud.google.com/apis/credentials) 6 | ![img](https://github.com/blackcowmoo/Grafana-Google-Analytics-DataSource/blob/master/img/serviceaccount.png?raw=true) 7 | 3. [analytics account access check](https://analytics.google.com/analytics) -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | node_modules/ 9 | 10 | # Runtime data 11 | pids 12 | *.pid 13 | *.seed 14 | *.pid.lock 15 | 16 | # Directory for instrumented libs generated by jscoverage/JSCover 17 | lib-cov 18 | 19 | # Coverage directory used by tools like istanbul 20 | coverage 21 | 22 | # Compiled binary addons (https://nodejs.org/api/addons.html) 23 | dist/ 24 | artifacts/ 25 | work/ 26 | ci/ 27 | e2e-results/ 28 | 29 | tests/credentials 30 | cypress/videos 31 | cypress/report.json 32 | # Editor 33 | .idea 34 | .vscode 35 | /test-results/ 36 | /playwright-report/ 37 | /blob-report/ 38 | /playwright/.cache/ 39 | /playwright/.auth/ 40 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: "[FEATURE]" 5 | labels: enhancement 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. 21 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: "[BUG]" 5 | labels: bug 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **Grafana Version & Plugin Version** 14 | 15 | **To Reproduce** 16 | Steps to reproduce the behavior: 17 | 1. Go to '...' 18 | 2. Click on '....' 19 | 3. Scroll down to '....' 20 | 4. See error 21 | 22 | **Expected behavior** 23 | A clear and concise description of what you expected to happen. 24 | 25 | **Screenshots** 26 | If applicable, add screenshots to help explain your problem. 27 | 28 | **Additional context** 29 | Add any other context about the problem here. 30 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | # Test against the latest version of this Node.js version 2 | environment: 3 | nodejs_version: "10" 4 | 5 | # Local NPM Modules 6 | cache: 7 | - node_modules 8 | 9 | # Install scripts. (runs after repo cloning) 10 | install: 11 | # Get the latest stable version of Node.js or io.js 12 | - ps: Install-Product node $env:nodejs_version 13 | # install modules 14 | - npm install -g yarn --quiet 15 | - yarn install --pure-lockfile 16 | 17 | # Post-install test scripts. 18 | test_script: 19 | # Output useful info for debugging. 20 | - node --version 21 | - npm --version 22 | 23 | # Run the build 24 | build_script: 25 | - yarn dev # This will also run prettier! 26 | - yarn build # make sure both scripts work 27 | -------------------------------------------------------------------------------- /.config/.eslintrc: -------------------------------------------------------------------------------- 1 | /* 2 | * ⚠️⚠️⚠️ THIS FILE WAS SCAFFOLDED BY `@grafana/create-plugin`. DO NOT EDIT THIS FILE DIRECTLY. ⚠️⚠️⚠️ 3 | * 4 | * In order to extend the configuration follow the steps in 5 | * https://grafana.com/developers/plugin-tools/create-a-plugin/extend-a-plugin/extend-configurations#extend-the-eslint-config 6 | */ 7 | { 8 | "extends": ["@grafana/eslint-config"], 9 | "root": true, 10 | "rules": { 11 | "react/prop-types": "off" 12 | }, 13 | "overrides": [ 14 | { 15 | "plugins": ["deprecation"], 16 | "files": ["src/**/*.{ts,tsx}"], 17 | "rules": { 18 | "deprecation/deprecation": "warn" 19 | }, 20 | "parserOptions": { 21 | "project": "./tsconfig.json" 22 | } 23 | } 24 | ] 25 | } 26 | -------------------------------------------------------------------------------- /.config/types/custom.d.ts: -------------------------------------------------------------------------------- 1 | // Image declarations 2 | declare module '*.gif' { 3 | const src: string; 4 | export default src; 5 | } 6 | 7 | declare module '*.jpg' { 8 | const src: string; 9 | export default src; 10 | } 11 | 12 | declare module '*.jpeg' { 13 | const src: string; 14 | export default src; 15 | } 16 | 17 | declare module '*.png' { 18 | const src: string; 19 | export default src; 20 | } 21 | 22 | declare module '*.webp' { 23 | const src: string; 24 | export default src; 25 | } 26 | 27 | declare module '*.svg' { 28 | const content: string; 29 | export default content; 30 | } 31 | 32 | // Font declarations 33 | declare module '*.woff'; 34 | declare module '*.woff2'; 35 | declare module '*.eot'; 36 | declare module '*.ttf'; 37 | declare module '*.otf'; 38 | -------------------------------------------------------------------------------- /.config/tsconfig.json: -------------------------------------------------------------------------------- 1 | /* 2 | * ⚠️⚠️⚠️ THIS FILE WAS SCAFFOLDED BY `@grafana/create-plugin`. DO NOT EDIT THIS FILE DIRECTLY. ⚠️⚠️⚠️ 3 | * 4 | * In order to extend the configuration follow the steps in 5 | * https://grafana.com/developers/plugin-tools/create-a-plugin/extend-a-plugin/extend-configurations#extend-the-typescript-config 6 | */ 7 | { 8 | "compilerOptions": { 9 | "alwaysStrict": true, 10 | "declaration": false, 11 | "rootDir": "../src", 12 | "baseUrl": "../src", 13 | "typeRoots": ["../node_modules/@types"], 14 | "resolveJsonModule": true 15 | }, 16 | "ts-node": { 17 | "compilerOptions": { 18 | "module": "commonjs", 19 | "target": "es5", 20 | "esModuleInterop": true 21 | }, 22 | "transpileOnly": true 23 | }, 24 | "include": ["../src", "./types"], 25 | "extends": "@grafana/tsconfig" 26 | } 27 | -------------------------------------------------------------------------------- /.config/webpack/publicPath.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * ⚠️⚠️⚠️ THIS FILE WAS SCAFFOLDED BY `@grafana/create-plugin`. DO NOT EDIT THIS FILE DIRECTLY. ⚠️⚠️⚠️ 3 | * 4 | * This file dynamically sets the public path at runtime based on the location of the plugin's AMD module. 5 | * It relies on the magic `module` which is defined by the AMD loader. 6 | * https://github.com/requirejs/requirejs/wiki/Differences-between-the-simplified-CommonJS-wrapper-and-standard-AMD-define#module 7 | * 8 | * We fallback to the plugin root so that older versions of Grafana will continue to load the plugin correctly. 9 | */ 10 | 11 | // @ts-nocheck 12 | import amdMetaModule from 'amd-module'; 13 | 14 | __webpack_public_path__ = 15 | amdMetaModule && amdMetaModule.uri 16 | ? amdMetaModule.uri.slice(0, amdMetaModule.uri.lastIndexOf('/') + 1) 17 | : 'public/plugins/blackcowmoo-googleanalytics-datasource/'; 18 | -------------------------------------------------------------------------------- /Dockerfile-debug: -------------------------------------------------------------------------------- 1 | FROM grafana/grafana:9.5.2-ubuntu 2 | 3 | USER root 4 | WORKDIR /root 5 | 6 | ENV GOFLAGS -buildvcs=false 7 | 8 | RUN apt-get -y update 9 | RUN apt-get -y install git build-essential 10 | 11 | RUN curl -L https://golang.org/dl/go1.18.linux-amd64.tar.gz > go1.18.linux-amd64.tar.gz 12 | 13 | RUN rm -rf /usr/local/go && \ 14 | tar -C /usr/local -xzf go1.18.linux-amd64.tar.gz 15 | 16 | RUN touch README; printf "~~~~~~ START THE DLV SERVER WITH THIS COMMAND BEFORE RUNNING IDE DEBUGGER ~~~~~~ \r\ndlv attach --headless --listen=:2345 PID\r\n\r\n" >> README 17 | 18 | RUN echo "export PATH=$PATH:/usr/local/go/bin:~/go/bin" >> ~/.bashrc 19 | RUN echo "cat ~/README" >> ~/.bashrc 20 | 21 | RUN /usr/local/go/bin/go install github.com/go-delve/delve/cmd/dlv@latest 22 | RUN git clone https://github.com/magefile/mage; \ 23 | cd mage; \ 24 | export PATH=$PATH:/usr/local/go/bin; \ 25 | go run bootstrap.go 26 | -------------------------------------------------------------------------------- /pkg/setting/setting.go: -------------------------------------------------------------------------------- 1 | package setting 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | 7 | "github.com/grafana/grafana-plugin-sdk-go/backend" 8 | ) 9 | 10 | type DatasourceSettings struct { 11 | Version string `json:"version"` 12 | } 13 | 14 | // DatasourceSecretSettings contains Google Sheets API authentication properties. 15 | type DatasourceSecretSettings struct { 16 | JWT string `json:"jwt"` 17 | ProfileId string `json:"profileId"` 18 | } 19 | 20 | // LoadSettings gets the relevant settings from the plugin context 21 | func LoadSettings(ctx backend.PluginContext) (*DatasourceSecretSettings, error) { 22 | model := &DatasourceSecretSettings{} 23 | 24 | settings := ctx.DataSourceInstanceSettings 25 | err := json.Unmarshal(settings.JSONData, &model) 26 | if err != nil { 27 | return nil, fmt.Errorf("error reading settings: %s", err.Error()) 28 | } 29 | 30 | model.JWT = settings.DecryptedSecureJSONData["jwt"] 31 | 32 | return model, nil 33 | } 34 | -------------------------------------------------------------------------------- /.config/jest/mocks/react-inlinesvg.tsx: -------------------------------------------------------------------------------- 1 | // Due to the grafana/ui Icon component making fetch requests to 2 | // `/public/img/icon/.svg` we need to mock react-inlinesvg to prevent 3 | // the failed fetch requests from displaying errors in console. 4 | 5 | import React from 'react'; 6 | 7 | type Callback = (...args: any[]) => void; 8 | 9 | export interface StorageItem { 10 | content: string; 11 | queue: Callback[]; 12 | status: string; 13 | } 14 | 15 | export const cacheStore: { [key: string]: StorageItem } = Object.create(null); 16 | 17 | const SVG_FILE_NAME_REGEX = /(.+)\/(.+)\.svg$/; 18 | 19 | const InlineSVG = ({ src }: { src: string }) => { 20 | // testId will be the file name without extension (e.g. `public/img/icons/angle-double-down.svg` -> `angle-double-down`) 21 | const testId = src.replace(SVG_FILE_NAME_REGEX, '$2'); 22 | return ; 23 | }; 24 | 25 | export default InlineSVG; 26 | -------------------------------------------------------------------------------- /.config/jest/utils.js: -------------------------------------------------------------------------------- 1 | /* 2 | * ⚠️⚠️⚠️ THIS FILE WAS SCAFFOLDED BY `@grafana/create-plugin`. DO NOT EDIT THIS FILE DIRECTLY. ⚠️⚠️⚠️ 3 | * 4 | * In order to extend the configuration follow the steps in .config/README.md 5 | */ 6 | 7 | /* 8 | * This utility function is useful in combination with jest `transformIgnorePatterns` config 9 | * to transform specific packages (e.g.ES modules) in a projects node_modules folder. 10 | */ 11 | const nodeModulesToTransform = (moduleNames) => `node_modules\/(?!.*(${moduleNames.join('|')})\/.*)`; 12 | 13 | // Array of known nested grafana package dependencies that only bundle an ESM version 14 | const grafanaESModules = [ 15 | '.pnpm', // Support using pnpm symlinked packages 16 | '@grafana/schema', 17 | 'd3', 18 | 'd3-color', 19 | 'd3-force', 20 | 'd3-interpolate', 21 | 'd3-scale-chromatic', 22 | 'ol', 23 | 'react-colorful', 24 | 'rxjs', 25 | 'uuid', 26 | ]; 27 | 28 | module.exports = { 29 | nodeModulesToTransform, 30 | grafanaESModules, 31 | }; 32 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | # This GitHub Action automates the process of building Grafana plugins. 2 | # (For more information, see https://github.com/grafana/plugin-actions/blob/main/build-plugin/README.md) 3 | name: Release 4 | 5 | on: 6 | push: 7 | tags: 8 | - 'v*' # Run workflow on version tags, e.g. v1.0.0. 9 | 10 | permissions: read-all 11 | 12 | jobs: 13 | release: 14 | permissions: 15 | contents: write 16 | runs-on: ubuntu-latest 17 | steps: 18 | - uses: actions/checkout@v4 19 | - uses: grafana/plugin-actions/build-plugin@release 20 | # Uncomment to enable plugin signing 21 | # (For more info on how to generate the access policy token see https://grafana.com/developers/plugin-tools/publish-a-plugin/sign-a-plugin#generate-an-access-policy-token) 22 | with: 23 | # Make sure to save the token in your repository secrets 24 | policy_token: ${{ secrets.GRAFANA_API_KEY }} 25 | # Usage of GRAFANA_API_KEY is deprecated, prefer `policy_token` option above 26 | #grafana_token: $ 27 | -------------------------------------------------------------------------------- /src/QueryEditorCommon.tsx: -------------------------------------------------------------------------------- 1 | import { QueryEditorProps } from '@grafana/data'; 2 | import { DataSource } from 'DataSource'; 3 | import { QueryEditorGA4 } from 'QueryEditorGA4'; 4 | import { QueryEditorUA } from 'QueryEditorUA'; 5 | import React, { PureComponent } from 'react'; 6 | import { GADataSourceOptions, GAQuery } from 'types'; 7 | 8 | type Props = QueryEditorProps; 9 | 10 | 11 | export class QueryEditorCommon extends PureComponent { 12 | constructor(props: Readonly) { 13 | super(props); 14 | this.props.query.version = props.datasource.getGaVersion() 15 | } 16 | render() { 17 | const { query, datasource, onChange, onRunQuery } = this.props; 18 | const { version } = query 19 | if (version === "v4") { 20 | return 21 | } else { 22 | return 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /docker-compose.yaml: -------------------------------------------------------------------------------- 1 | services: 2 | grafana: 3 | user: root 4 | container_name: 'blackcowmoo-googleanalytics-datasource' 5 | 6 | build: 7 | context: ./.config 8 | args: 9 | grafana_image: ${GRAFANA_IMAGE:-grafana-enterprise} 10 | grafana_version: ${GRAFANA_VERSION:-10.4.0} 11 | development: ${DEVELOPMENT:-false} 12 | ports: 13 | - 3000:3000/tcp 14 | - 2345:2345/tcp # delve 15 | security_opt: 16 | - "apparmor:unconfined" 17 | - "seccomp:unconfined" 18 | cap_add: 19 | - SYS_PTRACE 20 | volumes: 21 | # - ~/.grafana:/var/lib/grafana 22 | - ./dist:/var/lib/grafana/plugins/blackcowmoo-googleanalytics-datasource 23 | - ./provisioning:/etc/grafana/provisioning 24 | - .:/root/blackcowmoo-googleanalytics-datasource 25 | 26 | environment: 27 | NODE_ENV: development 28 | GF_LOG_FILTERS: plugin.blackcowmoo-googleanalytics-datasource:debug 29 | GF_LOG_LEVEL: debug 30 | GF_DATAPROXY_LOGGING: 1 31 | GF_PLUGINS_ALLOW_LOADING_UNSIGNED_PLUGINS: blackcowmoo-googleanalytics-datasource 32 | -------------------------------------------------------------------------------- /.config/jest-setup.js: -------------------------------------------------------------------------------- 1 | /* 2 | * ⚠️⚠️⚠️ THIS FILE WAS SCAFFOLDED BY `@grafana/create-plugin`. DO NOT EDIT THIS FILE DIRECTLY. ⚠️⚠️⚠️ 3 | * 4 | * In order to extend the configuration follow the steps in 5 | * https://grafana.com/developers/plugin-tools/create-a-plugin/extend-a-plugin/extend-configurations#extend-the-jest-config 6 | */ 7 | 8 | import '@testing-library/jest-dom'; 9 | import { TextEncoder, TextDecoder } from 'util'; 10 | 11 | Object.assign(global, { TextDecoder, TextEncoder }); 12 | 13 | // https://jestjs.io/docs/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom 14 | Object.defineProperty(global, 'matchMedia', { 15 | writable: true, 16 | value: jest.fn().mockImplementation((query) => ({ 17 | matches: false, 18 | media: query, 19 | onchange: null, 20 | addListener: jest.fn(), // deprecated 21 | removeListener: jest.fn(), // deprecated 22 | addEventListener: jest.fn(), 23 | removeEventListener: jest.fn(), 24 | dispatchEvent: jest.fn(), 25 | })), 26 | }); 27 | 28 | HTMLCanvasElement.prototype.getContext = () => {}; 29 | -------------------------------------------------------------------------------- /pkg/analytics.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | 6 | "github.com/blackcowmoo/grafana-google-analytics-dataSource/pkg/model" 7 | "github.com/blackcowmoo/grafana-google-analytics-dataSource/pkg/setting" 8 | "github.com/grafana/grafana-plugin-sdk-go/backend" 9 | "github.com/grafana/grafana-plugin-sdk-go/data" 10 | ) 11 | 12 | type GoogleAnalytics interface { 13 | Query(context.Context, *setting.DatasourceSecretSettings, backend.DataQuery) (*data.Frames, error) 14 | GetAccountSummaries(context.Context, *setting.DatasourceSecretSettings) ([]*model.AccountSummary, error) 15 | GetTimezone(context.Context, *setting.DatasourceSecretSettings, string, string, string) (string, error) 16 | GetServiceLevel(context.Context, *setting.DatasourceSecretSettings, string, string) (string, error) 17 | GetDimensions(context.Context, *setting.DatasourceSecretSettings, string) ([]model.MetadataItem, error) 18 | GetRealtimeDimensions(context.Context, *setting.DatasourceSecretSettings, string) ([]model.MetadataItem, error) 19 | GetRealTimeMetrics(context.Context, *setting.DatasourceSecretSettings, string) ([]model.MetadataItem, error) 20 | GetMetrics(context.Context, *setting.DatasourceSecretSettings, string) ([]model.MetadataItem, error) 21 | CheckHealth(context.Context, *setting.DatasourceSecretSettings) (*backend.CheckHealthResult, error) 22 | } 23 | -------------------------------------------------------------------------------- /pkg/gav4/model.go: -------------------------------------------------------------------------------- 1 | package gav4 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | "time" 7 | 8 | "github.com/blackcowmoo/grafana-google-analytics-dataSource/pkg/model" 9 | "github.com/grafana/grafana-plugin-sdk-go/backend" 10 | "github.com/grafana/grafana-plugin-sdk-go/backend/log" 11 | ) 12 | 13 | // GetQueryModel returns the well typed query model 14 | func GetQueryModel(query backend.DataQuery) (*model.QueryModel, error) { 15 | model := &model.QueryModel{ 16 | PageSize: GaReportMaxResult, 17 | PageToken: "", 18 | Offset: 0, 19 | } 20 | err := json.Unmarshal(query.JSON, &model) 21 | if err != nil { 22 | return nil, fmt.Errorf("error reading query: %s", err.Error()) 23 | } 24 | 25 | // Copy directly from the well typed query 26 | timezone, err := time.LoadLocation(model.Timezone) 27 | if err != nil { 28 | return nil, fmt.Errorf("error get timezone %s", err.Error()) 29 | } 30 | 31 | log.DefaultLogger.Debug("query timezone", "timezone", timezone.String()) 32 | 33 | model.StartDate = query.TimeRange.From.In(timezone).Format("2006-01-02") 34 | model.EndDate = query.TimeRange.To.In(timezone).Format("2006-01-02") 35 | 36 | model.From = query.TimeRange.From 37 | model.To = query.TimeRange.To 38 | if model.TimeDimension != "" { 39 | model.Dimensions = append([]string{model.TimeDimension}, model.Dimensions...) 40 | } 41 | 42 | // model.TimeRange = query.TimeRange 43 | // model.MaxDataPoints = query.MaxDataPoints 44 | return model, nil 45 | } 46 | -------------------------------------------------------------------------------- /tests/config/configEditor.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect, test } from '@grafana/plugin-e2e'; 2 | import { GADataSourceOptions, GASecureJsonData } from '../../src/types'; 3 | 4 | test('"Save & test" should be successful when configuration is valid', async ({ 5 | gotoDataSourceConfigPage, 6 | readProvisionedDataSource, 7 | page, 8 | }) => { 9 | const ds = await readProvisionedDataSource({ fileName: 'datasources.yml' }); 10 | const configPage = await gotoDataSourceConfigPage(ds.uid); 11 | const resetButton = await page.getByText('Upload another JWT file').isVisible() 12 | if(resetButton){ 13 | await page.getByText('Upload another JWT file').click() 14 | } 15 | await page.locator('input[accept="application/json"]').setInputFiles('./tests/credentials/grafana-success.json') 16 | await expect(configPage.saveAndTest()).toBeOK(); 17 | }); 18 | 19 | test('"Save & test" should fail when configuration is invalid', async ({ 20 | createDataSourceConfigPage, 21 | readProvisionedDataSource, 22 | page, 23 | }) => { 24 | const ds = await readProvisionedDataSource({ fileName: 'datasources.yml' }); 25 | const configPage = await createDataSourceConfigPage({ type: ds.type, deleteDataSourceAfterTest: true }); 26 | await page.locator('input[accept="application/json"]').setInputFiles('./tests/credentials/grafana-fail.json') 27 | await expect(configPage.saveAndTest()).not.toBeOK(); 28 | await expect(configPage).toHaveAlert('error'); 29 | }); 30 | 31 | 32 | -------------------------------------------------------------------------------- /src/plugin.json: -------------------------------------------------------------------------------- 1 | { 2 | "type": "datasource", 3 | "name": " Google Analytics", 4 | "id": "blackcowmoo-googleanalytics-datasource", 5 | "metrics": true, 6 | "backend": true, 7 | "executable": "gpx_blackcowmoo-googleanalytics-datasource", 8 | "info": { 9 | "description": " GoogleAnalytics Visualize & datasource", 10 | "author": { 11 | "name": "blackcowmoo", 12 | "url": "https://github.com/blackcowmoo" 13 | }, 14 | "keywords": [ 15 | "GA", 16 | "ga", 17 | "GoogleAnalytics", 18 | "Google", 19 | "googleanalytics", 20 | "google", 21 | "analytics" 22 | ], 23 | "logos": { 24 | "small": "img/logo.svg", 25 | "large": "img/logo.svg" 26 | }, 27 | "links": [ 28 | { 29 | "name": "Website", 30 | "url": "https://github.com/blackcowmoo/grafana-google-analytics-datasource" 31 | }, 32 | { 33 | "name": "License", 34 | "url": "https://github.com/blackcowmoo/grafana-google-analytics-datasource/blob/master/LICENSE" 35 | } 36 | ], 37 | "screenshots": [ 38 | { 39 | "name": "Setting", 40 | "path": "img/setting.png" 41 | }, 42 | { 43 | "name": "After Setting", 44 | "path": "img/after-setting.png" 45 | }, 46 | { 47 | "name": "Dashboard", 48 | "path": "img/query.png" 49 | } 50 | ], 51 | "version": "%VERSION%", 52 | "updated": "%TODAY%" 53 | }, 54 | "dependencies": { 55 | "grafanaDependency": ">=9.0.0", 56 | "plugins": [] 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /.config/jest.config.js: -------------------------------------------------------------------------------- 1 | /* 2 | * ⚠️⚠️⚠️ THIS FILE WAS SCAFFOLDED BY `@grafana/create-plugin`. DO NOT EDIT THIS FILE DIRECTLY. ⚠️⚠️⚠️ 3 | * 4 | * In order to extend the configuration follow the steps in 5 | * https://grafana.com/developers/plugin-tools/create-a-plugin/extend-a-plugin/extend-configurations#extend-the-jest-config 6 | */ 7 | 8 | const path = require('path'); 9 | const { grafanaESModules, nodeModulesToTransform } = require('./jest/utils'); 10 | 11 | module.exports = { 12 | moduleNameMapper: { 13 | '\\.(css|scss|sass)$': 'identity-obj-proxy', 14 | 'react-inlinesvg': path.resolve(__dirname, 'jest', 'mocks', 'react-inlinesvg.tsx'), 15 | }, 16 | modulePaths: ['/src'], 17 | setupFilesAfterEnv: ['/jest-setup.js'], 18 | testEnvironment: 'jest-environment-jsdom', 19 | testMatch: [ 20 | '/src/**/__tests__/**/*.{js,jsx,ts,tsx}', 21 | '/src/**/*.{spec,test,jest}.{js,jsx,ts,tsx}', 22 | '/src/**/*.{spec,test,jest}.{js,jsx,ts,tsx}', 23 | ], 24 | transform: { 25 | '^.+\\.(t|j)sx?$': [ 26 | '@swc/jest', 27 | { 28 | sourceMaps: 'inline', 29 | jsc: { 30 | parser: { 31 | syntax: 'typescript', 32 | tsx: true, 33 | decorators: false, 34 | dynamicImport: true, 35 | }, 36 | }, 37 | }, 38 | ], 39 | }, 40 | // Jest will throw `Cannot use import statement outside module` if it tries to load an 41 | // ES module without it being transformed first. ./config/README.md#esm-errors-with-jest 42 | transformIgnorePatterns: [nodeModulesToTransform(grafanaESModules)], 43 | }; 44 | -------------------------------------------------------------------------------- /.config/supervisord/supervisord.conf: -------------------------------------------------------------------------------- 1 | [supervisord] 2 | nodaemon=true 3 | user=root 4 | 5 | [program:grafana] 6 | user=root 7 | directory=/var/lib/grafana 8 | command=bash -c 'while [ ! -f /root/blackcowmoo-googleanalytics-datasource/dist/gpx_blackcowmoo-googleanalytics-datasource* ]; do sleep 1; done; /run.sh' 9 | stdout_logfile=/dev/fd/1 10 | stdout_logfile_maxbytes=0 11 | redirect_stderr=true 12 | killasgroup=true 13 | stopasgroup=true 14 | autostart=true 15 | 16 | [program:delve] 17 | user=root 18 | command=/bin/bash -c 'pid=""; while [ -z "$pid" ]; do pid=$(pgrep -f gpx_blackcowmoo-googleanalytics-datasource); done; /root/go/bin/dlv attach --api-version=2 --headless --continue --accept-multiclient --listen=:2345 $pid' 19 | stdout_logfile=/dev/fd/1 20 | stdout_logfile_maxbytes=0 21 | redirect_stderr=true 22 | killasgroup=false 23 | stopasgroup=false 24 | autostart=true 25 | autorestart=true 26 | 27 | [program:build-watcher] 28 | user=root 29 | command=/bin/bash -c 'while inotifywait -e modify,create,delete -r /var/lib/grafana/plugins/blackcowmoo-googleanalytics-datasource; do echo "Change detected, restarting delve...";supervisorctl restart delve; done' 30 | stdout_logfile=/dev/fd/1 31 | stdout_logfile_maxbytes=0 32 | redirect_stderr=true 33 | killasgroup=true 34 | stopasgroup=true 35 | autostart=true 36 | 37 | [program:mage-watcher] 38 | user=root 39 | environment=PATH="/usr/local/go/bin:/root/go/bin:%(ENV_PATH)s" 40 | directory=/root/blackcowmoo-googleanalytics-datasource 41 | command=/bin/bash -c 'git config --global --add safe.directory /root/blackcowmoo-googleanalytics-datasource && mage -v watch' 42 | stdout_logfile=/dev/fd/1 43 | stdout_logfile_maxbytes=0 44 | redirect_stderr=true 45 | killasgroup=true 46 | stopasgroup=true 47 | autostart=true 48 | -------------------------------------------------------------------------------- /src/DropZone.tsx: -------------------------------------------------------------------------------- 1 | import React, { useMemo } from 'react'; 2 | import { DropzoneOptions, DropzoneRootProps, useDropzone } from 'react-dropzone'; 3 | 4 | const defaultBaseStyle = { 5 | display: 'flex', 6 | justifyContent: 'center', 7 | alignItems: 'center', 8 | height: '150px', 9 | backgroundColor: '#212327', 10 | padding: 24, 11 | borderWidth: 2, 12 | borderRadius: 2, 13 | borderColor: '#808080', 14 | borderStyle: 'dashed', 15 | outline: 'none', 16 | transition: 'border .24s ease-in-out', 17 | }; 18 | 19 | const defaultActiveStyle = { 20 | borderColor: '#2196f3', 21 | }; 22 | 23 | const defaultAcceptStyle = { 24 | borderColor: '#EF843C', 25 | }; 26 | 27 | const defaultRejectStyle = { 28 | borderColor: '#E24D42', 29 | }; 30 | 31 | type Style = { [key: string]: any }; 32 | 33 | export interface Props extends DropzoneOptions { 34 | children: React.ReactNode; 35 | baseStyle?: Style; 36 | activeStyle?: Style; 37 | acceptStyle?: Style; 38 | rejectStyle?: Style; 39 | } 40 | 41 | export function DropZone({ 42 | children, 43 | baseStyle = {}, 44 | activeStyle = {}, 45 | acceptStyle = {}, 46 | rejectStyle = {}, 47 | ...rest 48 | }: Props) { 49 | const { getRootProps, getInputProps, isDragActive, isDragAccept, isDragReject } = useDropzone(rest); 50 | 51 | const style = useMemo( 52 | () => ({ 53 | ...defaultBaseStyle, 54 | ...baseStyle, 55 | ...(isDragActive ? { ...defaultActiveStyle, ...activeStyle } : {}), 56 | ...(isDragAccept ? { ...defaultAcceptStyle, ...acceptStyle } : {}), 57 | ...(isDragReject ? { ...defaultRejectStyle, ...rejectStyle } : {}), 58 | }), 59 | [isDragActive, isDragAccept, isDragReject, activeStyle, acceptStyle, rejectStyle, baseStyle] 60 | ); 61 | 62 | return ( 63 |
64 | 65 | {children} 66 |
67 | ); 68 | } 69 | -------------------------------------------------------------------------------- /pkg/util/util.go: -------------------------------------------------------------------------------- 1 | package util 2 | 3 | import ( 4 | "encoding/json" 5 | "strings" 6 | "time" 7 | 8 | "github.com/grafana/grafana-plugin-sdk-go/backend/log" 9 | ) 10 | 11 | func Elapsed(what string) func() { 12 | start := time.Now() 13 | return func() { 14 | log.DefaultLogger.Debug("Elapsed", what, time.Since(start).String()) 15 | } 16 | } 17 | 18 | func padRightSide(str string, item string, count int) string { 19 | return str + strings.Repeat(item, count) 20 | } 21 | 22 | func ParseAndTimezoneTime(sTime string, timezone *time.Location) (*time.Time, error) { 23 | time, err := time.ParseInLocation("200601021504", padRightSide(sTime, "0", 12-len(sTime)), timezone) 24 | 25 | if err != nil { 26 | log.DefaultLogger.Error("timeConverter", "error", err) 27 | return nil, err 28 | } 29 | return &time, nil 30 | } 31 | 32 | func addTime(t1 time.Time, t2 time.Duration) time.Time { 33 | tmp := time.Time(t1) 34 | return tmp.Add(t2) 35 | } 36 | 37 | func subTime(t1 time.Time, t2 time.Duration) time.Time { 38 | return addTime(t1, t2*-1) 39 | } 40 | 41 | func SubOneHour(t1 time.Time) time.Time { 42 | return subTime(t1, time.Hour) 43 | } 44 | 45 | func SubOneDay(t1 time.Time) time.Time { 46 | return subTime(t1, time.Hour*24) 47 | } 48 | 49 | func SubOneMinute(t1 time.Time) time.Time { 50 | return subTime(t1, time.Minute) 51 | } 52 | 53 | func AddOneHour(t1 time.Time) time.Time { 54 | return addTime(t1, time.Hour) 55 | } 56 | 57 | func AddOneDay(t1 time.Time) time.Time { 58 | return addTime(t1, time.Hour*24) 59 | } 60 | 61 | func AddOneMinute(t1 time.Time) time.Time { 62 | return addTime(t1, time.Minute) 63 | } 64 | 65 | func FillArray(array []string, value string) []string { 66 | for i := range array { 67 | array[i] = value 68 | } 69 | return array 70 | } 71 | 72 | func TypeConverter[R any](data any) (*R, error) { 73 | var result R 74 | b, err := json.Marshal(&data) 75 | if err != nil { 76 | return nil, err 77 | } 78 | err = json.Unmarshal(b, &result) 79 | if err != nil { 80 | return nil, err 81 | } 82 | return &result, err 83 | } 84 | -------------------------------------------------------------------------------- /.config/webpack/utils.ts: -------------------------------------------------------------------------------- 1 | import fs from 'fs'; 2 | import process from 'process'; 3 | import os from 'os'; 4 | import path from 'path'; 5 | import { glob } from 'glob'; 6 | import { SOURCE_DIR } from './constants'; 7 | 8 | export function isWSL() { 9 | if (process.platform !== 'linux') { 10 | return false; 11 | } 12 | 13 | if (os.release().toLowerCase().includes('microsoft')) { 14 | return true; 15 | } 16 | 17 | try { 18 | return fs.readFileSync('/proc/version', 'utf8').toLowerCase().includes('microsoft'); 19 | } catch { 20 | return false; 21 | } 22 | } 23 | 24 | export function getPackageJson() { 25 | return require(path.resolve(process.cwd(), 'package.json')); 26 | } 27 | 28 | export function getPluginJson() { 29 | return require(path.resolve(process.cwd(), `${SOURCE_DIR}/plugin.json`)); 30 | } 31 | 32 | export function getCPConfigVersion() { 33 | const cprcJson = path.resolve(__dirname, '../', '.cprc.json'); 34 | return fs.existsSync(cprcJson) ? require(cprcJson).version : { version: 'unknown' }; 35 | } 36 | 37 | export function hasReadme() { 38 | return fs.existsSync(path.resolve(process.cwd(), SOURCE_DIR, 'README.md')); 39 | } 40 | 41 | // Support bundling nested plugins by finding all plugin.json files in src directory 42 | // then checking for a sibling module.[jt]sx? file. 43 | export async function getEntries(): Promise> { 44 | const pluginsJson = await glob('**/src/**/plugin.json', { absolute: true }); 45 | 46 | const plugins = await Promise.all( 47 | pluginsJson.map((pluginJson) => { 48 | const folder = path.dirname(pluginJson); 49 | return glob(`${folder}/module.{ts,tsx,js,jsx}`, { absolute: true }); 50 | }) 51 | ); 52 | 53 | return plugins.reduce((result, modules) => { 54 | return modules.reduce((result, module) => { 55 | const pluginPath = path.dirname(module); 56 | const pluginName = path.relative(process.cwd(), pluginPath).replace(/src\/?/i, ''); 57 | const entryName = pluginName === '' ? 'module' : `${pluginName}/module`; 58 | 59 | result[entryName] = module; 60 | return result; 61 | }, result); 62 | }, {}); 63 | } 64 | -------------------------------------------------------------------------------- /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2.1 2 | 3 | parameters: 4 | ssh-fingerprint: 5 | type: string 6 | default: ${GITHUB_SSH_FINGERPRINT} 7 | 8 | aliases: 9 | # Workflow filters 10 | - &filter-only-master 11 | branches: 12 | only: master 13 | - &filter-only-release 14 | branches: 15 | only: /^v[1-9]*[0-9]+\.[1-9]*[0-9]+\.x$/ 16 | 17 | workflows: 18 | plugin_workflow: 19 | jobs: 20 | - build 21 | 22 | executors: 23 | default_exec: # declares a reusable executor 24 | docker: 25 | - image: srclosson/grafana-plugin-ci-alpine:latest 26 | e2e_exec: 27 | docker: 28 | - image: srclosson/grafana-plugin-ci-e2e:latest 29 | 30 | jobs: 31 | build: 32 | executor: default_exec 33 | steps: 34 | - checkout 35 | - restore_cache: 36 | name: restore node_modules 37 | keys: 38 | - build-cache-{{ .Environment.CACHE_VERSION }}-{{ checksum "yarn.lock" }} 39 | - run: 40 | name: Install dependencies 41 | command: | 42 | mkdir ci 43 | [ -f ~/project/node_modules/.bin/grafana-toolkit ] || yarn install --frozen-lockfile 44 | - save_cache: 45 | name: save node_modules 46 | paths: 47 | - ~/project/node_modules 48 | key: build-cache-{{ .Environment.CACHE_VERSION }}-{{ checksum "yarn.lock" }} 49 | - run: 50 | name: Build and test frontend 51 | command: ./node_modules/.bin/grafana-toolkit plugin:ci-build 52 | - run: 53 | name: Build backend 54 | command: mage -v buildAll 55 | - run: 56 | name: Test backend 57 | command: | 58 | mage -v lint 59 | mage -v coverage 60 | - run: 61 | name: Move results to ci folder 62 | command: ./node_modules/.bin/grafana-toolkit plugin:ci-build --finish 63 | - run: 64 | name: Package distribution 65 | command: | 66 | ./node_modules/.bin/grafana-toolkit plugin:ci-package 67 | - persist_to_workspace: 68 | root: . 69 | paths: 70 | - ci/jobs/package 71 | - ci/packages 72 | - ci/dist 73 | - ci/grafana-test-env 74 | - store_artifacts: 75 | path: ci -------------------------------------------------------------------------------- /playwright.config.ts: -------------------------------------------------------------------------------- 1 | import { dirname } from 'path'; 2 | import { defineConfig, devices } from '@playwright/test'; 3 | import type { PluginOptions } from '@grafana/plugin-e2e'; 4 | 5 | const pluginE2eAuth = `${dirname(require.resolve('@grafana/plugin-e2e'))}/auth`; 6 | /** 7 | * Read environment variables from file. 8 | * https://github.com/motdotla/dotenv 9 | */ 10 | // import dotenv from 'dotenv'; 11 | // dotenv.config({ path: path.resolve(__dirname, '.env') }); 12 | 13 | /** 14 | * See https://playwright.dev/docs/test-configuration. 15 | */ 16 | export default defineConfig({ 17 | testDir: './tests', 18 | /* Run tests in files in parallel */ 19 | fullyParallel: true, 20 | /* Fail the build on CI if you accidentally left test.only in the source code. */ 21 | forbidOnly: !!process.env.CI, 22 | /* Retry on CI only */ 23 | retries: process.env.CI ? 2 : 0, 24 | /* Opt out of parallel tests on CI. */ 25 | workers: process.env.CI ? 1 : undefined, 26 | /* Reporter to use. See https://playwright.dev/docs/test-reporters */ 27 | reporter: 'html', 28 | /* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */ 29 | use: { 30 | /* Base URL to use in actions like `await page.goto('/')`. */ 31 | baseURL: 'http://localhost:3000', 32 | 33 | /* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */ 34 | trace: 'on-first-retry', 35 | }, 36 | 37 | /* Configure projects for major browsers */ 38 | projects: [ 39 | { 40 | name: 'auth', 41 | testDir: pluginE2eAuth, 42 | testMatch: [/.*\.js/], 43 | }, 44 | { 45 | name: 'config', 46 | testDir: './tests/config', 47 | use: { 48 | ...devices['Desktop Chrome'], 49 | // @grafana/plugin-e2e writes the auth state to this file, 50 | // the path should not be modified 51 | storageState: 'playwright/.auth/admin.json', 52 | }, 53 | dependencies: ['auth'], 54 | }, 55 | { 56 | name: 'query', 57 | testDir: './tests/query', 58 | use: { 59 | ...devices['Desktop Chrome'], 60 | // @grafana/plugin-e2e writes the auth state to this file, 61 | // the path should not be modified 62 | storageState: 'playwright/.auth/admin.json', 63 | }, 64 | dependencies: ['config'], 65 | } 66 | ], 67 | }); 68 | -------------------------------------------------------------------------------- /src/JWTConfig.tsx: -------------------------------------------------------------------------------- 1 | import { Alert, Button, InlineFormLabel } from '@grafana/ui'; 2 | import { isObject, startCase } from 'lodash'; 3 | import React, { useState } from 'react'; 4 | import { DropZone } from './DropZone'; 5 | // import { JWTFile } from '../types'; 6 | 7 | const configKeys = [ 8 | 'type', 9 | 'project_id', 10 | 'private_key_id', 11 | 'private_key', 12 | 'client_email', 13 | 'client_id', 14 | 'auth_uri', 15 | 'token_uri', 16 | 'auth_provider_x509_cert_url', 17 | 'client_x509_cert_url', 18 | ]; 19 | 20 | export interface Props { 21 | onChange: (jwt: string) => void; 22 | isConfigured: boolean; 23 | } 24 | 25 | const validateJson = (json: { [key: string]: string }) => isObject(json) && configKeys.every((key) => !!json[key]); 26 | 27 | export function JWTConfig({ onChange, isConfigured }: Props) { 28 | const [enableUpload, setEnableUpload] = useState(!isConfigured); 29 | const [error, setError] = useState(); 30 | 31 | return enableUpload ? ( 32 | <> 33 | { 39 | const reader = new FileReader(); 40 | if (acceptedFiles.length === 1) { 41 | reader.onloadend = (e: any) => { 42 | const json = JSON.parse(e.target.result); 43 | if (validateJson(json)) { 44 | onChange(e.target.result); 45 | setEnableUpload(false); 46 | } else { 47 | setError('Invalid JWT file'); 48 | } 49 | }; 50 | reader.readAsText(acceptedFiles[0]); 51 | } else if (acceptedFiles.length > 1) { 52 | setError('You can only upload one file'); 53 | } 54 | }} 55 | > 56 |

Drop the file here, or click to use the file explorer

57 |
58 | 59 | {error && ( 60 | 61 | )} 62 | 63 | ) : ( 64 | <> 65 | {configKeys.map((key) => ( 66 |
67 | {startCase(key)} 68 | 69 |
70 | ))} 71 | 74 | 75 | ); 76 | } 77 | -------------------------------------------------------------------------------- /.github/workflows/codeql-analysis.yml: -------------------------------------------------------------------------------- 1 | # For most projects, this workflow file will not need changing; you simply need 2 | # to commit it to your repository. 3 | # 4 | # You may wish to alter this file to override the set of languages analyzed, 5 | # or to provide custom queries or build logic. 6 | # 7 | # ******** NOTE ******** 8 | # We have attempted to detect the languages in your repository. Please check 9 | # the `language` matrix defined below to confirm you have the correct set of 10 | # supported CodeQL languages. 11 | # 12 | name: "CodeQL" 13 | 14 | on: 15 | push: 16 | branches: [ master ] 17 | pull_request: 18 | # The branches below must be a subset of the branches above 19 | branches: [ master ] 20 | schedule: 21 | - cron: '20 14 * * 3' 22 | 23 | jobs: 24 | analyze: 25 | name: Analyze 26 | runs-on: ubuntu-latest 27 | 28 | strategy: 29 | fail-fast: false 30 | matrix: 31 | language: [ 'go', 'javascript' ] 32 | # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ] 33 | # Learn more: 34 | # https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed 35 | 36 | steps: 37 | - name: Checkout repository 38 | uses: actions/checkout@v2 39 | 40 | # Initializes the CodeQL tools for scanning. 41 | - name: Initialize CodeQL 42 | uses: github/codeql-action/init@v1 43 | with: 44 | languages: ${{ matrix.language }} 45 | # If you wish to specify custom queries, you can do so here or in a config file. 46 | # By default, queries listed here will override any specified in a config file. 47 | # Prefix the list here with "+" to use these queries and those in the config file. 48 | # queries: ./path/to/local/query, your-org/your-repo/queries@main 49 | 50 | # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). 51 | # If this step fails, then you should remove it and run the build manually (see below) 52 | - name: Autobuild 53 | uses: github/codeql-action/autobuild@v1 54 | 55 | # ℹ️ Command-line programs to run using the OS shell. 56 | # 📚 https://git.io/JvXDl 57 | 58 | # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines 59 | # and modify them (or add more) to build your code if your project 60 | # uses a compiled language 61 | 62 | #- run: | 63 | # make bootstrap 64 | # make release 65 | 66 | - name: Perform CodeQL Analysis 67 | uses: github/codeql-action/analyze@v1 68 | -------------------------------------------------------------------------------- /.config/Dockerfile: -------------------------------------------------------------------------------- 1 | ARG grafana_version=latest 2 | ARG grafana_image=grafana-enterprise 3 | 4 | FROM grafana/${grafana_image}:${grafana_version} 5 | 6 | ARG development=false 7 | ARG TARGETARCH 8 | 9 | ARG GO_VERSION=1.21.6 10 | ARG GO_ARCH=${TARGETARCH:-amd64} 11 | 12 | ENV DEV "${development}" 13 | 14 | # Make it as simple as possible to access the grafana instance for development purposes 15 | # Do NOT enable these settings in a public facing / production grafana instance 16 | ENV GF_AUTH_ANONYMOUS_ORG_ROLE "Admin" 17 | ENV GF_AUTH_ANONYMOUS_ENABLED "true" 18 | ENV GF_AUTH_BASIC_ENABLED "false" 19 | # Set development mode so plugins can be loaded without the need to sign 20 | ENV GF_DEFAULT_APP_MODE "development" 21 | 22 | 23 | LABEL maintainer="Grafana Labs " 24 | 25 | ENV GF_PATHS_HOME="/usr/share/grafana" 26 | WORKDIR $GF_PATHS_HOME 27 | 28 | USER root 29 | 30 | # Installing supervisor and inotify-tools 31 | RUN if [ "${development}" = "true" ]; then \ 32 | if grep -i -q alpine /etc/issue; then \ 33 | apk add supervisor inotify-tools git; \ 34 | elif grep -i -q ubuntu /etc/issue; then \ 35 | DEBIAN_FRONTEND=noninteractive && \ 36 | apt-get update && \ 37 | apt-get install -y supervisor inotify-tools git && \ 38 | rm -rf /var/lib/apt/lists/*; \ 39 | else \ 40 | echo 'ERROR: Unsupported base image' && /bin/false; \ 41 | fi \ 42 | fi 43 | 44 | COPY supervisord/supervisord.conf /etc/supervisor.d/supervisord.ini 45 | COPY supervisord/supervisord.conf /etc/supervisor/conf.d/supervisord.conf 46 | 47 | 48 | # Installing Go 49 | RUN if [ "${development}" = "true" ]; then \ 50 | curl -O -L https://golang.org/dl/go${GO_VERSION}.linux-${GO_ARCH}.tar.gz && \ 51 | rm -rf /usr/local/go && \ 52 | tar -C /usr/local -xzf go${GO_VERSION}.linux-${GO_ARCH}.tar.gz && \ 53 | echo "export PATH=$PATH:/usr/local/go/bin:~/go/bin" >> ~/.bashrc && \ 54 | rm -f go${GO_VERSION}.linux-${GO_ARCH}.tar.gz; \ 55 | fi 56 | 57 | # Installing delve for debugging 58 | RUN if [ "${development}" = "true" ]; then \ 59 | /usr/local/go/bin/go install github.com/go-delve/delve/cmd/dlv@latest; \ 60 | fi 61 | 62 | # Installing mage for plugin (re)building 63 | RUN if [ "${development}" = "true" ]; then \ 64 | git clone https://github.com/magefile/mage; \ 65 | cd mage; \ 66 | export PATH=$PATH:/usr/local/go/bin; \ 67 | go run bootstrap.go; \ 68 | fi 69 | 70 | # Inject livereload script into grafana index.html 71 | RUN sed -i 's|||g' /usr/share/grafana/public/views/index.html 72 | 73 | 74 | COPY entrypoint.sh /entrypoint.sh 75 | RUN chmod +x /entrypoint.sh 76 | ENTRYPOINT ["/entrypoint.sh"] 77 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "blackcowmoo-googleanalytics-datasource", 3 | "version": "0.3.1", 4 | "description": " Google-Analytics to Grafana", 5 | "scripts": { 6 | "build": "webpack -c ./.config/webpack/webpack.config.ts --env production", 7 | "dev": "webpack -w -c ./.config/webpack/webpack.config.ts --env development", 8 | "e2e": "playwright test", 9 | "e2e:ui": "playwright test --ui", 10 | "lint": "eslint --cache --ignore-path ./.gitignore --ext .js,.jsx,.ts,.tsx .", 11 | "lint:fix": "yarn run lint --fix", 12 | "postinstall": "patch-package", 13 | "server": "docker-compose up --build", 14 | "sign": "npx --yes @grafana/sign-plugin@latest", 15 | "test": "jest --watch --onlyChanged", 16 | "test:ci": "jest --passWithNoTests --maxWorkers 4", 17 | "typecheck": "tsc --noEmit" 18 | }, 19 | "author": "blackcowmoo", 20 | "license": "Apache-2.0", 21 | "devDependencies": { 22 | "@babel/core": "^7.21.4", 23 | "@grafana/eslint-config": "^7.0.0", 24 | "@grafana/plugin-e2e": "^1.6.1", 25 | "@grafana/tsconfig": "^1.2.0-rc1", 26 | "@playwright/test": "^1.45.1", 27 | "@swc/core": "^1.3.90", 28 | "@swc/helpers": "^0.5.0", 29 | "@swc/jest": "^0.2.26", 30 | "@testing-library/jest-dom": "6.1.4", 31 | "@testing-library/react": "14.0.0", 32 | "@types/jest": "^29.5.0", 33 | "@types/lodash": "^4.14.194", 34 | "@types/node": "^20.8.7", 35 | "@types/react-dropzone": "^5.1.0", 36 | "@types/react-router-dom": "^5.2.0", 37 | "@types/testing-library__jest-dom": "5.14.8", 38 | "copy-webpack-plugin": "^11.0.0", 39 | "css-loader": "^6.7.3", 40 | "eslint-plugin-deprecation": "^2.0.0", 41 | "eslint-webpack-plugin": "^4.0.1", 42 | "fork-ts-checker-webpack-plugin": "^8.0.0", 43 | "glob": "^10.2.7", 44 | "identity-obj-proxy": "3.0.0", 45 | "imports-loader": "^5.0.0", 46 | "jest": "^29.5.0", 47 | "jest-environment-jsdom": "^29.5.0", 48 | "patch-package": "^8.0.0", 49 | "prettier": "^2.8.7", 50 | "replace-in-file-webpack-plugin": "^1.0.6", 51 | "sass": "1.63.2", 52 | "sass-loader": "13.3.1", 53 | "style-loader": "3.3.3", 54 | "swc-loader": "^0.2.3", 55 | "ts-node": "^10.9.1", 56 | "tsconfig-paths": "^4.2.0", 57 | "typescript": "4.8.4", 58 | "webpack": "^5.94.0", 59 | "webpack-cli": "^5.1.4", 60 | "webpack-livereload-plugin": "^3.0.2" 61 | }, 62 | "engines": { 63 | "node": ">=14" 64 | }, 65 | "dependencies": { 66 | "@emotion/css": "11.10.6", 67 | "@grafana/data": "10.2.2", 68 | "@grafana/runtime": "10.2.2", 69 | "@grafana/schema": "^10.4.0", 70 | "@grafana/ui": "10.2.2", 71 | "react": "18.2.0", 72 | "react-dom": "18.2.0", 73 | "tslib": "2.5.3" 74 | }, 75 | "packageManager": "yarn@1.22.19" 76 | } 77 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## 0.3.1 4 | - Support or,and,not filter[#128](https://github.com/blackcowmoo/grafana-google-analytics-datasource/pull/128) 5 | - Support grafana version < 13[#136](https://github.com/blackcowmoo/grafana-google-analytics-datasource/pull/136) 6 | - Fix github actions [#136](https://github.com/blackcowmoo/grafana-google-analytics-datasource/pull/136) 7 | - Update README.md[#127](https://github.com/blackcowmoo/grafana-google-analytics-datasource/pull/127) 8 | - Remove upper bound of your grafana dependency 9 | 10 | ## 0.3.0 11 | - Fix timezone no zoneinfo.zip([#102](https://github.com/blackcowmoo/grafana-google-analytics-datasource/pull/102)) 12 | - Supprot variable at Dimensions Filter([#103](https://github.com/blackcowmoo/grafana-google-analytics-datasource/pull/103)) 13 | - Support realtime Report([#107](https://github.com/blackcowmoo/grafana-google-analytics-datasource/pull/107)) 14 | - Fix error message typo([#110](https://github.com/blackcowmoo/grafana-google-analytics-datasource/pull/110)) 15 | - Replace deprecated Grafana(v12) SCSS styles([#114](https://github.com/blackcowmoo/grafana-google-analytics-datasource/pull/114)) 16 | 17 | ## 0.2.3 18 | - Support DimensionFilter([#90](https://github.com/blackcowmoo/grafana-google-analytics-datasource/pull/90)) 19 | - Fix metric type bug([#88](https://github.com/blackcowmoo/grafana-google-analytics-datasource/pull/88)) 20 | - Add time series and table query mode([#87](https://github.com/blackcowmoo/grafana-google-analytics-datasource/pull/87)) 21 | 22 | ## 0.2.2 23 | - Hotfix 0.2.1 query editor frontend bug 24 | 25 | ## 0.2.1 26 | - Fix healthcheck fail when no profile or no property([#80](https://github.com/blackcowmoo/grafana-google-analytics-datasource/pull/80)) 27 | - Update README to remove hardcoded GCP Project ID([#79](https://github.com/blackcowmoo/grafana-google-analytics-datasource/pull/79)) 28 | - Support custom metrics and dimensions([#69](https://github.com/blackcowmoo/grafana-google-analytics-datasource/pull/69/files)) 29 | 30 | ## 0.2.0 31 | - support GA4 32 | - change query editor UI 33 | - go version 1.18 -> 1.20 34 | - update dependencies 35 | ## 0.1.5 36 | - support grafana version < 11 37 | - update dependencies 38 | ## 0.1.4 39 | - apply datasource intance management 40 | - upgrade go dependencies 41 | - support grafana 8.x version 42 | ## 0.1.3 43 | - fix query editor ui 44 | - add filter expression 45 | ## 0.1.2 46 | - separate time dimension and other dimensions 47 | - fix bug multi dimensions breaking graphs 48 | 49 | ## 0.1.1 50 | - enhancement query editor ui 51 | - add timezone label at query editor 52 | - get metric bug fix 53 | - add Metrics & Dimensions description 54 | - add Metadata type for typescript 55 | - remove unused function 56 | 57 | 58 | ## 0.1.0 59 | 60 | Initial release. Not fit for production use. 61 | -------------------------------------------------------------------------------- /.github/workflows/playwright.yml: -------------------------------------------------------------------------------- 1 | name: E2E tests 2 | on: 3 | pull_request: 4 | 5 | permissions: 6 | contents: read 7 | id-token: write 8 | 9 | jobs: 10 | resolve-versions: 11 | name: Resolve Grafana images 12 | runs-on: ubuntu-latest 13 | timeout-minutes: 3 14 | outputs: 15 | matrix: ${{ steps.resolve-versions.outputs.matrix }} 16 | steps: 17 | - uses: actions/checkout@v4 18 | - name: Resolve Grafana E2E versions 19 | id: resolve-versions 20 | uses: grafana/plugin-actions/e2e-version@main 21 | with: 22 | version-resolver-type: plugin-grafana-dependency 23 | skip-grafana-dev-image: true 24 | playwright-tests: 25 | needs: resolve-versions 26 | timeout-minutes: 60 27 | strategy: 28 | fail-fast: false 29 | matrix: 30 | GRAFANA_IMAGE: ${{fromJson(needs.resolve-versions.outputs.matrix)}} 31 | name: e2e ${{ matrix.GRAFANA_IMAGE.name }}@${{ matrix.GRAFANA_IMAGE.VERSION }} 32 | runs-on: ubuntu-latest 33 | steps: 34 | - uses: actions/checkout@v4 35 | 36 | - name: Setup Node.js environment 37 | uses: actions/setup-node@v4 38 | with: 39 | cache: 'yarn' 40 | node-version-file: .nvmrc 41 | 42 | - name: Set up Go 43 | uses: actions/setup-go@v4 44 | with: 45 | go-version: 1.22 46 | 47 | - name: Install Mage 48 | uses: magefile/mage-action@v3 49 | with: 50 | install-only: true 51 | 52 | - name: Install yarn dependencies 53 | run: yarn install 54 | 55 | - name: Build binaries 56 | run: mage -v build:linux 57 | 58 | - name: Build frontend 59 | run: yarn build 60 | 61 | - name: Install Playwright Browsers 62 | run: yarn playwright install --with-deps 63 | 64 | - name: Start Grafana 65 | run: | 66 | docker compose pull 67 | GRAFANA_VERSION=${{ matrix.GRAFANA_IMAGE.VERSION }} GRAFANA_IMAGE=${{ matrix.GRAFANA_IMAGE.NAME }} docker compose up -d 68 | 69 | - name: Wait for Grafana to start 70 | uses: nev7n/wait_for_response@v1 71 | with: 72 | url: 'http://localhost:3000/' 73 | responseCode: 200 74 | timeout: 60000 75 | interval: 500 76 | 77 | - name: Restore Google Analytics Json 78 | id: restore-google-json 79 | run: | 80 | mkdir -p ./tests/credentials 81 | echo '${{ secrets.GOOGLE_AUTH_JSON }}' | base64 --decode > ./tests/credentials/grafana-success.json 82 | echo '${{ secrets.GOOGLE_AUTH_FAIL_JSON }}' | base64 --decode > ./tests/credentials/grafana-fail.json 83 | 84 | 85 | - name: Run E2E tests 86 | id: run-tests 87 | run: yarn run e2e 88 | 89 | - uses: actions/upload-artifact@v4 90 | if: always() 91 | with: 92 | name: playwright-report-${{ matrix.GRAFANA_IMAGE.VERSION }} 93 | path: playwright-report/ 94 | retention-days: 30 95 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![CodeQL](https://github.com/blackcowmoo/Grafana-Google-Analytics-DataSource/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/blackcowmoo/Grafana-Google-Analytics-DataSource/actions/workflows/codeql-analysis.yml) ![](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fgrafana.com%2Fapi%2Fplugins%2Fblackcowmoo-googleanalytics-datasource%3Fversion%3Dlatest&query=%24.downloads&label=downloads) ![](https://img.shields.io/github/v/release/blackcowmoo/Grafana-Google-Analytics-DataSource?style=plastic?label=repo) ![](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fgrafana.com%2Fapi%2Fplugins%2Fblackcowmoo-googleanalytics-datasource%3Fversion%3Dlatest&query=%24.version&label=grafana%20release&prefix=v) 2 | # Google Analytics datasource 3 | 4 | Visualize data from Google Analytics UA(Deprecated) And GA4(beta) 5 | 6 | ## Feature 7 | - AutoComplete AccountID & WebpropertyID & ProfileID 8 | - AutoComplete Metrics & Dimensions 9 | - Query using Metrics & Dimensions 10 | - Setting with json 11 | 12 | ![query](https://github.com/blackcowmoo/Grafana-Google-Analytics-DataSource/blob/master/src/img/query.png?raw=true) 13 | 14 | ## Preparations 15 | ### Generate a JWT file 16 | 17 | 1. if you don't have gcp project, add new gcp project. [link](https://cloud.google.com/resource-manager/docs/creating-managing-projects#console) 18 | 2. Open the [Credentials](https://console.developers.google.com/apis/credentials) page in the Google API Console. 19 | 3. Click **Create Credentials** then click **Service account**. 20 | 4. On the Create service account page, enter the Service account details. 21 | 5. On the `Create service account` page, fill in the `Service account details` and then click `Create` 22 | 6. On the `Service account permissions` page, don't add a role to the service account. Just click `Continue` 23 | 7. In the next step, click `Create Key`. Choose key type `JSON` and click `Create`. A JSON key file will be created and downloaded to your computer 24 | 8. Note your `service account email` ex) *@*.iam.gserviceaccount.com 25 | 9. Open the [Google Analytics Admin API](https://console.cloud.google.com/apis/library/analyticsadmin.googleapis.com) in API Library and enable access for your account 26 | 10. Open the [Google Analytics Data API](https://console.cloud.google.com/apis/library/analyticsdata.googleapis.com) in API Library and enable access for your GA Data 27 | 28 | ### Google Analytics Setting 29 | 30 | 1. Open the [Google Analytics](https://analytics.google.com/) 31 | 2. Select Your Analytics Account And Open Admin Page 32 | 3. Click **Account User Management** on the **Account Tab** 33 | 4. Click plus Button then Add users 34 | 5. Enter `service account email` at **Generate a JWT file** 8th step and Permissions add `Read & Analyze` 35 | 36 | ### Grafana 37 | Go To Add Data source then Drag the file to the dotted zone above. Then click `Save & Test`. 38 | The file contents will be encrypted and saved in the Grafana database. 39 | 40 | ## FAQ 41 | [FAQ](https://github.com/blackcowmoo/Grafana-Google-Analytics-DataSource/tree/master/FAQ.md) 42 | 43 | ## How To Dev 44 | [build directory](https://github.com/blackcowmoo/Grafana-Google-Analytics-DataSource/tree/master/build) 45 | -------------------------------------------------------------------------------- /src/img/logo.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /pkg/gav3/model.go: -------------------------------------------------------------------------------- 1 | package gav3 2 | 3 | import ( 4 | "context" 5 | "encoding/json" 6 | "fmt" 7 | "net/http" 8 | "time" 9 | 10 | "github.com/blackcowmoo/grafana-google-analytics-dataSource/pkg/model" 11 | "github.com/blackcowmoo/grafana-google-analytics-dataSource/pkg/setting" 12 | "github.com/grafana/grafana-plugin-sdk-go/backend" 13 | "github.com/grafana/grafana-plugin-sdk-go/backend/log" 14 | ) 15 | 16 | // GetQueryModel returns the well typed query model 17 | func GetQueryModel(query backend.DataQuery) (*model.QueryModel, error) { 18 | model := &model.QueryModel{ 19 | PageSize: GaReportMaxResult, 20 | PageToken: "", 21 | } 22 | err := json.Unmarshal(query.JSON, &model) 23 | if err != nil { 24 | return nil, fmt.Errorf("error reading query: %s", err.Error()) 25 | } 26 | 27 | // Copy directly from the well typed query 28 | timezone, err := time.LoadLocation(model.Timezone) 29 | if err != nil { 30 | return nil, fmt.Errorf("error get timezone %s", err.Error()) 31 | } 32 | 33 | log.DefaultLogger.Debug("query timezone", "timezone", timezone.String()) 34 | 35 | model.StartDate = query.TimeRange.From.In(timezone).Format("2006-01-02") 36 | model.EndDate = query.TimeRange.To.In(timezone).Format("2006-01-02") 37 | model.Dimensions = append([]string{model.TimeDimension}, model.Dimensions...) 38 | // model.TimeRange = query.TimeRange 39 | // model.MaxDataPoints = query.MaxDataPoints 40 | return model, nil 41 | } 42 | 43 | func (ga *GoogleAnalytics) getMetadata() (*model.Metadata, error) { 44 | res, err := http.Get(GaMetadataURL) 45 | if err != nil { 46 | return nil, fmt.Errorf("failed to fetch metadata api %w", err) 47 | } 48 | defer res.Body.Close() 49 | 50 | metadata := model.Metadata{} 51 | 52 | err = json.NewDecoder(res.Body).Decode(&metadata) 53 | if err != nil { 54 | return nil, fmt.Errorf("fail to parsing metadata to json %w", err) 55 | } 56 | return &metadata, nil 57 | } 58 | 59 | func (ga *GoogleAnalytics) getFilteredMetadata() ([]model.MetadataItem, []model.MetadataItem, error) { 60 | metadata, err := ga.getMetadata() 61 | if err != nil { 62 | return nil, nil, err 63 | } 64 | 65 | // length := int(metadata.TotalResults) 66 | var dimensionItems = make([]model.MetadataItem, 0) 67 | var metricItems = make([]model.MetadataItem, 0) 68 | for _, item := range metadata.Items { 69 | if item.Attributes.Status == "DEPRECATED" || item.Attributes.ReplacedBy != "" { 70 | continue 71 | } 72 | if item.Attributes.Type == model.AttributeTypeDimension { 73 | dimensionItems = append(dimensionItems, item) 74 | } else if item.Attributes.Type == model.AttributeTypeMetric { 75 | metricItems = append(metricItems, item) 76 | } 77 | } 78 | 79 | return metricItems, dimensionItems, nil 80 | } 81 | 82 | func (ga *GoogleAnalytics) GetDimensions(ctx context.Context, config *setting.DatasourceSecretSettings, propertyId string) ([]model.MetadataItem, error) { 83 | cacheKey := "ga:metadata:dimensions" 84 | if dimensions, _, found := ga.Cache.GetWithExpiration(cacheKey); found { 85 | return dimensions.([]model.MetadataItem), nil 86 | } 87 | 88 | _, dimensions, err := ga.getFilteredMetadata() 89 | if err != nil { 90 | return nil, err 91 | } 92 | 93 | ga.Cache.Set(cacheKey, dimensions, time.Hour) 94 | 95 | return dimensions, nil 96 | } 97 | 98 | func (ga *GoogleAnalytics) GetMetrics(ctx context.Context, config *setting.DatasourceSecretSettings, propertyId string) ([]model.MetadataItem, error) { 99 | cacheKey := "ga:metadata:metrics" 100 | if metrics, _, found := ga.Cache.GetWithExpiration(cacheKey); found { 101 | return metrics.([]model.MetadataItem), nil 102 | } 103 | metrics, _, err := ga.getFilteredMetadata() 104 | if err != nil { 105 | return nil, err 106 | } 107 | 108 | ga.Cache.Set(cacheKey, metrics, time.Hour) 109 | 110 | return metrics, nil 111 | } 112 | -------------------------------------------------------------------------------- /.github/workflows/ci.yaml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | - main 8 | pull_request: 9 | branches: 10 | - master 11 | - main 12 | 13 | jobs: 14 | build: 15 | runs-on: ubuntu-latest 16 | steps: 17 | - uses: actions/checkout@v3 18 | - name: Setup Node.js environment 19 | uses: actions/setup-node@v3 20 | with: 21 | node-version: '20' 22 | cache: 'yarn' 23 | 24 | - name: Install dependencies 25 | run: yarn install --immutable --prefer-offline 26 | 27 | - name: Check types 28 | run: yarn run typecheck 29 | # - name: Lint 30 | # run: yarn run lint 31 | # - name: Unit tests 32 | # run: yarn run test:ci 33 | - name: Build frontend 34 | run: yarn run build 35 | 36 | - name: Check for backend 37 | id: check-for-backend 38 | run: | 39 | if [ -f "Magefile.go" ] 40 | then 41 | echo "has-backend=true" >> $GITHUB_OUTPUT 42 | fi 43 | 44 | - name: Setup Go environment 45 | if: steps.check-for-backend.outputs.has-backend == 'true' 46 | uses: actions/setup-go@v3 47 | with: 48 | go-version: '1.21' 49 | 50 | - name: Test backend 51 | if: steps.check-for-backend.outputs.has-backend == 'true' 52 | uses: magefile/mage-action@v2 53 | with: 54 | version: latest 55 | args: coverage 56 | 57 | - name: Build backend 58 | if: steps.check-for-backend.outputs.has-backend == 'true' 59 | uses: magefile/mage-action@v2 60 | with: 61 | version: latest 62 | args: buildAll 63 | 64 | - name: Get plugin metadata 65 | id: metadata 66 | run: | 67 | sudo apt-get install jq 68 | 69 | export GRAFANA_PLUGIN_ID=$(cat dist/plugin.json | jq -r .id) 70 | export GRAFANA_PLUGIN_VERSION=$(cat dist/plugin.json | jq -r .info.version) 71 | export GRAFANA_PLUGIN_ARTIFACT=${GRAFANA_PLUGIN_ID}-${GRAFANA_PLUGIN_VERSION}.zip 72 | 73 | echo "plugin-id=${GRAFANA_PLUGIN_ID}" >> $GITHUB_OUTPUT 74 | echo "plugin-version=${GRAFANA_PLUGIN_VERSION}" >> $GITHUB_OUTPUT 75 | echo "archive=${GRAFANA_PLUGIN_ARTIFACT}" >> $GITHUB_OUTPUT 76 | 77 | - name: Package plugin 78 | id: package-plugin 79 | run: | 80 | mv dist ${PLUGIN_ID} 81 | zip ${ARCHIVE} ${PLUGIN_ID} -r 82 | env: 83 | ARCHIVE: ${{ steps.metadata.outputs.archive }} 84 | PLUGIN_ID: ${{ steps.metadata.outputs.plugin-id }} 85 | 86 | - name: Check plugin.json 87 | run: | 88 | docker run --pull=always \ 89 | -v $PWD/${ARCHIVE}:/archive.zip \ 90 | grafana/plugin-validator-cli -analyzer=metadatavalid /archive.zip 91 | env: 92 | ARCHIVE: ${{ steps.metadata.outputs.archive }} 93 | 94 | - name: Restore Google Analytics Json 95 | id: restore-google-json 96 | run: | 97 | mkdir -p ./test/credencials 98 | echo '${{ secrets.GOOGLE_AUTH_JSON }}' | base64 --decode > ./test/credencials/auth.json 99 | 100 | - name: Check for E2E 101 | id: check-for-e2e 102 | run: | 103 | if [ -d "cypress" ] 104 | then 105 | echo "has-e2e=true" >> $GITHUB_OUTPUT 106 | fi 107 | 108 | - name: Start grafana docker 109 | if: steps.check-for-e2e.outputs.has-e2e == 'true' 110 | run: docker-compose up -d 111 | 112 | - name: Run e2e tests 113 | continue-on-error: true 114 | id: run-e2e-tests 115 | if: steps.check-for-e2e.outputs.has-e2e == 'true' 116 | run: yarn run e2e 117 | 118 | - name: Stop grafana docker 119 | if: steps.check-for-e2e.outputs.has-e2e == 'true' 120 | run: docker-compose down 121 | 122 | - name: Archive E2E output 123 | uses: actions/upload-artifact@v4 124 | if: steps.check-for-e2e.outputs.has-e2e == 'true' && steps.run-e2e-tests.outcome != 'success' 125 | with: 126 | name: cypress 127 | path: | 128 | cypress/videos 129 | cypress/report.json 130 | cypress/screenshots 131 | retention-days: 5 132 | -------------------------------------------------------------------------------- /src/types.ts: -------------------------------------------------------------------------------- 1 | import { DataQuery, DataSourceJsonData, SelectableValue } from '@grafana/data'; 2 | 3 | export interface GAQuery extends DataQuery { 4 | displayName: Map 5 | version: string; 6 | accountId: string; 7 | webPropertyId: string; 8 | profileId: string; 9 | startDate: string; 10 | endDate: string; 11 | metrics: string[]; 12 | timeDimension: string; 13 | dimensions: string[]; 14 | selectedMetrics: Array>; 15 | selectedTimeDimensions: SelectableValue; 16 | selectedDimensions: Array>; 17 | cacheDurationSeconds?: number; 18 | timezone: string; 19 | filtersExpression: string; 20 | mode: string; 21 | dimensionFilter: GAFilterExpression; 22 | serviceLevel: string; 23 | // metricFilter: Array; 24 | } 25 | // mapping on google-key.json 26 | export interface JWT { 27 | private_key: any; 28 | token_uri: any; 29 | client_email: any; 30 | project_id: any; 31 | } 32 | 33 | export const defaultQuery: Partial = { 34 | // constant: 6.5, 35 | }; 36 | 37 | /** 38 | * These are options configured for each DataSource instance 39 | */ 40 | export interface GADataSourceOptions extends DataSourceJsonData { 41 | version: string; 42 | } 43 | 44 | /** 45 | * Value that is used in the backend, but never sent over HTTP to the frontend 46 | */ 47 | export interface GASecureJsonData { } 48 | 49 | export interface GAMetadata { 50 | id: string; 51 | kind: string; 52 | attributes: GAMetadataAttribute; 53 | } 54 | 55 | export interface GAMetadataAttribute { 56 | type: string; 57 | dataType: string; 58 | group: string; 59 | status?: string; 60 | uiName: string; 61 | description: string; 62 | allowedInSegments?: string; 63 | addedInAPIVersion?: string; 64 | } 65 | 66 | export interface AccountSummary { 67 | Account: string 68 | DisplayName: string 69 | PropertySummaries: PropertySummary[] 70 | } 71 | 72 | export interface PropertySummary { 73 | Property: string 74 | DisplayName: string 75 | Parent: string 76 | ProfileSummaries: ProfileSummary[] 77 | } 78 | 79 | export interface ProfileSummary { 80 | Profile: string 81 | DisplayName: string 82 | Parent: string 83 | Type: string 84 | } 85 | 86 | // https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/FilterExpression?hl=ko#Filter 87 | 88 | export interface GAFilterExpression { 89 | andGroup?: GAFilterExpressionList 90 | orGroup?: GAFilterExpressionList 91 | notExpression?: GAFilterExpression 92 | filter?: GAFilter 93 | } 94 | 95 | export interface GAFilterExpressionList { 96 | expressions: GAFilterExpression[] 97 | } 98 | 99 | export interface GAFilter { 100 | fieldName: string; 101 | 102 | // filterType: GAMetricFilterType | GADimensionFilterType | undefined; 103 | filterType: GADimensionFilterType; 104 | stringFilter?: GAStringFilter; 105 | inListFilter?: GAInListFilter; 106 | numberFilter?: GANumbericFilter; 107 | betweenFilter?: GABetweenFilter; 108 | } 109 | 110 | export interface GAStringFilter { 111 | matchType: GAStringFilterMatchType; 112 | value: string; 113 | caseSensitive: boolean; 114 | } 115 | 116 | export interface GAInListFilter { 117 | values: string[]; 118 | caseSensitive: boolean; 119 | } 120 | 121 | export interface GANumbericFilter { 122 | operation: GANumbericFilterOperation; 123 | value: GANumbericValue; 124 | caseSensitive: boolean; 125 | } 126 | 127 | export interface GABetweenFilter { 128 | fromValue: GANumbericValue; 129 | toValue: GANumbericValue; 130 | } 131 | 132 | export interface GANumbericValue { 133 | int64Value: string; 134 | doubleValue: number; 135 | } 136 | 137 | export enum GADimensionFilterType { 138 | STRING = "STRING", 139 | IN_LIST = "IN_LIST" 140 | } 141 | 142 | export enum GAMetricFilterType { 143 | NUMBERIC, 144 | BETWEEN 145 | } 146 | 147 | 148 | export enum GAStringFilterMatchType { 149 | MATCH_TYPE_UNSPECIFIED = "MATCH_TYPE_UNSPECIFIED", 150 | EXACT = "EXACT", 151 | BEGINS_WITH = "BEGINS_WITH", 152 | ENDS_WITH = "ENDS_WITH", 153 | CONTAINS = "CONTAINS", 154 | FULL_REGEXP = "FULL_REGEXP", 155 | PARTIAL_REGEXP = "PARTIAL_REGEXP" 156 | } 157 | 158 | export enum GANumbericFilterOperation { 159 | OPERATION_UNSPECIFIED, 160 | EQUAL, 161 | LESS_THAN, 162 | LESS_THAN_OR_EQUAL, 163 | GREATER_THAN, 164 | GREATER_THAN_OR_EQUAL, 165 | } 166 | -------------------------------------------------------------------------------- /provisioning/dashboards/v0.3.0.json: -------------------------------------------------------------------------------- 1 | { 2 | "annotations": { 3 | "list": [ 4 | { 5 | "builtIn": 1, 6 | "datasource": { 7 | "type": "grafana", 8 | "uid": "-- Grafana --" 9 | }, 10 | "enable": true, 11 | "hide": true, 12 | "iconColor": "rgba(0, 211, 255, 1)", 13 | "name": "Annotations & Alerts", 14 | "type": "dashboard" 15 | } 16 | ] 17 | }, 18 | "editable": true, 19 | "fiscalYearStartMonth": 0, 20 | "graphTooltip": 0, 21 | "id": 1, 22 | "links": [], 23 | "panels": [ 24 | { 25 | "datasource": { 26 | "type": "blackcowmoo-googleanalytics-datasource", 27 | "uid": "lcc3108_test" 28 | }, 29 | "fieldConfig": { 30 | "defaults": { 31 | "color": { 32 | "mode": "palette-classic" 33 | }, 34 | "custom": { 35 | "axisBorderShow": false, 36 | "axisCenteredZero": false, 37 | "axisColorMode": "text", 38 | "axisLabel": "", 39 | "axisPlacement": "auto", 40 | "barAlignment": 0, 41 | "drawStyle": "line", 42 | "fillOpacity": 0, 43 | "gradientMode": "none", 44 | "hideFrom": { 45 | "legend": false, 46 | "tooltip": false, 47 | "viz": false 48 | }, 49 | "insertNulls": false, 50 | "lineInterpolation": "linear", 51 | "lineWidth": 1, 52 | "pointSize": 5, 53 | "scaleDistribution": { 54 | "type": "linear" 55 | }, 56 | "showPoints": "auto", 57 | "spanNulls": false, 58 | "stacking": { 59 | "group": "A", 60 | "mode": "none" 61 | }, 62 | "thresholdsStyle": { 63 | "mode": "off" 64 | } 65 | }, 66 | "mappings": [], 67 | "thresholds": { 68 | "mode": "absolute", 69 | "steps": [ 70 | { 71 | "color": "green", 72 | "value": null 73 | }, 74 | { 75 | "color": "red", 76 | "value": 80 77 | } 78 | ] 79 | } 80 | }, 81 | "overrides": [] 82 | }, 83 | "gridPos": { 84 | "h": 8, 85 | "w": 12, 86 | "x": 0, 87 | "y": 0 88 | }, 89 | "id": 1, 90 | "options": { 91 | "legend": { 92 | "calcs": [], 93 | "displayMode": "list", 94 | "placement": "bottom", 95 | "showLegend": true 96 | }, 97 | "tooltip": { 98 | "maxHeight": 600, 99 | "mode": "single", 100 | "sort": "none" 101 | } 102 | }, 103 | "targets": [ 104 | { 105 | "accountId": "accounts/145710468", 106 | "cacheDurationSeconds": 300, 107 | "datasource": { 108 | "type": "blackcowmoo-googleanalytics-datasource", 109 | "uid": "eemfkybhl4ydca" 110 | }, 111 | "dimensionFilter": {}, 112 | "displayName": {}, 113 | "metrics": [ 114 | "activeUsers" 115 | ], 116 | "mode": "time series", 117 | "refId": "A", 118 | "selectedMetrics": [ 119 | { 120 | "description": "The number of distinct users who visited your site or app.", 121 | "label": "Active users", 122 | "value": "activeUsers" 123 | } 124 | ], 125 | "selectedTimeDimensions": { 126 | "description": "The date of the event, formatted as YYYYMMDD.", 127 | "label": "Date", 128 | "value": "date" 129 | }, 130 | "serviceLevel": "GOOGLE_ANALYTICS_STANDARD", 131 | "timeDimension": "date", 132 | "timezone": "Asia/Seoul", 133 | "version": "v4", 134 | "webPropertyId": "properties/323466308" 135 | } 136 | ], 137 | "title": "Panel Title", 138 | "type": "timeseries" 139 | } 140 | ], 141 | "schemaVersion": 39, 142 | "tags": [], 143 | "templating": { 144 | "list": [] 145 | }, 146 | "time": { 147 | "from": "now-30d", 148 | "to": "now" 149 | }, 150 | "timeRangeUpdatedDuringEditOrView": false, 151 | "timepicker": {}, 152 | "timezone": "browser", 153 | "title": "v0.3.0", 154 | "uid": "eemfl56a7hdz4a", 155 | "version": 1, 156 | "weekStart": "" 157 | } -------------------------------------------------------------------------------- /provisioning/dashboards/v0.2.2.json: -------------------------------------------------------------------------------- 1 | { 2 | "annotations": { 3 | "list": [ 4 | { 5 | "builtIn": 1, 6 | "datasource": { 7 | "type": "grafana", 8 | "uid": "-- Grafana --" 9 | }, 10 | "enable": true, 11 | "hide": true, 12 | "iconColor": "rgba(0, 211, 255, 1)", 13 | "name": "Annotations & Alerts", 14 | "type": "dashboard" 15 | } 16 | ] 17 | }, 18 | "editable": true, 19 | "fiscalYearStartMonth": 0, 20 | "graphTooltip": 0, 21 | "id": 6, 22 | "links": [], 23 | "panels": [ 24 | { 25 | "datasource": { 26 | "default": false, 27 | "type": "blackcowmoo-googleanalytics-datasource", 28 | "uid": "lcc3108_test" 29 | }, 30 | "fieldConfig": { 31 | "defaults": { 32 | "color": { 33 | "mode": "palette-classic" 34 | }, 35 | "custom": { 36 | "axisBorderShow": false, 37 | "axisCenteredZero": false, 38 | "axisColorMode": "text", 39 | "axisLabel": "", 40 | "axisPlacement": "auto", 41 | "barAlignment": 0, 42 | "barWidthFactor": 0.6, 43 | "drawStyle": "line", 44 | "fillOpacity": 0, 45 | "gradientMode": "none", 46 | "hideFrom": { 47 | "legend": false, 48 | "tooltip": false, 49 | "viz": false 50 | }, 51 | "insertNulls": false, 52 | "lineInterpolation": "linear", 53 | "lineWidth": 1, 54 | "pointSize": 5, 55 | "scaleDistribution": { 56 | "type": "linear" 57 | }, 58 | "showPoints": "auto", 59 | "spanNulls": false, 60 | "stacking": { 61 | "group": "A", 62 | "mode": "none" 63 | }, 64 | "thresholdsStyle": { 65 | "mode": "off" 66 | } 67 | }, 68 | "mappings": [], 69 | "thresholds": { 70 | "mode": "absolute", 71 | "steps": [ 72 | { 73 | "color": "green", 74 | "value": null 75 | }, 76 | { 77 | "color": "red", 78 | "value": 80 79 | } 80 | ] 81 | } 82 | }, 83 | "overrides": [] 84 | }, 85 | "gridPos": { 86 | "h": 8, 87 | "w": 12, 88 | "x": 0, 89 | "y": 0 90 | }, 91 | "id": 1, 92 | "options": { 93 | "legend": { 94 | "calcs": [], 95 | "displayMode": "list", 96 | "placement": "bottom", 97 | "showLegend": true 98 | }, 99 | "tooltip": { 100 | "mode": "single", 101 | "sort": "none" 102 | } 103 | }, 104 | "targets": [ 105 | { 106 | "accountId": "accounts/145710468", 107 | "cacheDurationSeconds": 300, 108 | "datasource": { 109 | "type": "blackcowmoo-googleanalytics-datasource", 110 | "uid": "cdw6b7xsotw5cc" 111 | }, 112 | "displayName": {}, 113 | "filtersExpression": "", 114 | "metrics": [ 115 | "active28DayUsers" 116 | ], 117 | "refId": "A", 118 | "selectedMetrics": [ 119 | { 120 | "description": "The number of distinct active users on your site or app within a 28 day period. The 28 day period includes the last day in the report's date range.", 121 | "label": "28-day active users", 122 | "value": "active28DayUsers" 123 | } 124 | ], 125 | "selectedTimeDimensions": { 126 | "description": "The date of the event, formatted as YYYYMMDD.", 127 | "label": "Date", 128 | "value": "date" 129 | }, 130 | "timeDimension": "date", 131 | "timezone": "Asia/Seoul", 132 | "version": "v4", 133 | "webPropertyId": "properties/323466308" 134 | } 135 | ], 136 | "title": "Panel Title", 137 | "type": "timeseries" 138 | } 139 | ], 140 | "schemaVersion": 39, 141 | "tags": [], 142 | "templating": { 143 | "list": [] 144 | }, 145 | "time": { 146 | "from": "now-30d", 147 | "to": "now" 148 | }, 149 | "timepicker": {}, 150 | "timezone": "browser", 151 | "title": "v0.2.2_test", 152 | "uid": "edw6g1hc4zg1sa", 153 | "version": 1, 154 | "weekStart": "" 155 | } 156 | -------------------------------------------------------------------------------- /provisioning/dashboards/v0.2.3.json: -------------------------------------------------------------------------------- 1 | { 2 | "annotations": { 3 | "list": [ 4 | { 5 | "builtIn": 1, 6 | "datasource": { 7 | "type": "grafana", 8 | "uid": "-- Grafana --" 9 | }, 10 | "enable": true, 11 | "hide": true, 12 | "iconColor": "rgba(0, 211, 255, 1)", 13 | "name": "Annotations & Alerts", 14 | "type": "dashboard" 15 | } 16 | ] 17 | }, 18 | "editable": true, 19 | "fiscalYearStartMonth": 0, 20 | "graphTooltip": 0, 21 | "id": 38, 22 | "links": [], 23 | "panels": [ 24 | { 25 | "datasource": { 26 | "type": "blackcowmoo-googleanalytics-datasource", 27 | "uid": "lcc3108_test" 28 | }, 29 | "fieldConfig": { 30 | "defaults": { 31 | "color": { 32 | "mode": "palette-classic" 33 | }, 34 | "custom": { 35 | "axisBorderShow": false, 36 | "axisCenteredZero": false, 37 | "axisColorMode": "text", 38 | "axisLabel": "", 39 | "axisPlacement": "auto", 40 | "barAlignment": 0, 41 | "barWidthFactor": 0.6, 42 | "drawStyle": "line", 43 | "fillOpacity": 0, 44 | "gradientMode": "none", 45 | "hideFrom": { 46 | "legend": false, 47 | "tooltip": false, 48 | "viz": false 49 | }, 50 | "insertNulls": false, 51 | "lineInterpolation": "linear", 52 | "lineWidth": 1, 53 | "pointSize": 5, 54 | "scaleDistribution": { 55 | "type": "linear" 56 | }, 57 | "showPoints": "auto", 58 | "spanNulls": false, 59 | "stacking": { 60 | "group": "A", 61 | "mode": "none" 62 | }, 63 | "thresholdsStyle": { 64 | "mode": "off" 65 | } 66 | }, 67 | "mappings": [], 68 | "thresholds": { 69 | "mode": "absolute", 70 | "steps": [ 71 | { 72 | "color": "green", 73 | "value": null 74 | }, 75 | { 76 | "color": "red", 77 | "value": 80 78 | } 79 | ] 80 | } 81 | }, 82 | "overrides": [] 83 | }, 84 | "gridPos": { 85 | "h": 8, 86 | "w": 12, 87 | "x": 0, 88 | "y": 0 89 | }, 90 | "id": 1, 91 | "options": { 92 | "legend": { 93 | "calcs": [], 94 | "displayMode": "list", 95 | "placement": "bottom", 96 | "showLegend": true 97 | }, 98 | "tooltip": { 99 | "mode": "single", 100 | "sort": "none" 101 | } 102 | }, 103 | "pluginVersion": "11.3.0-74991", 104 | "targets": [ 105 | { 106 | "accountId": "accounts/145710468", 107 | "cacheDurationSeconds": 300, 108 | "datasource": { 109 | "type": "blackcowmoo-googleanalytics-datasource", 110 | "uid": "e7bdc464-a204-4e54-b865-9d712e112c66" 111 | }, 112 | "dimensionFilter": {}, 113 | "displayName": {}, 114 | "metrics": [ 115 | "activeUsers" 116 | ], 117 | "mode": "time series", 118 | "refId": "A", 119 | "selectedMetrics": [ 120 | { 121 | "description": "The number of distinct users who visited your site or app.", 122 | "label": "Active users", 123 | "value": "activeUsers" 124 | } 125 | ], 126 | "selectedTimeDimensions": { 127 | "description": "The date of the event, formatted as YYYYMMDD.", 128 | "label": "Date", 129 | "value": "date" 130 | }, 131 | "timeDimension": "date", 132 | "timezone": "Asia/Seoul", 133 | "version": "v4", 134 | "webPropertyId": "properties/323466308" 135 | } 136 | ], 137 | "title": "Panel Title", 138 | "type": "timeseries" 139 | } 140 | ], 141 | "preload": false, 142 | "refresh": "", 143 | "schemaVersion": 39, 144 | "tags": [], 145 | "templating": { 146 | "list": [] 147 | }, 148 | "time": { 149 | "from": "now-30d", 150 | "to": "now" 151 | }, 152 | "timepicker": {}, 153 | "timezone": "", 154 | "title": "0.2.3", 155 | "uid": "b8503369-1580-4f6d-972f-0718f0f14fa6", 156 | "version": 5, 157 | "weekStart": "" 158 | } 159 | -------------------------------------------------------------------------------- /provisioning/dashboards/default.json: -------------------------------------------------------------------------------- 1 | { 2 | "annotations": { 3 | "list": [ 4 | { 5 | "builtIn": 1, 6 | "datasource": { 7 | "type": "grafana", 8 | "uid": "-- Grafana --" 9 | }, 10 | "enable": true, 11 | "hide": true, 12 | "iconColor": "rgba(0, 211, 255, 1)", 13 | "name": "Annotations & Alerts", 14 | "type": "dashboard" 15 | } 16 | ] 17 | }, 18 | "editable": true, 19 | "fiscalYearStartMonth": 0, 20 | "graphTooltip": 0, 21 | "id": 1, 22 | "links": [], 23 | "panels": [ 24 | { 25 | "datasource": { 26 | "type": "blackcowmoo-googleanalytics-datasource", 27 | "uid": "lcc3108_test" 28 | }, 29 | "fieldConfig": { 30 | "defaults": { 31 | "color": { 32 | "mode": "palette-classic" 33 | }, 34 | "custom": { 35 | "axisBorderShow": false, 36 | "axisCenteredZero": false, 37 | "axisColorMode": "text", 38 | "axisLabel": "", 39 | "axisPlacement": "auto", 40 | "barAlignment": 0, 41 | "drawStyle": "line", 42 | "fillOpacity": 0, 43 | "gradientMode": "none", 44 | "hideFrom": { 45 | "legend": false, 46 | "tooltip": false, 47 | "viz": false 48 | }, 49 | "insertNulls": false, 50 | "lineInterpolation": "linear", 51 | "lineWidth": 1, 52 | "pointSize": 5, 53 | "scaleDistribution": { 54 | "type": "linear" 55 | }, 56 | "showPoints": "auto", 57 | "spanNulls": false, 58 | "stacking": { 59 | "group": "A", 60 | "mode": "none" 61 | }, 62 | "thresholdsStyle": { 63 | "mode": "off" 64 | } 65 | }, 66 | "mappings": [], 67 | "thresholds": { 68 | "mode": "absolute", 69 | "steps": [ 70 | { 71 | "color": "green", 72 | "value": null 73 | }, 74 | { 75 | "color": "red", 76 | "value": 80 77 | } 78 | ] 79 | } 80 | }, 81 | "overrides": [] 82 | }, 83 | "gridPos": { 84 | "h": 8, 85 | "w": 12, 86 | "x": 0, 87 | "y": 0 88 | }, 89 | "id": 1, 90 | "options": { 91 | "legend": { 92 | "calcs": [], 93 | "displayMode": "list", 94 | "placement": "bottom", 95 | "showLegend": true 96 | }, 97 | "tooltip": { 98 | "mode": "single", 99 | "sort": "none" 100 | } 101 | }, 102 | "targets": [ 103 | { 104 | "accountId": "accounts/145710468", 105 | "cacheDurationSeconds": 300, 106 | "datasource": { 107 | "type": "blackcowmoo-googleanalytics-datasource", 108 | "uid": "${DS__GOOGLE ANALYTICS}" 109 | }, 110 | "dimensionFilter": {}, 111 | "dimensions": [ 112 | "country" 113 | ], 114 | "displayName": {}, 115 | "metrics": [ 116 | "activeUsers" 117 | ], 118 | "mode": "time series", 119 | "refId": "A", 120 | "selectedDimensions": [ 121 | { 122 | "description": "The country from which the user activity originated.", 123 | "label": "Country", 124 | "value": "country" 125 | } 126 | ], 127 | "selectedMetrics": [ 128 | { 129 | "description": "The number of distinct users who visited your site or app.", 130 | "label": "Active users", 131 | "value": "activeUsers" 132 | } 133 | ], 134 | "selectedTimeDimensions": { 135 | "description": "The combined values of date and hour formatted as YYYYMMDDHH.", 136 | "label": "Date + hour (YYYYMMDDHH)", 137 | "value": "dateHour" 138 | }, 139 | "serviceLevel": "GOOGLE_ANALYTICS_STANDARD", 140 | "timeDimension": "dateHour", 141 | "timezone": "Asia/Seoul", 142 | "version": "v4", 143 | "webPropertyId": "properties/323466308" 144 | } 145 | ], 146 | "title": "E2E", 147 | "type": "timeseries" 148 | } 149 | ], 150 | "schemaVersion": 39, 151 | "tags": [], 152 | "templating": { 153 | "list": [] 154 | }, 155 | "time": { 156 | "from": "now-7d", 157 | "to": "now" 158 | }, 159 | "timepicker": {}, 160 | "timezone": "browser", 161 | "title": "New dashboard", 162 | "uid": "cdrhlrxwp375sc", 163 | "version": 1, 164 | "weekStart": "" 165 | } 166 | -------------------------------------------------------------------------------- /pkg/model/models.go: -------------------------------------------------------------------------------- 1 | package model 2 | 3 | import ( 4 | "time" 5 | 6 | analyticsdata "google.golang.org/api/analyticsdata/v1beta" 7 | ) 8 | 9 | // GA service level 10 | type ServiceLevel string 11 | 12 | const ( 13 | ServiceLevelStandard = "GOOGLE_ANALYTICS_STANDARD" 14 | ServiceLevelPremium = "GOOGLE_ANALYTICS_360" 15 | ServiceLevelUnspecified = "SERVICE_LEVEL_UNSPECIFIED" 16 | ) 17 | 18 | // ColumnType is the set of possible column types 19 | type ColumnType string 20 | 21 | const ( 22 | // ColumTypeTime is the TIME type 23 | ColumTypeTime ColumnType = "TIME" 24 | // ColumTypeNumber is the NUMBER type 25 | ColumTypeNumber ColumnType = "NUMBER" 26 | // ColumTypeString is the STRING type 27 | ColumTypeString ColumnType = "STRING" 28 | ) 29 | 30 | // ColumnDefinition represents a spreadsheet column definition. 31 | type ColumnDefinition struct { 32 | Header string 33 | ColumnIndex int 34 | columnType ColumnType 35 | } 36 | 37 | // GetType gets the type of a ColumnDefinition. 38 | func (cd *ColumnDefinition) GetType() ColumnType { 39 | return cd.columnType 40 | } 41 | 42 | func getColumnType(headerType string) ColumnType { 43 | switch headerType { 44 | case /*gav4*/ "TYPE_INTEGER", "TYPE_FLOAT", "TYPE_CURRENCY", "TYPE_MILLISECONDS", "TYPE_SECONDS" /*gav3*/, "CURRENCY", "INTEGER", "FLOAT", "PERCENT": 45 | return ColumTypeNumber 46 | case "TIME": 47 | return ColumTypeTime 48 | default: 49 | return ColumTypeString 50 | } 51 | } 52 | 53 | // NewColumnDefinition creates a new ColumnDefinition. 54 | func NewColumnDefinition(header string, index int, headerType string) *ColumnDefinition { 55 | 56 | return &ColumnDefinition{ 57 | Header: header, 58 | ColumnIndex: index, 59 | columnType: getColumnType(headerType), 60 | } 61 | } 62 | 63 | // Metadata 64 | const ( 65 | AttributeTypeDimension AttributeType = "DIMENSION" 66 | AttributeTypeMetric AttributeType = "METRIC" 67 | ) 68 | 69 | type Metadata struct { 70 | Kind string `json:"kind"` 71 | Etag string `json:"etag"` 72 | TotalResults int64 `json:"totalResults"` 73 | AttributeNames []string `json:"attributeNames"` 74 | Items []MetadataItem `json:"items"` 75 | } 76 | 77 | type MetadataItem struct { 78 | ID string `json:"id"` 79 | Kind string `json:"kind"` 80 | Attributes MetadataItemAttribute `json:"attributes"` 81 | } 82 | 83 | type MetadataItemAttribute struct { 84 | Type AttributeType `json:"type,omitempty"` 85 | DataType string `json:"dataType,omitempty"` 86 | Group string `json:"group,omitempty"` 87 | Status string `json:"status,omitempty"` 88 | UIName string `json:"uiName,omitempty"` 89 | Description string `json:"description,omitempty"` 90 | AllowedInSegments string `json:"allowedInSegments,omitempty"` 91 | AddedInAPIVersion string `json:"addedInApiVersion,omitempty"` 92 | ReplacedBy string `json:"replacedBy,omitempty"` 93 | } 94 | 95 | type AttributeType string 96 | 97 | type AccountSummary struct { 98 | Account, DisplayName string 99 | PropertySummaries []*PropertySummary 100 | } 101 | 102 | type PropertySummary struct { 103 | Property, DisplayName, Parent string 104 | ProfileSummaries []*ProfileSummary 105 | } 106 | 107 | type ProfileSummary struct { 108 | Profile, DisplayName, Parent, Type string 109 | } 110 | 111 | type QueryMode string 112 | 113 | const ( 114 | TIME_SERIES QueryMode = "time series" 115 | TABLE QueryMode = "table" 116 | REALTIME QueryMode = "realtime" 117 | ) 118 | 119 | type QueryModel struct { 120 | AccountID string `json:"accountId"` 121 | WebPropertyID string `json:"webPropertyId"` 122 | ProfileID string `json:"profileId"` 123 | StartDate string `json:"startDate"` 124 | EndDate string `json:"endDate"` 125 | RefID string `json:"refId"` 126 | Metrics []string `json:"metrics"` 127 | TimeDimension string `json:"timeDimension"` 128 | Dimensions []string `json:"dimensions"` 129 | PageSize int64 `json:"pageSize,omitempty"` 130 | PageToken string `json:"pageToken,omitempty"` 131 | UseNextPage bool `json:"useNextpage,omitempty"` 132 | Timezone string `json:"timezone,omitempty"` 133 | FiltersExpression string `json:"filtersExpression,omitempty"` 134 | Offset int64 `json:"offset,omitempty"` 135 | Mode QueryMode `json:"mode,omitempty"` 136 | ServiceLevel ServiceLevel `json:"serviceLevel,omitempty"` 137 | // TODO type convert 138 | DimensionFilter analyticsdata.FilterExpression `json:"dimensionFilter,omitempty"` 139 | 140 | From time.Time 141 | To time.Time 142 | // Not from JSON 143 | // TimeRange backend.TimeRange `json:"-"` 144 | // MaxDataPoints int64 `json:"-"` 145 | } 146 | -------------------------------------------------------------------------------- /pkg/gav4/const.go: -------------------------------------------------------------------------------- 1 | package gav4 2 | 3 | import ( 4 | "time" 5 | 6 | "github.com/blackcowmoo/grafana-google-analytics-dataSource/pkg/model" 7 | ) 8 | 9 | const ( 10 | GaDefaultIdx = 1 11 | GaAdminMaxResult = 200 12 | GaReportMaxResult = 100000 13 | GaRealTimeMinMinute = 0 * time.Minute 14 | GaRealTimeMaxMinute = 29 * time.Minute 15 | Ga360RealTimeMaxMinute = 59 * time.Minute 16 | ) 17 | 18 | // Realtime metrics and dimensions not provided by Google Analytics 19 | // 리얼타임 메트릭 디멘션은 구글 api에서 제공하지 않기때문에 상수화로 처리 20 | var GaRealTimeDimensions = []model.MetadataItem{ 21 | { 22 | ID: "appVersion", 23 | Attributes: model.MetadataItemAttribute{ 24 | UIName: "App version", 25 | Description: "The app's versionName (Android) or short bundle version (iOS).", 26 | }, 27 | }, 28 | { 29 | ID: "audienceId", 30 | Attributes: model.MetadataItemAttribute{ 31 | UIName: "Audience ID", 32 | Description: "The numeric identifier of an Audience. Users are reported in the audiences to which they belonged during the report's date range. Current user behavior does not affect historical audience membership in reports.", 33 | }, 34 | }, 35 | { 36 | ID: "audienceName", 37 | Attributes: model.MetadataItemAttribute{ 38 | UIName: "Audience name", 39 | Description: "The given name of an Audience. Users are reported in the audiences to which they belonged during the report's date range. Current user behavior does not affect historical audience membership in reports.", 40 | }, 41 | }, 42 | { 43 | ID: "city", 44 | Attributes: model.MetadataItemAttribute{ 45 | UIName: "City", 46 | Description: "The city from which the user activity originated.", 47 | }, 48 | }, 49 | { 50 | ID: "cityId", 51 | Attributes: model.MetadataItemAttribute{ 52 | UIName: "City ID", 53 | Description: "The geographic ID of the city from which the user activity originated, derived from their IP address.", 54 | }, 55 | }, 56 | { 57 | ID: "country", 58 | Attributes: model.MetadataItemAttribute{ 59 | UIName: "Country", 60 | Description: "The country from which the user activity originated.", 61 | }, 62 | }, 63 | { 64 | ID: "countryId", 65 | Attributes: model.MetadataItemAttribute{ 66 | UIName: "Country ID", 67 | Description: "The geographic ID of the country from which the user activity originated, derived from their IP address. Formatted according to ISO 3166-1 alpha-2 standard.", 68 | }, 69 | }, 70 | { 71 | ID: "deviceCategory", 72 | Attributes: model.MetadataItemAttribute{ 73 | UIName: "Device category", 74 | Description: "The type of device: Desktop, Tablet, or Mobile.", 75 | }, 76 | }, 77 | { 78 | ID: "eventName", 79 | Attributes: model.MetadataItemAttribute{ 80 | UIName: "Event name", 81 | Description: "The name of the event.", 82 | }, 83 | }, 84 | { 85 | ID: "minutesAgo", 86 | Attributes: model.MetadataItemAttribute{ 87 | UIName: "Realtime minutes ago", 88 | Description: "The number of minutes ago that an event was collected. 00 is the current minute, and 01 means the previous minute.", 89 | }, 90 | }, 91 | { 92 | ID: "platform", 93 | Attributes: model.MetadataItemAttribute{ 94 | UIName: "Platform", 95 | Description: "The platform on which your app or website ran; for example, web, iOS, or Android. To determine a stream's type in a report, use both platform and streamId.", 96 | }, 97 | }, 98 | { 99 | ID: "streamId", 100 | Attributes: model.MetadataItemAttribute{ 101 | UIName: "Stream ID", 102 | Description: "The numeric data stream identifier for your app or website.", 103 | }, 104 | }, 105 | { 106 | ID: "streamName", 107 | Attributes: model.MetadataItemAttribute{ 108 | UIName: "Stream name", 109 | Description: "The data stream name for your app or website.", 110 | }, 111 | }, 112 | { 113 | ID: "unifiedScreenName", 114 | Attributes: model.MetadataItemAttribute{ 115 | UIName: "Page title and screen name", 116 | Description: "The page title (web) or screen name (app) on which the event was logged.", 117 | }, 118 | }, 119 | } 120 | 121 | var GaRealTimeMetrics = []model.MetadataItem{ 122 | { 123 | ID: "activeUsers", 124 | Attributes: model.MetadataItemAttribute{ 125 | UIName: "Active users", 126 | Description: "The number of distinct users who visited your site or app.", 127 | }, 128 | }, 129 | { 130 | ID: "conversions", 131 | Attributes: model.MetadataItemAttribute{ 132 | UIName: "Conversions", 133 | Description: "The count of conversion events. Events are marked as conversions at collection time; changes to an event's conversion marking apply going forward. You can mark any event as a conversion in Google Analytics, and some events (i.e. first_open, purchase) are marked as conversions by default. To learn more, see About conversions.", 134 | }, 135 | }, 136 | { 137 | ID: "eventCount", 138 | Attributes: model.MetadataItemAttribute{ 139 | UIName: "Event count", 140 | Description: "The count of events.", 141 | }, 142 | }, 143 | { 144 | ID: "screenPageViews", 145 | Attributes: model.MetadataItemAttribute{ 146 | UIName: "Views", 147 | Description: "The number of app screens or web pages your users viewed. Repeated views of a single page or screen are counted. (screen_view + page_view events).", 148 | }, 149 | }, 150 | } 151 | -------------------------------------------------------------------------------- /.config/README.md: -------------------------------------------------------------------------------- 1 | # Default build configuration by Grafana 2 | 3 | **This is an auto-generated directory and is not intended to be changed! ⚠️** 4 | 5 | The `.config/` directory holds basic configuration for the different tools 6 | that are used to develop, test and build the project. In order to make it updates easier we ask you to 7 | not edit files in this folder to extend configuration. 8 | 9 | ## How to extend the basic configs? 10 | 11 | Bear in mind that you are doing it at your own risk, and that extending any of the basic configuration can lead 12 | to issues around working with the project. 13 | 14 | ### Extending the ESLint config 15 | 16 | Edit the `.eslintrc` file in the project root in order to extend the ESLint configuration. 17 | 18 | **Example:** 19 | 20 | ```json 21 | { 22 | "extends": "./.config/.eslintrc", 23 | "rules": { 24 | "react/prop-types": "off" 25 | } 26 | } 27 | ``` 28 | 29 | --- 30 | 31 | ### Extending the Prettier config 32 | 33 | Edit the `.prettierrc.js` file in the project root in order to extend the Prettier configuration. 34 | 35 | **Example:** 36 | 37 | ```javascript 38 | module.exports = { 39 | // Prettier configuration provided by Grafana scaffolding 40 | ...require('./.config/.prettierrc.js'), 41 | 42 | semi: false, 43 | }; 44 | ``` 45 | 46 | --- 47 | 48 | ### Extending the Jest config 49 | 50 | There are two configuration in the project root that belong to Jest: `jest-setup.js` and `jest.config.js`. 51 | 52 | **`jest-setup.js`:** A file that is run before each test file in the suite is executed. We are using it to 53 | set up the Jest DOM for the testing library and to apply some polyfills. ([link to Jest docs](https://jestjs.io/docs/configuration#setupfilesafterenv-array)) 54 | 55 | **`jest.config.js`:** The main Jest configuration file that extends the Grafana recommended setup. ([link to Jest docs](https://jestjs.io/docs/configuration)) 56 | 57 | #### ESM errors with Jest 58 | 59 | A common issue with the current jest config involves importing an npm package that only offers an ESM build. These packages cause jest to error with `SyntaxError: Cannot use import statement outside a module`. To work around this, we provide a list of known packages to pass to the `[transformIgnorePatterns](https://jestjs.io/docs/configuration#transformignorepatterns-arraystring)` jest configuration property. If need be, this can be extended in the following way: 60 | 61 | ```javascript 62 | process.env.TZ = 'UTC'; 63 | const { grafanaESModules, nodeModulesToTransform } = require('./config/jest/utils'); 64 | 65 | module.exports = { 66 | // Jest configuration provided by Grafana 67 | ...require('./.config/jest.config'), 68 | // Inform jest to only transform specific node_module packages. 69 | transformIgnorePatterns: [nodeModulesToTransform([...grafanaESModules, 'packageName'])], 70 | }; 71 | ``` 72 | 73 | --- 74 | 75 | ### Extending the TypeScript config 76 | 77 | Edit the `tsconfig.json` file in the project root in order to extend the TypeScript configuration. 78 | 79 | **Example:** 80 | 81 | ```json 82 | { 83 | "extends": "./.config/tsconfig.json", 84 | "compilerOptions": { 85 | "preserveConstEnums": true 86 | } 87 | } 88 | ``` 89 | 90 | --- 91 | 92 | ### Extending the Webpack config 93 | 94 | Follow these steps to extend the basic Webpack configuration that lives under `.config/`: 95 | 96 | #### 1. Create a new Webpack configuration file 97 | 98 | Create a new config file that is going to extend the basic one provided by Grafana. 99 | It can live in the project root, e.g. `webpack.config.ts`. 100 | 101 | #### 2. Merge the basic config provided by Grafana and your custom setup 102 | 103 | We are going to use [`webpack-merge`](https://github.com/survivejs/webpack-merge) for this. 104 | 105 | ```typescript 106 | // webpack.config.ts 107 | import type { Configuration } from 'webpack'; 108 | import { merge } from 'webpack-merge'; 109 | import grafanaConfig from './.config/webpack/webpack.config'; 110 | 111 | const config = async (env): Promise => { 112 | const baseConfig = await grafanaConfig(env); 113 | 114 | return merge(baseConfig, { 115 | // Add custom config here... 116 | output: { 117 | asyncChunks: true, 118 | }, 119 | }); 120 | }; 121 | 122 | export default config; 123 | ``` 124 | 125 | #### 3. Update the `package.json` to use the new Webpack config 126 | 127 | We need to update the `scripts` in the `package.json` to use the extended Webpack configuration. 128 | 129 | **Update for `build`:** 130 | 131 | ```diff 132 | -"build": "webpack -c ./.config/webpack/webpack.config.ts --env production", 133 | +"build": "webpack -c ./webpack.config.ts --env production", 134 | ``` 135 | 136 | **Update for `dev`:** 137 | 138 | ```diff 139 | -"dev": "webpack -w -c ./.config/webpack/webpack.config.ts --env development", 140 | +"dev": "webpack -w -c ./webpack.config.ts --env development", 141 | ``` 142 | 143 | ### Configure grafana image to use when running docker 144 | 145 | By default, `grafana-enterprise` will be used as the docker image for all docker related commands. If you want to override this behavior, simply alter the `docker-compose.yaml` by adding the following build arg `grafana_image`. 146 | 147 | **Example:** 148 | 149 | ```yaml 150 | version: '3.7' 151 | 152 | services: 153 | grafana: 154 | container_name: 'myorg-basic-app' 155 | build: 156 | context: ./.config 157 | args: 158 | grafana_version: ${GRAFANA_VERSION:-9.1.2} 159 | grafana_image: ${GRAFANA_IMAGE:-grafana} 160 | ``` 161 | 162 | In this example, we assign the environment variable `GRAFANA_IMAGE` to the build arg `grafana_image` with a default value of `grafana`. This will allow you to set the value while running the docker-compose commands, which might be convenient in some scenarios. 163 | 164 | --- 165 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/blackcowmoo/grafana-google-analytics-dataSource 2 | 3 | go 1.23.0 4 | 5 | require ( 6 | github.com/grafana/grafana-plugin-sdk-go v0.250.0 7 | github.com/jinzhu/copier v0.3.5 8 | github.com/patrickmn/go-cache v2.1.0+incompatible 9 | golang.org/x/oauth2 v0.23.0 10 | google.golang.org/api v0.162.0 11 | ) 12 | 13 | require ( 14 | cloud.google.com/go/compute/metadata v0.3.0 // indirect 15 | github.com/BurntSushi/toml v1.3.2 // indirect 16 | github.com/apache/arrow/go/v15 v15.0.2 // indirect 17 | github.com/beorn7/perks v1.0.1 // indirect 18 | github.com/cenkalti/backoff/v4 v4.3.0 // indirect 19 | github.com/cespare/xxhash/v2 v2.3.0 // indirect 20 | github.com/cheekybits/genny v1.0.0 // indirect 21 | github.com/chromedp/cdproto v0.0.0-20220428002153-285dfb42699c // indirect 22 | github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect 23 | github.com/elazarl/goproxy v0.0.0-20230731152917-f99041a5c027 // indirect 24 | github.com/fatih/color v1.15.0 // indirect 25 | github.com/felixge/httpsnoop v1.0.4 // indirect 26 | github.com/getkin/kin-openapi v0.131.0 // indirect 27 | github.com/go-logr/logr v1.4.2 // indirect 28 | github.com/go-logr/stdr v1.2.2 // indirect 29 | github.com/go-openapi/jsonpointer v0.21.0 // indirect 30 | github.com/go-openapi/swag v0.23.0 // indirect 31 | github.com/goccy/go-json v0.10.2 // indirect 32 | github.com/gogo/protobuf v1.3.2 // indirect 33 | github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect 34 | github.com/golang/protobuf v1.5.4 // indirect 35 | github.com/google/flatbuffers v23.5.26+incompatible // indirect 36 | github.com/google/go-cmp v0.6.0 // indirect 37 | github.com/google/s2a-go v0.1.7 // indirect 38 | github.com/google/uuid v1.6.0 // indirect 39 | github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect 40 | github.com/googleapis/gax-go/v2 v2.12.0 // indirect 41 | github.com/gorilla/mux v1.8.1 // indirect 42 | github.com/grafana/otel-profiling-go v0.5.1 // indirect 43 | github.com/grafana/pyroscope-go/godeltaprof v0.1.8 // indirect 44 | github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.1 // indirect 45 | github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0 // indirect 46 | github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect 47 | github.com/hashicorp/go-hclog v1.6.3 // indirect 48 | github.com/hashicorp/go-plugin v1.6.1 // indirect 49 | github.com/hashicorp/yamux v0.1.1 // indirect 50 | github.com/josharian/intern v1.0.0 // indirect 51 | github.com/json-iterator/go v1.1.12 // indirect 52 | github.com/klauspost/compress v1.17.9 // indirect 53 | github.com/klauspost/cpuid/v2 v2.2.5 // indirect 54 | github.com/magefile/mage v1.15.0 // indirect 55 | github.com/mailru/easyjson v0.7.7 // indirect 56 | github.com/mattetti/filebuffer v1.0.1 // indirect 57 | github.com/mattn/go-colorable v0.1.13 // indirect 58 | github.com/mattn/go-isatty v0.0.19 // indirect 59 | github.com/mattn/go-runewidth v0.0.9 // indirect 60 | github.com/mitchellh/go-testing-interface v1.14.1 // indirect 61 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect 62 | github.com/modern-go/reflect2 v1.0.2 // indirect 63 | github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect 64 | github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect 65 | github.com/oasdiff/yaml v0.0.0-20250309154309-f31be36b4037 // indirect 66 | github.com/oasdiff/yaml3 v0.0.0-20250309153720-d2182401db90 // indirect 67 | github.com/oklog/run v1.1.0 // indirect 68 | github.com/olekukonko/tablewriter v0.0.5 // indirect 69 | github.com/perimeterx/marshmallow v1.1.5 // indirect 70 | github.com/pierrec/lz4/v4 v4.1.18 // indirect 71 | github.com/prometheus/client_golang v1.20.3 // indirect 72 | github.com/prometheus/client_model v0.6.1 // indirect 73 | github.com/prometheus/common v0.55.0 // indirect 74 | github.com/prometheus/procfs v0.15.1 // indirect 75 | github.com/russross/blackfriday/v2 v2.1.0 // indirect 76 | github.com/unknwon/bra v0.0.0-20200517080246-1e3013ecaff8 // indirect 77 | github.com/unknwon/com v1.0.1 // indirect 78 | github.com/unknwon/log v0.0.0-20150304194804-e617c87089d3 // indirect 79 | github.com/urfave/cli v1.22.15 // indirect 80 | github.com/zeebo/xxh3 v1.0.2 // indirect 81 | go.opencensus.io v0.24.0 // indirect 82 | go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.53.0 // indirect 83 | go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace v0.53.0 // indirect 84 | go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.53.0 // indirect 85 | go.opentelemetry.io/contrib/propagators/jaeger v1.29.0 // indirect 86 | go.opentelemetry.io/contrib/samplers/jaegerremote v0.23.0 // indirect 87 | go.opentelemetry.io/otel v1.29.0 // indirect 88 | go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.29.0 // indirect 89 | go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.28.0 // indirect 90 | go.opentelemetry.io/otel/metric v1.29.0 // indirect 91 | go.opentelemetry.io/otel/sdk v1.29.0 // indirect 92 | go.opentelemetry.io/otel/trace v1.29.0 // indirect 93 | go.opentelemetry.io/proto/otlp v1.3.1 // indirect 94 | golang.org/x/crypto v0.36.0 // indirect 95 | golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect 96 | golang.org/x/mod v0.17.0 // indirect 97 | golang.org/x/net v0.38.0 // indirect 98 | golang.org/x/sync v0.12.0 // indirect 99 | golang.org/x/sys v0.31.0 // indirect 100 | golang.org/x/text v0.23.0 // indirect 101 | golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect 102 | golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect 103 | google.golang.org/genproto/googleapis/api v0.0.0-20240822170219-fc7c04adadcd // indirect 104 | google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect 105 | google.golang.org/grpc v1.66.0 // indirect 106 | google.golang.org/protobuf v1.34.2 // indirect 107 | gopkg.in/fsnotify/fsnotify.v1 v1.4.7 // indirect 108 | gopkg.in/yaml.v3 v3.0.1 // indirect 109 | ) 110 | -------------------------------------------------------------------------------- /src/ConfigEditor.tsx: -------------------------------------------------------------------------------- 1 | import { DataSourcePluginOptionsEditorProps } from '@grafana/data'; 2 | import { Alert, RadioButtonGroup } from '@grafana/ui'; 3 | import React, { PureComponent } from 'react'; 4 | import { GADataSourceOptions, GASecureJsonData } from 'types'; 5 | import { JWTConfig } from './JWTConfig'; 6 | 7 | const gaVersion = [ 8 | { label: 'UA(GA3)', value: 'v3' }, 9 | { label: 'GA4(beta)', value: 'v4' }, 10 | ]; 11 | 12 | export type Props = DataSourcePluginOptionsEditorProps; 13 | 14 | export class ConfigEditor extends PureComponent { 15 | constructor(props: Readonly) { 16 | super(props); 17 | if (!this.props.options.jsonData.version) { 18 | this.props.options.jsonData.version = 'v4'; 19 | } 20 | } 21 | onResetProfileId = () => { 22 | const { options } = this.props; 23 | this.props.onOptionsChange({ 24 | ...options, 25 | secureJsonData: { 26 | ...options.secureJsonData, 27 | }, 28 | secureJsonFields: { 29 | ...options.secureJsonFields, 30 | }, 31 | }); 32 | }; 33 | 34 | render() { 35 | const { options, onOptionsChange } = this.props; 36 | const { secureJsonFields } = options; 37 | const secureJsonData = options.secureJsonData; 38 | const jsonData = options.jsonData; 39 | return ( 40 |
41 | 44 | onOptionsChange({ 45 | ...options, 46 | jsonData: { 47 | ...jsonData, 48 | version: v, 49 | }, 50 | }) 51 | } 52 | value={options.jsonData.version} 53 | /> 54 | <> 55 | { 58 | onOptionsChange({ 59 | ...options, 60 | secureJsonData: { 61 | ...secureJsonData, 62 | jwt, 63 | }, 64 | }); 65 | }} 66 | > 67 | 68 | 69 |
    70 |
  1. 71 | if you don't have gcp project, add new gcp project. 72 | link 73 |
  2. 74 |
  3. 75 | Open the Credentials page in the 76 | Google API Console. 77 |
  4. 78 |
  5. 79 | Click Create Credentials then click Service account. 80 |
  6. 81 |
  7. On the Create service account page, enter the Service account details.
  8. 82 |
  9. 83 | On the Create service account page, fill in the Service account details and then 84 | click Create 85 |
  10. 86 |
  11. 87 | On the Service account permissions page, don't add a role to the service account. Just 88 | click Continue 89 |
  12. 90 |
  13. 91 | In the next step, click Create Key. Choose key type JSON and click 92 | Create. A JSON key file will be created and downloaded to your computer 93 |
  14. 94 |
  15. 95 | Note your service account email ex) *@*.iam.gserviceaccount.com 96 |
  16. 97 |
  17. 98 | Open the 99 | {jsonData.version === 'v3' ? ( 100 | <> 101 | 102 | Google Analytics API(UA) 103 | 104 | 105 | ) : ( 106 | <> 107 | 108 | Google Analytics Admin API(GA4) 109 | 110 | 111 | )} 112 | in API Library and enable access for your account 113 |
  18. 114 |
  19. 115 | Open the 116 | {jsonData.version === 'v3' ? ( 117 | <> 118 | 119 | Google Analytics Reporting API(UA) 120 | 121 | 122 | ) : ( 123 | <> 124 | 125 | Google Analytics Data API(GA4) 126 | 127 | 128 | )} 129 | in API Library and enable access for your Analytics Data 130 |
  20. 131 |
  21. 132 | Check your api setting 133 |
  22. 134 |
135 | 136 |

Google Analytics Setting

137 |
    138 |
  1. 139 | Open the Google Analytics 140 |
  2. 141 |
  3. Select Your Analytics Account And Open Admin Page
  4. 142 |
  5. 143 | Click Account User Management on the Account Tab 144 |
  6. 145 |
  7. Click plus Button then Add users
  8. 146 |
  9. 147 | Enter service account email at Generate a JWT file 8th step and Permissions 148 | add Read & Analyze 149 |
  10. 150 |
151 |
152 |
153 | ); 154 | } 155 | } 156 | -------------------------------------------------------------------------------- /pkg/gav3/client.go: -------------------------------------------------------------------------------- 1 | package gav3 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | 7 | "github.com/blackcowmoo/grafana-google-analytics-dataSource/pkg/model" 8 | "github.com/blackcowmoo/grafana-google-analytics-dataSource/pkg/util" 9 | "github.com/grafana/grafana-plugin-sdk-go/backend/log" 10 | "golang.org/x/oauth2/google" 11 | "google.golang.org/api/option" 12 | 13 | analytics "google.golang.org/api/analytics/v3" 14 | reporting "google.golang.org/api/analyticsreporting/v4" 15 | ) 16 | 17 | type GoogleClient struct { 18 | reporting *reporting.Service 19 | analytics *analytics.Service 20 | } 21 | 22 | func NewGoogleClient(ctx context.Context, jwt string) (*GoogleClient, error) { 23 | reportingService, reportingError := createReportingService(ctx, jwt) 24 | if reportingError != nil { 25 | return nil, reportingError 26 | } 27 | analyticsService, analyticsError := createAnalyticsService(ctx, jwt) 28 | if analyticsError != nil { 29 | return nil, analyticsError 30 | } 31 | 32 | return &GoogleClient{reportingService, analyticsService}, nil 33 | } 34 | 35 | func createReportingService(ctx context.Context, jwt string) (*reporting.Service, error) { 36 | jwtConfig, err := google.JWTConfigFromJSON([]byte(jwt), reporting.AnalyticsReadonlyScope) 37 | if err != nil { 38 | return nil, fmt.Errorf("error parsing JWT file: %w", err) 39 | } 40 | 41 | client := jwtConfig.Client(ctx) 42 | return reporting.NewService(ctx, option.WithHTTPClient(client)) 43 | } 44 | 45 | func createAnalyticsService(ctx context.Context, jwt string) (*analytics.Service, error) { 46 | jwtConfig, err := google.JWTConfigFromJSON([]byte(jwt), analytics.AnalyticsReadonlyScope) 47 | if err != nil { 48 | return nil, fmt.Errorf("error parsing JWT file: %w", err) 49 | } 50 | 51 | client := jwtConfig.Client(ctx) 52 | return analytics.NewService(ctx, option.WithHTTPClient(client)) 53 | } 54 | 55 | func (client *GoogleClient) getProfile(accountId, webpropertyId, profileId string) (*analytics.Profile, error) { 56 | profile, err := client.analytics.Management.Profiles.Get(accountId, webpropertyId, profileId).Do() 57 | if err != nil { 58 | log.DefaultLogger.Error("getProfile fail", "error", err.Error(), "accountId", accountId, "webpropertyId", webpropertyId) 59 | return nil, err 60 | } 61 | 62 | return profile, nil 63 | } 64 | 65 | func (client *GoogleClient) getReport(query model.QueryModel) (*reporting.GetReportsResponse, error) { 66 | defer util.Elapsed("Get report data at GA API")() 67 | log.DefaultLogger.Debug("getReport", "queries", query) 68 | Metrics := []*reporting.Metric{} 69 | Dimensions := []*reporting.Dimension{} 70 | for _, metric := range query.Metrics { 71 | Metrics = append(Metrics, &reporting.Metric{Expression: metric}) 72 | } 73 | for _, dimension := range query.Dimensions { 74 | Dimensions = append(Dimensions, &reporting.Dimension{Name: dimension}) 75 | } 76 | 77 | reportRequest := reporting.ReportRequest{ 78 | ViewId: query.ProfileID, 79 | DateRanges: []*reporting.DateRange{ 80 | // Create the DateRange object. 81 | {StartDate: query.StartDate, EndDate: query.EndDate}, 82 | }, 83 | Metrics: Metrics, 84 | Dimensions: Dimensions, 85 | PageSize: query.PageSize, 86 | PageToken: query.PageToken, 87 | IncludeEmptyRows: true, 88 | FiltersExpression: query.FiltersExpression, 89 | } 90 | 91 | log.DefaultLogger.Debug("getReport", "reportRequests", reportRequest) 92 | 93 | // A GetReportsRequest instance is a batch request 94 | // which can have a maximum of 5 requests 95 | req := &reporting.GetReportsRequest{ 96 | // Our request contains only one request 97 | // So initialise the slice with one ga.ReportRequest object 98 | ReportRequests: []*reporting.ReportRequest{&reportRequest}, 99 | } 100 | 101 | log.DefaultLogger.Debug("Doing GET request from analytics reporting", "req", req) 102 | // Call the BatchGet method and return the response. 103 | report, err := client.reporting.Reports.BatchGet(req).Do() 104 | if err != nil { 105 | return nil, fmt.Errorf(err.Error()) 106 | } 107 | 108 | log.DefaultLogger.Debug("Do GET report", "report len", len(report.Reports), "report", report) 109 | 110 | if report.Reports[0].NextPageToken != "" { 111 | query.PageToken = report.Reports[0].NextPageToken 112 | newReport, err := client.getReport(query) 113 | if err != nil { 114 | return nil, fmt.Errorf(err.Error()) 115 | } 116 | 117 | report.Reports[0].Data.Rows = append(report.Reports[0].Data.Rows, newReport.Reports[0].Data.Rows...) 118 | return report, nil 119 | } 120 | return report, nil 121 | } 122 | 123 | func printResponse(res *reporting.GetReportsResponse) { 124 | log.DefaultLogger.Debug("Printing Response from analytics reporting", "") 125 | for _, report := range res.Reports { 126 | header := report.ColumnHeader 127 | dimHdrs := header.Dimensions 128 | metricHdrs := header.MetricHeader.MetricHeaderEntries 129 | rows := report.Data.Rows 130 | 131 | if rows == nil { 132 | log.DefaultLogger.Debug("no data", "") 133 | } 134 | for _, row := range rows { 135 | dims := row.Dimensions 136 | metrics := row.Metrics 137 | 138 | for i := 0; i < len(dimHdrs) && i < len(dims); i++ { 139 | log.DefaultLogger.Debug("%s: %s", dimHdrs[i], dims[i]) 140 | } 141 | 142 | for _, metric := range metrics { 143 | // We have only 1 date range in the example 144 | // So it'll always print "Date Range (0)" 145 | // log.DefaultLogger.Defaultlog.DefaultLogger.Infof("Date Range (%d)", idx) 146 | for j := 0; j < len(metricHdrs) && j < len(metric.Values); j++ { 147 | log.DefaultLogger.Debug("%s: %s", metricHdrs[j].Name, metric.Values[j]) 148 | } 149 | } 150 | } 151 | } 152 | log.DefaultLogger.Info("Completed printing response", "", "") 153 | } 154 | 155 | func (client *GoogleClient) getAccountSummaries(start int64) ([]*analytics.AccountSummary, error) { 156 | accountSummaries, err := client.analytics.Management.AccountSummaries.List().MaxResults(GaManageMaxResult).StartIndex(start).Do() 157 | if err != nil { 158 | log.DefaultLogger.Error("getAccountSummary fail", "error", err.Error()) 159 | return nil, err 160 | } 161 | 162 | if accountSummaries.TotalResults > (start + GaManageMaxResult - 1) { 163 | start += GaManageMaxResult 164 | nextAccountSummaries, err := client.getAccountSummaries(start) 165 | if err != nil { 166 | return nil, err 167 | } 168 | accountSummaries.Items = append(accountSummaries.Items, nextAccountSummaries...) 169 | } 170 | 171 | return accountSummaries.Items, nil 172 | } 173 | -------------------------------------------------------------------------------- /src/DataSource.ts: -------------------------------------------------------------------------------- 1 | import { DataSourceInstanceSettings, ScopedVars, SelectableValue } from '@grafana/data'; 2 | import { DataSourceWithBackend, getTemplateSrv } from '@grafana/runtime'; 3 | import { CascaderOption } from '@grafana/ui'; 4 | import { AccountSummary, GADataSourceOptions, GAMetadata, GAQuery } from './types'; 5 | 6 | export class DataSource extends DataSourceWithBackend { 7 | version: string; 8 | constructor(instanceSettings: DataSourceInstanceSettings) { 9 | super(instanceSettings); 10 | this.version = instanceSettings.jsonData.version; 11 | } 12 | 13 | applyTemplateVariables(query: GAQuery, scopedVars: ScopedVars): Record { 14 | const templateSrv = getTemplateSrv(); 15 | let dimensionFilter = query.dimensionFilter; 16 | dimensionFilter?.orGroup?.expressions.map((expression) => { 17 | if (expression.filter?.stringFilter) { 18 | expression.filter.stringFilter.value = templateSrv.replace(expression.filter.stringFilter.value, scopedVars); 19 | } 20 | if (expression.filter?.inListFilter) { 21 | expression.filter.inListFilter.values = expression.filter.inListFilter.values.map((value) => { 22 | value = templateSrv.replace(value, scopedVars); 23 | return value; 24 | }); 25 | } 26 | return expression; 27 | }); 28 | return { 29 | ...query, 30 | dimensionFilter, 31 | }; 32 | } 33 | async getAccountSummaries(): Promise { 34 | let accountSummaries = (await this.getResource('account-summaries')).accountSummaries as AccountSummary[]; 35 | let accounts: CascaderOption[] = []; 36 | for (const accountSummary of accountSummaries) { 37 | let accountCascader: CascaderOption = { 38 | label: accountSummary.DisplayName, 39 | value: accountSummary.Account, 40 | }; 41 | let properties: CascaderOption[] = []; 42 | for (const propertySummary of accountSummary.PropertySummaries) { 43 | let propertyCascader: CascaderOption = { 44 | label: propertySummary.DisplayName, 45 | value: propertySummary.Property, 46 | }; 47 | properties.push(propertyCascader); 48 | let profiles: CascaderOption[] = []; 49 | 50 | if (!propertySummary.ProfileSummaries) { 51 | continue; 52 | } 53 | for (const profileSummary of propertySummary.ProfileSummaries) { 54 | let profileCascader: CascaderOption = { 55 | label: profileSummary.DisplayName, 56 | value: profileSummary.Profile, 57 | }; 58 | profiles.push(profileCascader); 59 | } 60 | propertyCascader.children = profiles; 61 | propertyCascader.items = profiles; 62 | } 63 | accountCascader.children = properties; 64 | accountCascader.items = properties; 65 | accounts.push(accountCascader); 66 | } 67 | return accounts; 68 | } 69 | 70 | async getTimezone(accountId: string, webPropertyId: string, profileId: string): Promise { 71 | return this.getResource('profile/timezone', { accountId, webPropertyId, profileId }).then(({ timezone }) => { 72 | return timezone; 73 | }); 74 | } 75 | 76 | async getServiceLevel(accountId: string, webPropertyId: string): Promise { 77 | return this.getResource('property/service-level', { accountId, webPropertyId }).then(({ serviceLevel }) => { 78 | return serviceLevel; 79 | }); 80 | } 81 | 82 | async getMetrics(query: string, webPropertyId: string): Promise>> { 83 | return this.getResource('metrics', { webPropertyId }).then(({ metrics }) => { 84 | let test = metrics.reduce((pre: Array>, element: GAMetadata) => { 85 | if ( 86 | element.id.toLowerCase().indexOf(query) > -1 || 87 | element.attributes.uiName.toLowerCase().indexOf(query) > -1 88 | ) { 89 | pre.push({ 90 | label: element.attributes.uiName, 91 | value: element.id, 92 | description: element.attributes.description, 93 | } as SelectableValue); 94 | } 95 | return pre; 96 | }, []); 97 | return test; 98 | }); 99 | } 100 | 101 | async getDimensions(query: string, exclude: any, webPropertyId: string): Promise>> { 102 | return this.getResource('dimensions', { webPropertyId }).then(({ dimensions }) => { 103 | return dimensions.reduce((pre: Array>, element: GAMetadata) => { 104 | if ( 105 | (element.id.toLowerCase().indexOf(query) > -1 || 106 | element.attributes.uiName.toLowerCase().indexOf(query) > -1) && 107 | !( 108 | element.id.toLowerCase().indexOf(exclude) > -1 || 109 | element.attributes.uiName.toLowerCase().indexOf(exclude) > -1 110 | ) 111 | ) { 112 | pre.push({ 113 | label: element.attributes.uiName, 114 | value: element.id, 115 | description: element.attributes.description, 116 | } as SelectableValue); 117 | } 118 | return pre; 119 | }, []); 120 | }); 121 | } 122 | 123 | async getRealtimeMetrics(query: string, webPropertyId: string): Promise>> { 124 | return this.getResource('realtime-metrics', { webPropertyId }).then(({ metrics }) => { 125 | let test = metrics.reduce((pre: Array>, element: GAMetadata) => { 126 | if ( 127 | element.id.toLowerCase().indexOf(query) > -1 || 128 | element.attributes.uiName.toLowerCase().indexOf(query) > -1 129 | ) { 130 | pre.push({ 131 | label: element.attributes.uiName, 132 | value: element.id, 133 | description: element.attributes.description, 134 | } as SelectableValue); 135 | } 136 | return pre; 137 | }, []); 138 | return test; 139 | }); 140 | } 141 | 142 | async getRealtimeDimensions( 143 | query: string, 144 | exclude: any, 145 | webPropertyId: string 146 | ): Promise>> { 147 | return this.getResource('realtime-dimensions', { webPropertyId }).then(({ dimensions }) => { 148 | return dimensions.reduce((pre: Array>, element: GAMetadata) => { 149 | if ( 150 | (element.id.toLowerCase().indexOf(query) > -1 || 151 | element.attributes.uiName.toLowerCase().indexOf(query) > -1) && 152 | !( 153 | element.id.toLowerCase().indexOf(exclude) > -1 || 154 | element.attributes.uiName.toLowerCase().indexOf(exclude) > -1 155 | ) 156 | ) { 157 | pre.push({ 158 | label: element.attributes.uiName, 159 | value: element.id, 160 | description: element.attributes.description, 161 | } as SelectableValue); 162 | } 163 | return pre; 164 | }, []); 165 | }); 166 | } 167 | 168 | async getTimeDimensions(): Promise>> { 169 | return this.getDimensions('date', null, ''); 170 | } 171 | 172 | async getDimensionsExcludeTimeDimensions( 173 | query: string, 174 | webPropertyId: string 175 | ): Promise>> { 176 | return await this.getDimensions(query, 'date', webPropertyId); 177 | } 178 | getGaVersion(): string { 179 | return this.version; 180 | } 181 | } 182 | -------------------------------------------------------------------------------- /tests/query/queryEdiyor.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect, test } from '@grafana/plugin-e2e'; 2 | import * as fs from 'fs'; 3 | import * as path from 'path'; 4 | 5 | // 대시보드 버전 목록 가져오기 6 | const getDashboardVersions = () => { 7 | const dashboardsDir = path.join(__dirname, '../../provisioning/dashboards'); 8 | const files = fs.readdirSync(dashboardsDir); 9 | return files 10 | .filter(file => file.match(/^v\d+\.\d+\.\d+\.json$/)) 11 | .map(file => file.replace('.json', '')); 12 | }; 13 | 14 | // 각 버전별 마이그레이션 테스트 15 | getDashboardVersions().forEach(version => { 16 | test(`${version} migration test`, async ({ readProvisionedDataSource, readProvisionedDashboard, gotoDashboardPage }) => { 17 | const dashboard = await readProvisionedDashboard({fileName: `${version}.json`}); 18 | const dashboardPage = await gotoDashboardPage({uid: dashboard.uid}); 19 | await dashboardPage.refreshDashboard(); 20 | await expect(dashboardPage.waitForQueryDataResponse()).toBeOK(); 21 | }); 22 | }); 23 | 24 | test('time series', async ({ readProvisionedDataSource, explorePage, page }) => { 25 | // default settings 26 | const ds = await readProvisionedDataSource({ fileName: 'datasources.yml' }); 27 | 28 | await explorePage.datasource.set(ds.name); 29 | await explorePage.timeRange.set({ from: 'now-7d', to: 'now' }); 30 | 31 | let queryMode = explorePage.getQueryEditorRow('A').getByLabel('query-mode').getByLabel('Time Series') 32 | // for grafana version < 10.4.5 33 | if(await queryMode.count()==0){ 34 | queryMode = explorePage.getQueryEditorRow('A').getByText('Time Series',{exact: true}) 35 | } 36 | await queryMode.check() 37 | 38 | // account select 39 | await explorePage.getQueryEditorRow('A').getByRole('button', { name: 'Account Select' }).click(); 40 | await page.getByText('Default Account for Firebase').click(); 41 | await page.getByText('gitblog - GA4').click(); 42 | // await page.waitForResponse((response) => response.url().includes('resources/property/service-level')); 43 | await expect(explorePage.getQueryEditorRow('A').getByLabel('account-info')).toContainText(/.*properties\/.*/); 44 | // metrics select 45 | await explorePage.getQueryEditorRow('A').getByLabel('metrics').click(); 46 | await expect(page.getByLabel('Select options menu')).toBeVisible(); 47 | await page.getByLabel('Select options menu').getByText('Active users', { exact: true }).click(); 48 | // time dimension 49 | await explorePage.getQueryEditorRow('A').getByLabel('time-dimension').click(); 50 | await expect(page.getByLabel('Select options menu')).toBeVisible(); 51 | await page.getByLabel('Select options menu').getByText('Date + hour (YYYYMMDDHH)', { exact: true }).click(); 52 | 53 | // dimensions 54 | await explorePage.getQueryEditorRow('A').getByLabel('dimensions').click(); 55 | await expect(page.getByLabel('Select options menu')).toBeVisible(); 56 | await page.getByLabel('Select options menu').getByText('Country', { exact: true }).click(); 57 | 58 | await expect(explorePage.timeSeriesPanel.waitForQueryDataResponse()).toBeOK() 59 | // await page.getByRole('combobox', { name: 'Query type' }).click(); 60 | // await panelEditPage.getByGrafanaSelector(selectors.components.Select.option).getByText('Table').click(); 61 | // await expect(panelEditPage.panel.fieldNames).toHaveText(['time', 'temperature outside', 'temperature inside']); 62 | }); 63 | 64 | 65 | test('table', async ({ readProvisionedDataSource, explorePage, page }) => { 66 | // default settings 67 | const ds = await readProvisionedDataSource({ fileName: 'datasources.yml' }); 68 | 69 | await explorePage.datasource.set(ds.name); 70 | await explorePage.timeRange.set({ from: 'now-7d', to: 'now' }); 71 | let queryMode = explorePage.getQueryEditorRow('A').getByLabel('query-mode').getByLabel('Table') 72 | // for grafana version < 10.4.5 73 | if(await queryMode.count()==0){ 74 | queryMode = explorePage.getQueryEditorRow('A').getByText('Table',{exact: true}) 75 | } 76 | await queryMode.check() // account select 77 | await explorePage.getQueryEditorRow('A').getByRole('button', { name: 'Account Select' }).click(); 78 | await page.getByText('Default Account for Firebase').click(); 79 | await page.getByText('gitblog - GA4').click(); 80 | // await page.waitForResponse((response) => response.url().includes('resources/property/service-level')); 81 | await expect(explorePage.getQueryEditorRow('A').getByLabel('account-info')).toContainText(/.*properties\/.*/); 82 | // metrics select 83 | await explorePage.getQueryEditorRow('A').getByLabel('metrics').click(); 84 | await expect(page.getByLabel('Select options menu')).toBeVisible(); 85 | await page.getByLabel('Select options menu').getByText('Active users', { exact: true }).click(); 86 | // time dimension 87 | await explorePage.getQueryEditorRow('A').getByLabel('time-dimension').click(); 88 | await expect(page.getByLabel('Select options menu')).toBeVisible(); 89 | await page.getByLabel('Select options menu').getByText('Date + hour (YYYYMMDDHH)', { exact: true }).click(); 90 | 91 | // dimensions 92 | await explorePage.getQueryEditorRow('A').getByLabel('dimensions').click(); 93 | await expect(page.getByLabel('Select options menu')).toBeVisible(); 94 | await page.getByLabel('Select options menu').getByText('Country', { exact: true }).click(); 95 | 96 | await expect(explorePage.timeSeriesPanel.waitForQueryDataResponse()).toBeOK() 97 | // await page.getByRole('combobox', { name: 'Query type' }).click(); 98 | // await panelEditPage.getByGrafanaSelector(selectors.components.Select.option).getByText('Table').click(); 99 | // await expect(panelEditPage.panel.fieldNames).toHaveText(['time', 'temperature outside', 'temperature inside']); 100 | }); 101 | 102 | 103 | test('realtime', async ({ readProvisionedDataSource, explorePage, page }) => { 104 | // default settings 105 | const ds = await readProvisionedDataSource({ fileName: 'datasources.yml' }); 106 | 107 | await explorePage.datasource.set(ds.name); 108 | await explorePage.timeRange.set({ from: 'now-7d', to: 'now' }); 109 | let queryMode = explorePage.getQueryEditorRow('A').getByLabel('query-mode').getByLabel('Realtime') 110 | // for grafana version < 10.4.5 111 | if(await queryMode.count()==0){ 112 | queryMode = explorePage.getQueryEditorRow('A').getByText('Realtime',{exact: true}) 113 | } 114 | await queryMode.check() // account select 115 | await explorePage.getQueryEditorRow('A').getByRole('button', { name: 'Account Select' }).click(); 116 | await page.getByText('Default Account for Firebase').click(); 117 | await page.getByText('gitblog - GA4').click(); 118 | // await page.waitForResponse((response) => response.url().includes('resources/property/service-level')); 119 | await expect(explorePage.getQueryEditorRow('A').getByLabel('account-info')).toContainText(/.*properties\/.*/); 120 | // metrics select 121 | await explorePage.getQueryEditorRow('A').getByLabel('metrics').click(); 122 | await expect(page.getByLabel('Select options menu')).toBeVisible(); 123 | await page.getByLabel('Select options menu').getByText('Active users', { exact: true }).click(); 124 | // time dimension 125 | await expect(explorePage.getQueryEditorRow('A').getByLabel('time-dimension')).toBeDisabled(); 126 | 127 | // dimensions 128 | await explorePage.getQueryEditorRow('A').getByLabel('dimensions').click(); 129 | await expect(page.getByLabel('Select options menu')).toBeVisible(); 130 | await page.getByLabel('Select options menu').getByText('Country', { exact: true }).click(); 131 | 132 | await expect(explorePage.timeSeriesPanel.waitForQueryDataResponse()).toBeOK() 133 | // await page.getByRole('combobox', { name: 'Query type' }).click(); 134 | // await panelEditPage.getByGrafanaSelector(selectors.components.Select.option).getByText('Table').click(); 135 | // await expect(panelEditPage.panel.fieldNames).toHaveText(['time', 'temperature outside', 'temperature inside']); 136 | }); 137 | -------------------------------------------------------------------------------- /pkg/datasource.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "encoding/json" 6 | "net/http" 7 | "time" 8 | 9 | "github.com/blackcowmoo/grafana-google-analytics-dataSource/pkg/gav3" 10 | "github.com/blackcowmoo/grafana-google-analytics-dataSource/pkg/gav4" 11 | "github.com/blackcowmoo/grafana-google-analytics-dataSource/pkg/setting" 12 | "github.com/grafana/grafana-plugin-sdk-go/backend" 13 | "github.com/grafana/grafana-plugin-sdk-go/backend/instancemgmt" 14 | "github.com/grafana/grafana-plugin-sdk-go/backend/log" 15 | "github.com/grafana/grafana-plugin-sdk-go/backend/resource/httpadapter" 16 | "github.com/grafana/grafana-plugin-sdk-go/data" 17 | "github.com/patrickmn/go-cache" 18 | ) 19 | 20 | // GoogleAnalyticsDataSource handler for google sheets 21 | type GoogleAnalyticsDataSource struct { 22 | analytics GoogleAnalytics 23 | resourceHandler backend.CallResourceHandler 24 | } 25 | 26 | // NewDataSource creates the google analytics datasource and sets up all the routes 27 | func NewDataSource(_ context.Context, dis backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) { 28 | version := &setting.DatasourceSettings{} 29 | cache := cache.New(300*time.Second, 5*time.Second) 30 | mux := http.NewServeMux() 31 | err := json.Unmarshal(dis.JSONData, &version) 32 | if err != nil { 33 | return nil, err 34 | } 35 | 36 | var analytics GoogleAnalytics 37 | if version.Version == "v3" { 38 | analytics = &gav3.GoogleAnalytics{ 39 | Cache: cache, 40 | } 41 | } else { 42 | analytics = &gav4.GoogleAnalytics{ 43 | Cache: cache, 44 | } 45 | } 46 | 47 | ds := &GoogleAnalyticsDataSource{ 48 | analytics: analytics, 49 | resourceHandler: httpadapter.New(mux), 50 | } 51 | mux.HandleFunc("/profile/timezone", ds.handleResourceProfileTimezone) 52 | mux.HandleFunc("/dimensions", ds.handleResourceDimensions) 53 | mux.HandleFunc("/metrics", ds.handleResourceMetrics) 54 | mux.HandleFunc("/account-summaries", ds.handleResourceAccountSummaries) 55 | mux.HandleFunc("/property/service-level", ds.handleResourcePropertyServiceLevel) 56 | mux.HandleFunc("/realtime-dimensions", ds.handleResourceRealtimeDimensions) 57 | mux.HandleFunc("/realtime-metrics", ds.handleResourceRealtimeMetrics) 58 | 59 | return ds, nil 60 | } 61 | 62 | func (ds *GoogleAnalyticsDataSource) CallResource(ctx context.Context, req *backend.CallResourceRequest, sender backend.CallResourceResponseSender) error { 63 | return ds.resourceHandler.CallResource(ctx, req, sender) 64 | } 65 | 66 | // CheckHealth checks if the plugin is running properly 67 | func (ds *GoogleAnalyticsDataSource) CheckHealth(ctx context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) { 68 | config, err := setting.LoadSettings(req.PluginContext) 69 | 70 | if err != nil { 71 | log.DefaultLogger.Error("CheckHealth: Fail LoadSetting", "error", err.Error()) 72 | return &backend.CheckHealthResult{ 73 | Status: backend.HealthStatusError, 74 | Message: "Setting Configuration Read Fail", 75 | }, nil 76 | } 77 | return ds.analytics.CheckHealth(ctx, config) 78 | } 79 | 80 | // QueryData queries for data. 81 | func (ds *GoogleAnalyticsDataSource) QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) { 82 | res := backend.NewQueryDataResponse() 83 | config, err := setting.LoadSettings(req.PluginContext) 84 | if err != nil { 85 | return nil, err 86 | } 87 | 88 | for _, query := range req.Queries { 89 | frames, err := ds.analytics.Query(ctx, config, query) 90 | if err != nil { 91 | log.DefaultLogger.Error("Fail query", "error", err) 92 | res.Responses[query.RefID] = backend.DataResponse{Frames: data.Frames{}, Error: err} 93 | continue 94 | } 95 | res.Responses[query.RefID] = backend.DataResponse{Frames: *frames, Error: err} 96 | } 97 | 98 | return res, nil 99 | } 100 | 101 | func writeResult(rw http.ResponseWriter, path string, val interface{}, err error) { 102 | response := make(map[string]interface{}) 103 | code := http.StatusOK 104 | if err != nil { 105 | response["error"] = err.Error() 106 | code = http.StatusBadRequest 107 | } else { 108 | response[path] = val 109 | } 110 | 111 | body, err := json.Marshal(response) 112 | if err != nil { 113 | body = []byte(err.Error()) 114 | code = http.StatusInternalServerError 115 | } 116 | _, err = rw.Write(body) 117 | if err != nil { 118 | code = http.StatusInternalServerError 119 | } 120 | rw.WriteHeader(code) 121 | } 122 | 123 | func (ds *GoogleAnalyticsDataSource) handleResourceDimensions(rw http.ResponseWriter, req *http.Request) { 124 | if req.Method != http.MethodGet { 125 | return 126 | } 127 | ctx := req.Context() 128 | config, err := setting.LoadSettings(httpadapter.PluginConfigFromContext(ctx)) 129 | 130 | query := req.URL.Query() 131 | var ( 132 | webPropertyId = query.Get("webPropertyId") 133 | ) 134 | res, err := ds.analytics.GetDimensions(ctx, config, webPropertyId) 135 | writeResult(rw, "dimensions", res, err) 136 | } 137 | 138 | func (ds *GoogleAnalyticsDataSource) handleResourceMetrics(rw http.ResponseWriter, req *http.Request) { 139 | if req.Method != http.MethodGet { 140 | return 141 | } 142 | ctx := req.Context() 143 | config, err := setting.LoadSettings(httpadapter.PluginConfigFromContext(ctx)) 144 | 145 | query := req.URL.Query() 146 | var ( 147 | webPropertyId = query.Get("webPropertyId") 148 | ) 149 | res, err := ds.analytics.GetMetrics(ctx, config, webPropertyId) 150 | writeResult(rw, "metrics", res, err) 151 | } 152 | 153 | func (ds *GoogleAnalyticsDataSource) handleResourceRealtimeDimensions(rw http.ResponseWriter, req *http.Request) { 154 | if req.Method != http.MethodGet { 155 | return 156 | } 157 | ctx := req.Context() 158 | config, err := setting.LoadSettings(httpadapter.PluginConfigFromContext(ctx)) 159 | 160 | query := req.URL.Query() 161 | var ( 162 | webPropertyId = query.Get("webPropertyId") 163 | ) 164 | res, err := ds.analytics.GetRealtimeDimensions(ctx, config, webPropertyId) 165 | writeResult(rw, "dimensions", res, err) 166 | } 167 | 168 | func (ds *GoogleAnalyticsDataSource) handleResourceRealtimeMetrics(rw http.ResponseWriter, req *http.Request) { 169 | if req.Method != http.MethodGet { 170 | return 171 | } 172 | ctx := req.Context() 173 | config, err := setting.LoadSettings(httpadapter.PluginConfigFromContext(ctx)) 174 | 175 | query := req.URL.Query() 176 | var ( 177 | webPropertyId = query.Get("webPropertyId") 178 | ) 179 | res, err := ds.analytics.GetRealTimeMetrics(ctx, config, webPropertyId) 180 | writeResult(rw, "metrics", res, err) 181 | } 182 | 183 | func (ds *GoogleAnalyticsDataSource) handleResourceProfileTimezone(rw http.ResponseWriter, req *http.Request) { 184 | if req.Method != http.MethodGet { 185 | return 186 | } 187 | 188 | ctx := req.Context() 189 | config, err := setting.LoadSettings(httpadapter.PluginConfigFromContext(ctx)) 190 | if err != nil { 191 | writeResult(rw, "?", nil, err) 192 | return 193 | } 194 | query := req.URL.Query() 195 | var ( 196 | accountId = query.Get("accountId") 197 | webPropertyId = query.Get("webPropertyId") 198 | profileId = query.Get("profileId") 199 | ) 200 | res, err := ds.analytics.GetTimezone(ctx, config, accountId, webPropertyId, profileId) 201 | writeResult(rw, "timezone", res, err) 202 | } 203 | 204 | func (ds *GoogleAnalyticsDataSource) handleResourcePropertyServiceLevel(rw http.ResponseWriter, req *http.Request) { 205 | if req.Method != http.MethodGet { 206 | return 207 | } 208 | 209 | ctx := req.Context() 210 | config, err := setting.LoadSettings(httpadapter.PluginConfigFromContext(ctx)) 211 | if err != nil { 212 | writeResult(rw, "?", nil, err) 213 | return 214 | } 215 | query := req.URL.Query() 216 | var ( 217 | accountId = query.Get("accountId") 218 | webPropertyId = query.Get("webPropertyId") 219 | ) 220 | res, err := ds.analytics.GetServiceLevel(ctx, config, accountId, webPropertyId) 221 | writeResult(rw, "serviceLevel", res, err) 222 | } 223 | 224 | func (ds *GoogleAnalyticsDataSource) handleResourceAccountSummaries(rw http.ResponseWriter, req *http.Request) { 225 | if req.Method != http.MethodGet { 226 | return 227 | } 228 | 229 | ctx := req.Context() 230 | config, err := setting.LoadSettings(httpadapter.PluginConfigFromContext(ctx)) 231 | if err != nil { 232 | writeResult(rw, "?", nil, err) 233 | return 234 | } 235 | 236 | res, err := ds.analytics.GetAccountSummaries(ctx, config) 237 | writeResult(rw, "accountSummaries", res, err) 238 | } 239 | -------------------------------------------------------------------------------- /pkg/gav3/analytics.go: -------------------------------------------------------------------------------- 1 | package gav3 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | "time" 7 | 8 | "github.com/blackcowmoo/grafana-google-analytics-dataSource/pkg/model" 9 | "github.com/blackcowmoo/grafana-google-analytics-dataSource/pkg/setting" 10 | 11 | "github.com/grafana/grafana-plugin-sdk-go/backend" 12 | "github.com/grafana/grafana-plugin-sdk-go/backend/log" 13 | "github.com/grafana/grafana-plugin-sdk-go/data" 14 | "github.com/patrickmn/go-cache" 15 | ) 16 | 17 | // GoogleAnalyticsv3DataSource handler for google sheets 18 | type GoogleAnalytics struct { 19 | Cache *cache.Cache 20 | } 21 | 22 | func (ga *GoogleAnalytics) Query(ctx context.Context, config *setting.DatasourceSecretSettings, query backend.DataQuery) (*data.Frames, error) { 23 | client, err := NewGoogleClient(ctx, config.JWT) 24 | if err != nil { 25 | log.DefaultLogger.Error("Query: Fail NewGoogleClient", "error", err.Error()) 26 | return nil, err 27 | } 28 | queryModel, err := GetQueryModel(query) 29 | if err != nil { 30 | log.DefaultLogger.Error("Failed to read query: %w", "error", err) 31 | return nil, fmt.Errorf("failed to read query: %w", err) 32 | } 33 | 34 | if len(queryModel.AccountID) < 1 { 35 | log.DefaultLogger.Error("Query", "error", "Required AccountID") 36 | return nil, fmt.Errorf("Required AccountID") 37 | } 38 | 39 | if len(queryModel.WebPropertyID) < 1 { 40 | log.DefaultLogger.Error("Query", "error", "Required WebPropertyID") 41 | return nil, fmt.Errorf("Required WebPropertyID") 42 | } 43 | 44 | if len(queryModel.ProfileID) < 1 { 45 | log.DefaultLogger.Error("Query", "error", "Required ProfileID") 46 | return nil, fmt.Errorf("Required ProfileID") 47 | } 48 | 49 | report, err := client.getReport(*queryModel) 50 | if err != nil { 51 | log.DefaultLogger.Error("Query", "error", err) 52 | return nil, err 53 | } 54 | 55 | return transformReportsResponseToDataFrames(report, queryModel.RefID, queryModel.Timezone) 56 | } 57 | 58 | func (ga *GoogleAnalytics) GetTimezone(ctx context.Context, config *setting.DatasourceSecretSettings, accountId string, webPropertyId string, profileId string) (string, error) { 59 | client, err := NewGoogleClient(ctx, config.JWT) 60 | if err != nil { 61 | return "", fmt.Errorf("failed to create Google API client: %w", err) 62 | } 63 | 64 | cacheKey := fmt.Sprintf("analytics:account:%s:webproperty:%s:profile:%s:timezone", accountId, webPropertyId, profileId) 65 | if item, _, found := ga.Cache.GetWithExpiration(cacheKey); found { 66 | return item.(string), nil 67 | } 68 | 69 | profile, err := client.getProfile(accountId, webPropertyId, profileId) 70 | if err != nil { 71 | return "", err 72 | } 73 | 74 | timezone := profile.Timezone 75 | 76 | ga.Cache.Set(cacheKey, timezone, 60*time.Second) 77 | return timezone, nil 78 | } 79 | 80 | func (ga *GoogleAnalytics) CheckHealth(ctx context.Context, config *setting.DatasourceSecretSettings) (*backend.CheckHealthResult, error) { 81 | var status = backend.HealthStatusOk 82 | var message = "Success" 83 | 84 | client, err := NewGoogleClient(ctx, config.JWT) 85 | if err != nil { 86 | log.DefaultLogger.Error("CheckHealth: Fail NewGoogleClient", "error", err.Error()) 87 | return &backend.CheckHealthResult{ 88 | Status: backend.HealthStatusError, 89 | Message: "CheckHealth: Fail NewGoogleClient" + err.Error(), 90 | }, nil 91 | } 92 | 93 | accountSummaries, err := ga.GetAccountSummaries(ctx, config) 94 | if err != nil { 95 | log.DefaultLogger.Error("CheckHealth: Fail getAllProfilesList", "error", err.Error()) 96 | return &backend.CheckHealthResult{ 97 | Status: backend.HealthStatusError, 98 | Message: "CheckHealth: Fail getProfileList" + err.Error(), 99 | }, nil 100 | } 101 | if len(accountSummaries) == 0 { 102 | log.DefaultLogger.Error("CheckHealth: Not Exist Valid Profile") 103 | return &backend.CheckHealthResult{ 104 | Status: backend.HealthStatusError, 105 | Message: "CheckHealth: Not Exist Valid Profile", 106 | }, nil 107 | } 108 | 109 | testData := model.QueryModel{AccountID: accountSummaries[0].Account, WebPropertyID: accountSummaries[0].PropertySummaries[0].Property, ProfileID: accountSummaries[0].PropertySummaries[0].ProfileSummaries[0].Profile, StartDate: "yesterday", EndDate: "today", RefID: "a", Metrics: []string{"ga:sessions"}, TimeDimension: "ga:date", Dimensions: []string{"ga:date"}, PageSize: GaReportMaxResult, PageToken: "", UseNextPage: false, Timezone: "UTC", FiltersExpression: "", Offset: GaDefaultIdx} 110 | res, err := client.getReport(testData) 111 | 112 | if err != nil { 113 | log.DefaultLogger.Error("CheckHealth: GET request to analyticsreporting/v4 returned error", "error", err.Error()) 114 | return &backend.CheckHealthResult{ 115 | Status: backend.HealthStatusError, 116 | Message: "CheckHealth: Test Request Fail" + err.Error(), 117 | }, nil 118 | } 119 | 120 | if res != nil { 121 | log.DefaultLogger.Debug("HTTPStatusCode", "status", res.HTTPStatusCode) 122 | log.DefaultLogger.Debug("res", res) 123 | } 124 | 125 | printResponse(res) 126 | 127 | return &backend.CheckHealthResult{ 128 | Status: status, 129 | Message: message, 130 | }, nil 131 | } 132 | 133 | // remove no profile account 134 | func (ga *GoogleAnalytics) GetAccountSummaries(ctx context.Context, config *setting.DatasourceSecretSettings) ([]*model.AccountSummary, error) { 135 | client, err := NewGoogleClient(ctx, config.JWT) 136 | if err != nil { 137 | return nil, fmt.Errorf("failed to create Google API client: %w", err) 138 | } 139 | 140 | cacheKey := fmt.Sprintf("analytics:accountsummaries:%s", config.JWT) 141 | if item, _, found := ga.Cache.GetWithExpiration(cacheKey); found { 142 | return item.([]*model.AccountSummary), nil 143 | } 144 | 145 | rawAccountSummaries, err := client.getAccountSummaries(GaDefaultIdx) 146 | if err != nil { 147 | return nil, err 148 | } 149 | log.DefaultLogger.Debug("UA GetAccountSummaries raw accounts", "debug", rawAccountSummaries) 150 | 151 | var accounts []*model.AccountSummary 152 | for _, rawAccountSummary := range rawAccountSummaries { 153 | if len(rawAccountSummary.WebProperties) == 0 { 154 | continue 155 | } 156 | var account = &model.AccountSummary{ 157 | Account: rawAccountSummary.Id, 158 | DisplayName: rawAccountSummary.Name, 159 | } 160 | var propertySummaries = make([]*model.PropertySummary, 0) 161 | for _, rawPropertySummary := range rawAccountSummary.WebProperties { 162 | if len(rawPropertySummary.Profiles) == 0 { 163 | continue 164 | } 165 | var propertySummary = &model.PropertySummary{ 166 | Property: rawPropertySummary.Id, 167 | DisplayName: rawPropertySummary.Name, 168 | Parent: rawAccountSummary.Id, 169 | } 170 | propertySummaries = append(propertySummaries, propertySummary) 171 | 172 | var profileSummaries = make([]*model.ProfileSummary, 0) 173 | 174 | for _, rawProfileSummaries := range rawPropertySummary.Profiles { 175 | var profileSummary = &model.ProfileSummary{ 176 | Profile: rawProfileSummaries.Id, 177 | DisplayName: rawProfileSummaries.Name, 178 | Parent: rawPropertySummary.Id, 179 | Type: rawProfileSummaries.Type, 180 | } 181 | profileSummaries = append(profileSummaries, profileSummary) 182 | } 183 | propertySummary.ProfileSummaries = profileSummaries 184 | } 185 | if len(propertySummaries) > 0 { 186 | account.PropertySummaries = propertySummaries 187 | accounts = append(accounts, account) 188 | } 189 | } 190 | log.DefaultLogger.Debug("UA GetAccountSummaries accounts", "debug", accounts) 191 | ga.Cache.Set(cacheKey, accounts, 60*time.Second) 192 | return accounts, nil 193 | } 194 | 195 | func (ga *GoogleAnalytics) GetServiceLevel(ctx context.Context, config *setting.DatasourceSecretSettings, accountId string, webPropertyId string) (string, error) { 196 | return "", nil 197 | } 198 | 199 | func (ga *GoogleAnalytics) GetRealtimeDimensions(ctx context.Context, config *setting.DatasourceSecretSettings, propertyId string) ([]model.MetadataItem, error) { 200 | cacheKey := "ga:metadata:" + propertyId + ":realtime-dimensions" 201 | if dimensions, _, found := ga.Cache.GetWithExpiration(cacheKey); found { 202 | return dimensions.([]model.MetadataItem), nil 203 | } 204 | 205 | return nil, nil 206 | } 207 | 208 | func (ga *GoogleAnalytics) GetRealTimeMetrics(ctx context.Context, config *setting.DatasourceSecretSettings, propertyId string) ([]model.MetadataItem, error) { 209 | cacheKey := "ga:metadata:" + propertyId + ":realtime-metrics" 210 | if metrics, _, found := ga.Cache.GetWithExpiration(cacheKey); found { 211 | return metrics.([]model.MetadataItem), nil 212 | } 213 | 214 | return nil, nil 215 | } 216 | -------------------------------------------------------------------------------- /.config/webpack/webpack.config.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * ⚠️⚠️⚠️ THIS FILE WAS SCAFFOLDED BY `@grafana/create-plugin`. DO NOT EDIT THIS FILE DIRECTLY. ⚠️⚠️⚠️ 3 | * 4 | * In order to extend the configuration follow the steps in 5 | * https://grafana.com/developers/plugin-tools/create-a-plugin/extend-a-plugin/extend-configurations#extend-the-webpack-config 6 | */ 7 | 8 | import CopyWebpackPlugin from 'copy-webpack-plugin'; 9 | import ESLintPlugin from 'eslint-webpack-plugin'; 10 | import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin'; 11 | import LiveReloadPlugin from 'webpack-livereload-plugin'; 12 | import path from 'path'; 13 | import ReplaceInFileWebpackPlugin from 'replace-in-file-webpack-plugin'; 14 | import TerserPlugin from 'terser-webpack-plugin'; 15 | import { type Configuration, BannerPlugin } from 'webpack'; 16 | 17 | import { getPackageJson, getPluginJson, hasReadme, getEntries, isWSL, getCPConfigVersion } from './utils'; 18 | import { SOURCE_DIR, DIST_DIR } from './constants'; 19 | 20 | const pluginJson = getPluginJson(); 21 | const cpVersion = getCPConfigVersion(); 22 | 23 | const config = async (env): Promise => { 24 | const baseConfig: Configuration = { 25 | cache: { 26 | type: 'filesystem', 27 | buildDependencies: { 28 | config: [__filename], 29 | }, 30 | }, 31 | 32 | context: path.join(process.cwd(), SOURCE_DIR), 33 | 34 | devtool: env.production ? 'source-map' : 'eval-source-map', 35 | 36 | entry: await getEntries(), 37 | 38 | externals: [ 39 | // Required for dynamic publicPath resolution 40 | { 'amd-module': 'module' }, 41 | 'lodash', 42 | 'jquery', 43 | 'moment', 44 | 'slate', 45 | 'emotion', 46 | '@emotion/react', 47 | '@emotion/css', 48 | 'prismjs', 49 | 'slate-plain-serializer', 50 | '@grafana/slate-react', 51 | 'react', 52 | 'react-dom', 53 | 'react-redux', 54 | 'redux', 55 | 'rxjs', 56 | 'react-router', 57 | 'react-router-dom', 58 | 'd3', 59 | 'angular', 60 | '@grafana/ui', 61 | '@grafana/runtime', 62 | '@grafana/data', 63 | 64 | // Mark legacy SDK imports as external if their name starts with the "grafana/" prefix 65 | ({ request }, callback) => { 66 | const prefix = 'grafana/'; 67 | const hasPrefix = (request) => request.indexOf(prefix) === 0; 68 | const stripPrefix = (request) => request.substr(prefix.length); 69 | 70 | if (hasPrefix(request)) { 71 | return callback(undefined, stripPrefix(request)); 72 | } 73 | 74 | callback(); 75 | }, 76 | ], 77 | 78 | // Support WebAssembly according to latest spec - makes WebAssembly module async 79 | experiments: { 80 | asyncWebAssembly: true, 81 | }, 82 | 83 | mode: env.production ? 'production' : 'development', 84 | 85 | module: { 86 | rules: [ 87 | { 88 | exclude: /(node_modules)/, 89 | test: /\.[tj]sx?$/, 90 | use: { 91 | loader: 'swc-loader', 92 | options: { 93 | jsc: { 94 | baseUrl: path.resolve(process.cwd(), SOURCE_DIR), 95 | target: 'es2015', 96 | loose: false, 97 | parser: { 98 | syntax: 'typescript', 99 | tsx: true, 100 | decorators: false, 101 | dynamicImport: true, 102 | }, 103 | }, 104 | }, 105 | }, 106 | }, 107 | { 108 | test: /src\/(?:.*\/)?module\.tsx?$/, 109 | use: [ 110 | { 111 | loader: 'imports-loader', 112 | options: { 113 | imports: `side-effects ${path.join(__dirname, 'publicPath.ts')}`, 114 | }, 115 | }, 116 | ], 117 | }, 118 | { 119 | test: /\.css$/, 120 | use: ['style-loader', 'css-loader'], 121 | }, 122 | { 123 | test: /\.s[ac]ss$/, 124 | use: ['style-loader', 'css-loader', 'sass-loader'], 125 | }, 126 | { 127 | test: /\.(png|jpe?g|gif|svg)$/, 128 | type: 'asset/resource', 129 | generator: { 130 | filename: Boolean(env.production) ? '[hash][ext]' : '[file]', 131 | }, 132 | }, 133 | { 134 | test: /\.(woff|woff2|eot|ttf|otf)(\?v=\d+\.\d+\.\d+)?$/, 135 | type: 'asset/resource', 136 | generator: { 137 | filename: Boolean(env.production) ? '[hash][ext]' : '[file]', 138 | }, 139 | }, 140 | ], 141 | }, 142 | 143 | optimization: { 144 | minimize: Boolean(env.production), 145 | minimizer: [ 146 | new TerserPlugin({ 147 | terserOptions: { 148 | format: { 149 | comments: (_, { type, value }) => type === 'comment2' && value.trim().startsWith('[create-plugin]'), 150 | }, 151 | }, 152 | }), 153 | ], 154 | }, 155 | 156 | output: { 157 | clean: { 158 | keep: new RegExp(`(.*?_(amd64|arm(64)?)(.exe)?|go_plugin_build_manifest)`), 159 | }, 160 | filename: '[name].js', 161 | library: { 162 | type: 'amd', 163 | }, 164 | path: path.resolve(process.cwd(), DIST_DIR), 165 | publicPath: `public/plugins/${pluginJson.id}/`, 166 | uniqueName: pluginJson.id, 167 | }, 168 | 169 | plugins: [ 170 | // Insert create plugin version information into the bundle 171 | new BannerPlugin({ 172 | banner: "/* [create-plugin] version: " + cpVersion + " */", 173 | raw: true, 174 | entryOnly: true, 175 | }), 176 | new CopyWebpackPlugin({ 177 | patterns: [ 178 | // If src/README.md exists use it; otherwise the root README 179 | // To `compiler.options.output` 180 | { from: hasReadme() ? 'README.md' : '../README.md', to: '.', force: true }, 181 | { from: 'plugin.json', to: '.' }, 182 | { from: '../LICENSE', to: '.' }, 183 | { from: '../CHANGELOG.md', to: '.', force: true }, 184 | { from: '**/*.json', to: '.' }, // TODO 185 | { from: '**/*.svg', to: '.', noErrorOnMissing: true }, // Optional 186 | { from: '**/*.png', to: '.', noErrorOnMissing: true }, // Optional 187 | { from: '**/*.html', to: '.', noErrorOnMissing: true }, // Optional 188 | { from: 'img/**/*', to: '.', noErrorOnMissing: true }, // Optional 189 | { from: 'libs/**/*', to: '.', noErrorOnMissing: true }, // Optional 190 | { from: 'static/**/*', to: '.', noErrorOnMissing: true }, // Optional 191 | { from: '**/query_help.md', to: '.', noErrorOnMissing: true }, // Optional 192 | ], 193 | }), 194 | // Replace certain template-variables in the README and plugin.json 195 | new ReplaceInFileWebpackPlugin([ 196 | { 197 | dir: DIST_DIR, 198 | files: ['plugin.json', 'README.md'], 199 | rules: [ 200 | { 201 | search: /\%VERSION\%/g, 202 | replace: getPackageJson().version, 203 | }, 204 | { 205 | search: /\%TODAY\%/g, 206 | replace: new Date().toISOString().substring(0, 10), 207 | }, 208 | { 209 | search: /\%PLUGIN_ID\%/g, 210 | replace: pluginJson.id, 211 | }, 212 | ], 213 | }, 214 | ]), 215 | ...(env.development ? [ 216 | new LiveReloadPlugin(), 217 | new ForkTsCheckerWebpackPlugin({ 218 | async: Boolean(env.development), 219 | issue: { 220 | include: [{ file: '**/*.{ts,tsx}' }], 221 | }, 222 | typescript: { configFile: path.join(process.cwd(), 'tsconfig.json') }, 223 | }), 224 | new ESLintPlugin({ 225 | extensions: ['.ts', '.tsx'], 226 | lintDirtyModulesOnly: Boolean(env.development), // don't lint on start, only lint changed files 227 | }), 228 | ] : []), 229 | ], 230 | 231 | resolve: { 232 | extensions: ['.js', '.jsx', '.ts', '.tsx'], 233 | // handle resolving "rootDir" paths 234 | modules: [path.resolve(process.cwd(), 'src'), 'node_modules'], 235 | unsafeCache: true, 236 | }, 237 | }; 238 | 239 | if (isWSL()) { 240 | baseConfig.watchOptions = { 241 | poll: 3000, 242 | ignored: /node_modules/, 243 | }; 244 | } 245 | 246 | return baseConfig; 247 | }; 248 | 249 | export default config; 250 | -------------------------------------------------------------------------------- /src/Filter.tsx: -------------------------------------------------------------------------------- 1 | // cursor ai로 작성됨 2 | import { SelectableValue } from '@grafana/data'; 3 | import { Button, FieldSet, HorizontalGroup, Input, RadioButtonGroup, Select, VerticalGroup } from '@grafana/ui'; 4 | import React from 'react'; 5 | import { GADimensionFilterType, GAFilter, GAFilterExpression, GAFilterExpressionList, GAInListFilter, GAStringFilter, GAStringFilterMatchType } from 'types'; 6 | 7 | interface Props { 8 | expression: GAFilterExpression; 9 | onChange: (expression: GAFilterExpression) => void; 10 | onDelete?: () => void; // New prop added 11 | selectedDimensions: Array>; // New prop added 12 | } 13 | 14 | export const GAFilterExpressionComponent: React.FC = ({ expression = {}, onChange, onDelete, selectedDimensions }) => { 15 | const expressionTypes: Array> = [ 16 | { label: 'no filter', value: 'none' }, 17 | { label: 'AND', value: 'andGroup' }, 18 | { label: 'OR', value: 'orGroup' }, 19 | { label: 'NOT', value: 'notExpression' }, 20 | { label: 'FILTER', value: 'filter' }, 21 | ]; 22 | 23 | const handleExpressionTypeChange = (option: SelectableValue) => { 24 | let newExpression: GAFilterExpression; 25 | switch (option.value) { 26 | case 'none': 27 | newExpression = {}; 28 | break; 29 | case 'andGroup': 30 | case 'orGroup': 31 | newExpression = { [option.value]: { expressions: [] } }; 32 | break; 33 | case 'notExpression': 34 | newExpression = { notExpression: {} }; 35 | break; 36 | case 'filter': 37 | newExpression = { 38 | filter: { 39 | fieldName: '', 40 | filterType: GADimensionFilterType.STRING, 41 | stringFilter: { matchType: GAStringFilterMatchType.EXACT, value: '', caseSensitive: false } 42 | } 43 | }; 44 | break; 45 | default: 46 | return; 47 | } 48 | onChange(newExpression); 49 | }; 50 | 51 | const renderExpressionContent = () => { 52 | if (Object.keys(expression).length === 0) { 53 | return
No filter set
; 54 | } else if (expression.andGroup) { 55 | return renderExpressionList(expression.andGroup, 'andGroup'); 56 | } else if (expression.orGroup) { 57 | return renderExpressionList(expression.orGroup, 'orGroup'); 58 | } else if (expression.notExpression) { 59 | return renderNotExpression(); 60 | } else if (expression.filter) { 61 | return renderFilter(); 62 | } 63 | return null; 64 | }; 65 | 66 | const renderExpressionList = (list: GAFilterExpressionList, type: 'andGroup' | 'orGroup') => { 67 | return ( 68 | 69 | {list.expressions.map((expr, index) => ( 70 | 71 | { 75 | const newList = { ...list, expressions: [...list.expressions] }; 76 | newList.expressions[index] = newExpr; 77 | onChange({ [type]: newList }); 78 | }} 79 | onDelete={() => { 80 | const newList = { ...list, expressions: [...list.expressions] }; 81 | newList.expressions.splice(index, 1); 82 | onChange({ [type]: newList }); 83 | }} 84 | /> 85 | 86 | ))} 87 | 88 | 96 | 97 | 98 | ); 99 | }; 100 | 101 | const renderNotExpression = () => { 102 | return ( 103 | onChange({ notExpression: newExpr })} 107 | onDelete={() => onChange({})} 108 | /> 109 | ); 110 | }; 111 | 112 | const renderFilter = () => { 113 | const filter = expression.filter || { fieldName: '', filterType: GADimensionFilterType.STRING }; 114 | 115 | const filterTypes: Array> = [ 116 | { label: 'String', value: 'STRING' }, 117 | { label: 'List', value: 'IN_LIST' }, 118 | ]; 119 | 120 | const handleFilterTypeChange = (option: SelectableValue) => { 121 | let newFilter: GAFilter = { 122 | ...filter, 123 | filterType: option.value as GADimensionFilterType, 124 | }; 125 | 126 | // Set initial values based on filter type 127 | switch (option.value) { 128 | case GADimensionFilterType.STRING: 129 | newFilter.stringFilter = { matchType: GAStringFilterMatchType.EXACT, value: '', caseSensitive: false }; 130 | newFilter.inListFilter = undefined; 131 | break; 132 | case GADimensionFilterType.IN_LIST: 133 | newFilter.inListFilter = { values: [], caseSensitive: false }; 134 | newFilter.stringFilter = undefined; 135 | break; 136 | } 137 | 138 | onChange({ filter: newFilter }); 139 | }; 140 | 141 | const renderFilterContent = () => { 142 | switch (filter.filterType) { 143 | case GADimensionFilterType.STRING: 144 | return renderStringFilter(filter.stringFilter!); 145 | case GADimensionFilterType.IN_LIST: 146 | return renderInListFilter(filter.inListFilter!); 147 | default: 148 | return null; 149 | } 150 | }; 151 | 152 | return ( 153 | 154 | 165 | {renderFilterContent()} 166 | 167 | ); 168 | }; 169 | 170 | const renderStringFilter = (stringFilter: GAStringFilter) => { 171 | const matchTypes = Object.values(GAStringFilterMatchType).map(value => ({ label: value, value })); 172 | 173 | return ( 174 | 175 | onChange({ filter: { ...expression.filter!, stringFilter: { ...stringFilter, value: e.currentTarget.value } } })} 183 | placeholder="Value" 184 | /> 185 | onChange({ filter: { ...expression.filter!, stringFilter: { ...stringFilter, caseSensitive: value } } })} 192 | /> 193 | 194 | ); 195 | }; 196 | 197 | const renderInListFilter = (inListFilter: GAInListFilter = { values: [], caseSensitive: false }) => { 198 | return ( 199 | 200 | onChange({ filter: { ...expression.filter!, inListFilter: { ...inListFilter, values: e.currentTarget.value.split(',').map(v => v.trim()) } } })} 203 | placeholder="Values (comma-separated)" 204 | /> 205 | onChange({ filter: { ...expression.filter!, inListFilter: { ...inListFilter, caseSensitive: value } } })} 212 | /> 213 | 214 | ); 215 | }; 216 | 217 | return ( 218 |
219 | 220 | this.onFiltersExpressionChange(e.currentTarget.value)} 259 | placeholder="ga:pagePath==/path/to/page" 260 | /> 261 | 262 | 263 | 264 | ); 265 | } 266 | } 267 | -------------------------------------------------------------------------------- /pkg/gav3/grafana.go: -------------------------------------------------------------------------------- 1 | package gav3 2 | 3 | import ( 4 | "fmt" 5 | "github.com/blackcowmoo/grafana-google-analytics-dataSource/pkg/model" 6 | "github.com/blackcowmoo/grafana-google-analytics-dataSource/pkg/util" 7 | "github.com/grafana/grafana-plugin-sdk-go/backend/log" 8 | "github.com/grafana/grafana-plugin-sdk-go/data" 9 | "github.com/jinzhu/copier" 10 | reporting "google.golang.org/api/analyticsreporting/v4" 11 | "sort" 12 | "strconv" 13 | "strings" 14 | "time" 15 | ) 16 | 17 | func transformReportToDataFrameByDimensions(columns []*model.ColumnDefinition, rows []*reporting.ReportRow, refId string, dimensions string) (*data.Frame, error) { 18 | warnings := []string{} 19 | meta := map[string]interface{}{} 20 | 21 | converters := make([]data.FieldConverter, len(columns)) 22 | for i, column := range columns { 23 | fc, ok := converterMap[column.GetType()] 24 | if !ok { 25 | return nil, fmt.Errorf("unknown column type: %s", column.GetType()) 26 | } 27 | converters[i] = fc 28 | } 29 | 30 | inputConverter, err := data.NewFrameInputConverter(converters, len(rows)) 31 | if err != nil { 32 | return nil, err 33 | } 34 | 35 | frame := inputConverter.Frame 36 | frame.RefID = refId 37 | frame.Name = refId // TODO: should set the name from metadata 38 | if len(dimensions) > 0 { 39 | frame.Name = dimensions 40 | } 41 | 42 | for i, column := range columns { 43 | field := frame.Fields[i] 44 | field.Name = column.Header 45 | displayName := dimensions 46 | if len(dimensions) > 0 { 47 | displayName = displayName + "|" 48 | } 49 | field.Config = &data.FieldConfig{ 50 | DisplayName: displayName + column.Header, 51 | // Unit: column.GetUnit(), 52 | } 53 | } 54 | i := 0 55 | for rowIndex, row := range rows { 56 | if dimensions == strings.Join(row.Dimensions, "|") { 57 | for _, metrics := range row.Metrics { 58 | // d := row.Dimensions[dateIndex] 59 | for valueIndex, value := range metrics.Values { 60 | err := inputConverter.Set(valueIndex, rowIndex, value) 61 | if err != nil { 62 | log.DefaultLogger.Error("frame convert", "error", err.Error()) 63 | warnings = append(warnings, err.Error()) 64 | continue 65 | } 66 | i++ 67 | } 68 | } 69 | } 70 | } 71 | 72 | meta["warnings"] = warnings 73 | frame.Meta = &data.FrameMeta{Custom: meta} 74 | return frame, nil 75 | } 76 | 77 | // <--------- primary secondary ---------> 78 | var timeDimensions []string = []string{"ga:dateHourMinute", "ga:dateHour", "ga:date"} 79 | 80 | func transformReportToDataFrames(report *reporting.Report, refId string, timezone string) ([]*data.Frame, error) { 81 | timeDimension := reporting.MetricHeaderEntry{ 82 | Name: report.ColumnHeader.Dimensions[0], 83 | Type: "TIME", 84 | } 85 | report.ColumnHeader.MetricHeader.MetricHeaderEntries = append([]*reporting.MetricHeaderEntry{ 86 | &timeDimension, 87 | }, report.ColumnHeader.MetricHeader.MetricHeaderEntries...) 88 | 89 | timeAddFunction, timeSubFunction := getTimeFunction(timeDimension.Name) 90 | 91 | tz, err := time.LoadLocation(timezone) 92 | if err != nil { 93 | log.DefaultLogger.Error("Load local timezone error", "error", err.Error()) 94 | } 95 | 96 | dimensions := map[string]struct{}{} 97 | var parsedReportMap = make(map[string]map[int64]*reporting.ReportRow) 98 | 99 | for _, row := range report.Data.Rows { 100 | parsedRow, parsedTime := parseRow(row, tz) 101 | dimension := strings.Join(parsedRow.Dimensions, "|") 102 | if _, ok := dimensions[dimension]; !ok { 103 | dimensions[dimension] = struct{}{} 104 | } 105 | if inner, ok := parsedReportMap[dimension]; !ok { 106 | inner = make(map[int64]*reporting.ReportRow) 107 | parsedReportMap[dimension] = inner 108 | } 109 | parsedReportMap[dimension][parsedTime.Unix()] = parsedRow 110 | 111 | beforeTime := timeSubFunction(*parsedTime) 112 | afterTime := timeAddFunction(*parsedTime) 113 | if _, ok := parsedReportMap[dimension][beforeTime.Unix()]; !ok { 114 | copyRow := copyRowAndInit(row) 115 | copyRow.Metrics[0].Values[0] = beforeTime.Format(time.RFC3339) 116 | parsedReportMap[dimension][beforeTime.Unix()] = copyRow 117 | } 118 | if _, ok := parsedReportMap[dimension][afterTime.Unix()]; !ok { 119 | copyRow := copyRowAndInit(row) 120 | copyRow.Metrics[0].Values[0] = afterTime.Format(time.RFC3339) 121 | parsedReportMap[dimension][afterTime.Unix()] = copyRow 122 | } 123 | } 124 | 125 | var dimensionKeys = make([]string, len(dimensions)) 126 | i := 0 127 | for value := range dimensions { 128 | dimensionKeys[i] = value 129 | i++ 130 | } 131 | 132 | var frames = make([]*data.Frame, 0, len(dimensionKeys)) 133 | columns := getColumnDefinitions(report.ColumnHeader) 134 | 135 | for _, dimension := range dimensionKeys { 136 | keys := make([]int64, len(parsedReportMap[dimension])) 137 | i := 0 138 | for k := range parsedReportMap[dimension] { 139 | keys[i] = k 140 | i++ 141 | } 142 | 143 | sort.Slice(keys, func(i, j int) bool { return keys[i] < keys[j] }) 144 | var parsedRows = make([]*reporting.ReportRow, len(parsedReportMap[dimension])) 145 | i = 0 146 | for _, t := range keys { 147 | parsedRows[i] = parsedReportMap[dimension][t] 148 | i++ 149 | } 150 | 151 | frame, err := transformReportToDataFrameByDimensions(columns, parsedRows, refId, dimension) 152 | if err != nil { 153 | log.DefaultLogger.Error("transformReportToDataFrameByDimensions", "error", err.Error()) 154 | return nil, err 155 | } 156 | 157 | frames = append(frames, frame) 158 | } 159 | 160 | return frames, nil 161 | } 162 | 163 | func transformReportsResponseToDataFrames(reportsResponse *reporting.GetReportsResponse, refId string, timezone string) (*data.Frames, error) { 164 | var frames = make(data.Frames, 0) 165 | for _, report := range reportsResponse.Reports { 166 | frame, err := transformReportToDataFrames(report, refId, timezone) 167 | if err != nil { 168 | return nil, err 169 | } 170 | 171 | frames = append(frames, frame...) 172 | } 173 | 174 | return &frames, nil 175 | } 176 | 177 | // timeConverter handles sheets TIME column types. 178 | var timeConverter = data.FieldConverter{ 179 | OutputFieldType: data.FieldTypeNullableTime, 180 | Converter: func(i interface{}) (interface{}, error) { 181 | strTime, ok := i.(string) 182 | if !ok { 183 | return nil, fmt.Errorf("expected type string, but got %T", i) 184 | } 185 | time, err := time.Parse(time.RFC3339, strTime) 186 | if err != nil { 187 | log.DefaultLogger.Error("timeConverter failed", "error", err) 188 | return nil, err 189 | } 190 | return &time, nil 191 | }, 192 | } 193 | 194 | // stringConverter handles sheets STRING column types. 195 | var stringConverter = data.FieldConverter{ 196 | OutputFieldType: data.FieldTypeNullableString, 197 | Converter: func(i interface{}) (interface{}, error) { 198 | value, ok := i.(string) 199 | if !ok { 200 | return nil, fmt.Errorf("expected type string, but got %T", i) 201 | } 202 | 203 | return &value, nil 204 | }, 205 | } 206 | 207 | // numberConverter handles sheets STRING column types. 208 | var numberConverter = data.FieldConverter{ 209 | OutputFieldType: data.FieldTypeNullableFloat64, 210 | Converter: func(i interface{}) (interface{}, error) { 211 | value, ok := i.(string) 212 | if !ok { 213 | return nil, fmt.Errorf("expected type string, but got %T", i) 214 | } 215 | 216 | num, err := strconv.ParseFloat(value, 64) 217 | if err != nil { 218 | return nil, fmt.Errorf("expected type string, but got %T", value) 219 | } 220 | 221 | return &num, nil 222 | }, 223 | } 224 | 225 | // converterMap is a map sheets.ColumnType to fieldConverter and 226 | // is used to create a data.FrameInputConverter for a returned sheet. 227 | var converterMap = map[model.ColumnType]data.FieldConverter{ 228 | "TIME": timeConverter, 229 | "STRING": stringConverter, 230 | "NUMBER": numberConverter, 231 | } 232 | 233 | func getColumnDefinitions(header *reporting.ColumnHeader) []*model.ColumnDefinition { 234 | columns := []*model.ColumnDefinition{} 235 | headerRow := header.MetricHeader.MetricHeaderEntries 236 | 237 | for columnIndex, headerCell := range headerRow { 238 | name := strings.TrimSpace(headerCell.Name) 239 | columns = append(columns, model.NewColumnDefinition(name, columnIndex, headerCell.Type)) 240 | } 241 | 242 | return columns 243 | } 244 | 245 | func copyRow(row *reporting.ReportRow) *reporting.ReportRow { 246 | var copyRow reporting.ReportRow 247 | copier.CopyWithOption(©Row.Dimensions, row.Dimensions, copier.Option{DeepCopy: true}) 248 | copier.CopyWithOption(©Row.Metrics, row.Metrics, copier.Option{DeepCopy: true}) 249 | return ©Row 250 | } 251 | 252 | func copyRowAndInit(row *reporting.ReportRow) *reporting.ReportRow { 253 | copyRow := copyRow(row) 254 | copyRow.Metrics[0].Values = util.FillArray(make([]string, len(row.Metrics[0].Values)), "0") 255 | return copyRow 256 | } 257 | 258 | func parseRow(row *reporting.ReportRow, timezone *time.Location) (*reporting.ReportRow, *time.Time) { 259 | timeDimension := row.Dimensions[0] 260 | otherDimensions := row.Dimensions[1:] 261 | parsedTime, err := util.ParseAndTimezoneTime(timeDimension, timezone) 262 | if err != nil { 263 | log.DefaultLogger.Error("parsedTime err", "err", err.Error()) 264 | } 265 | strTime := parsedTime.Format(time.RFC3339) 266 | 267 | // row.Metrics[0].Values = append(row.Metrics[0].Values, strTime) 268 | row.Metrics[0].Values = append([]string{strTime}, row.Metrics[0].Values...) 269 | row.Dimensions = otherDimensions 270 | return row, parsedTime 271 | } 272 | 273 | func getTimeFunction(timeDimension string) (func(time.Time) time.Time, func(time.Time) time.Time) { 274 | var add, sub func(time.Time) time.Time 275 | switch timeDimension { 276 | case timeDimensions[0]: 277 | add = util.AddOneMinute 278 | sub = util.SubOneMinute 279 | break 280 | case timeDimensions[1]: 281 | add = util.AddOneHour 282 | sub = util.SubOneHour 283 | break 284 | case timeDimensions[2]: 285 | add = util.AddOneDay 286 | sub = util.SubOneDay 287 | break 288 | default: 289 | add = util.AddOneHour 290 | sub = util.SubOneHour 291 | } 292 | return add, sub 293 | } 294 | -------------------------------------------------------------------------------- /pkg/gav4/client.go: -------------------------------------------------------------------------------- 1 | package gav4 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | "time" 7 | 8 | "github.com/blackcowmoo/grafana-google-analytics-dataSource/pkg/model" 9 | "github.com/blackcowmoo/grafana-google-analytics-dataSource/pkg/util" 10 | "github.com/grafana/grafana-plugin-sdk-go/backend/log" 11 | "golang.org/x/oauth2/google" 12 | "google.golang.org/api/option" 13 | 14 | analyticsadmin "google.golang.org/api/analyticsadmin/v1beta" 15 | analyticsdata "google.golang.org/api/analyticsdata/v1beta" 16 | ) 17 | 18 | type GoogleClient struct { 19 | analyticsdata *analyticsdata.Service 20 | analyticsadmin *analyticsadmin.Service 21 | } 22 | 23 | func NewGoogleClient(ctx context.Context, jwt string) (*GoogleClient, error) { 24 | analyticsdataService, analyticsdataError := createAnalyticsdataService(ctx, jwt) 25 | if analyticsdataError != nil { 26 | return nil, analyticsdataError 27 | } 28 | 29 | analyticsadminService, analyticsadminError := createAnalyticsadminService(ctx, jwt) 30 | if analyticsadminError != nil { 31 | return nil, analyticsadminError 32 | } 33 | 34 | return &GoogleClient{analyticsdataService, analyticsadminService}, nil 35 | } 36 | 37 | func createAnalyticsdataService(ctx context.Context, jwt string) (*analyticsdata.Service, error) { 38 | jwtConfig, err := google.JWTConfigFromJSON([]byte(jwt), analyticsdata.AnalyticsReadonlyScope) 39 | if err != nil { 40 | return nil, fmt.Errorf("error parsing JWT file: %w", err) 41 | } 42 | 43 | client := jwtConfig.Client(ctx) 44 | return analyticsdata.NewService(ctx, option.WithHTTPClient(client)) 45 | } 46 | 47 | func createAnalyticsadminService(ctx context.Context, jwt string) (*analyticsadmin.Service, error) { 48 | jwtConfig, err := google.JWTConfigFromJSON([]byte(jwt), analyticsadmin.AnalyticsReadonlyScope) 49 | if err != nil { 50 | return nil, fmt.Errorf("error parsing JWT file: %w", err) 51 | } 52 | 53 | client := jwtConfig.Client(ctx) 54 | return analyticsadmin.NewService(ctx, option.WithHTTPClient(client)) 55 | } 56 | 57 | func (client *GoogleClient) GetWebProperty(webpropertyID string) (*analyticsadmin.GoogleAnalyticsAdminV1betaProperty, error) { 58 | webproperty, err := client.analyticsadmin.Properties.Get(webpropertyID).Do() 59 | if err != nil { 60 | log.DefaultLogger.Error("GetWebProperty fail", "error", err.Error()) 61 | return nil, err 62 | } 63 | 64 | return webproperty, nil 65 | } 66 | 67 | func (client *GoogleClient) getReport(query model.QueryModel) (*analyticsdata.RunReportResponse, error) { 68 | defer util.Elapsed("Get report data at GA API")() 69 | log.DefaultLogger.Debug("getReport", "queries", query) 70 | Metrics := []*analyticsdata.Metric{} 71 | Dimensions := []*analyticsdata.Dimension{} 72 | for _, metric := range query.Metrics { 73 | Metrics = append(Metrics, &analyticsdata.Metric{Name: metric}) 74 | } 75 | for _, dimension := range query.Dimensions { 76 | Dimensions = append(Dimensions, &analyticsdata.Dimension{Name: dimension}) 77 | } 78 | var offset int64 = 0 79 | req := analyticsdata.RunReportRequest{ 80 | DateRanges: []*analyticsdata.DateRange{ 81 | // Create the DateRange object. 82 | {StartDate: query.StartDate, EndDate: query.EndDate}, 83 | }, 84 | Metrics: Metrics, 85 | Dimensions: Dimensions, 86 | Offset: offset, 87 | KeepEmptyRows: true, 88 | Limit: GaReportMaxResult, 89 | } 90 | if len(query.Dimensions) > 0 { 91 | req.OrderBys = []*analyticsdata.OrderBy{ 92 | { 93 | Dimension: &analyticsdata.DimensionOrderBy{ 94 | DimensionName: query.Dimensions[0], 95 | }, 96 | }, 97 | } 98 | } 99 | if !(query.DimensionFilter.OrGroup == nil && query.DimensionFilter.AndGroup == nil && query.DimensionFilter.Filter == nil && query.DimensionFilter.NotExpression == nil) { 100 | req.DimensionFilter = &query.DimensionFilter 101 | } 102 | log.DefaultLogger.Debug("Doing GET request from analytics reporting", "req", req) 103 | // Call the BatchGet method and return the response. 104 | report, err := client.analyticsdata.Properties.RunReport(query.WebPropertyID, &req).Do() 105 | if err != nil { 106 | return nil, fmt.Errorf(err.Error()) 107 | } 108 | // TODO 페이지 네이션 109 | log.DefaultLogger.Debug("Do GET report", "report len", report.RowCount, "report", report) 110 | 111 | if report.RowCount > (query.Offset + GaReportMaxResult) { 112 | query.Offset = query.Offset + GaReportMaxResult 113 | newReport, err := client.getReport(query) 114 | if err != nil { 115 | return nil, fmt.Errorf(err.Error()) 116 | } 117 | 118 | report.Rows = append(report.Rows, newReport.Rows...) 119 | return report, nil 120 | } 121 | return report, nil 122 | } 123 | 124 | func (client *GoogleClient) getRealtimeReport(query model.QueryModel) (*analyticsdata.RunRealtimeReportResponse, error) { 125 | defer util.Elapsed("Get getRealtimeReport data at GA API")() 126 | log.DefaultLogger.Debug("getRealtimeReport", "queries", query) 127 | Metrics := []*analyticsdata.Metric{} 128 | Dimensions := []*analyticsdata.Dimension{} 129 | for _, metric := range query.Metrics { 130 | Metrics = append(Metrics, &analyticsdata.Metric{Name: metric}) 131 | } 132 | for _, dimension := range query.Dimensions { 133 | Dimensions = append(Dimensions, &analyticsdata.Dimension{Name: dimension}) 134 | } 135 | 136 | end := time.Since(query.To) 137 | start := time.Since(query.From) 138 | 139 | log.DefaultLogger.Debug("getRealtimeReport", "start", start.Minutes()) 140 | log.DefaultLogger.Debug("getRealtimeReport", "end", end.Minutes()) 141 | 142 | var ( 143 | min = GaRealTimeMinMinute 144 | max = GaRealTimeMaxMinute 145 | ) 146 | 147 | if query.ServiceLevel == model.ServiceLevelPremium { 148 | max = Ga360RealTimeMaxMinute 149 | } 150 | 151 | if end < min { 152 | end = min 153 | } 154 | 155 | if start > max { 156 | start = max 157 | } 158 | 159 | log.DefaultLogger.Debug("getRealtimeReport", "after start", start.Minutes()) 160 | log.DefaultLogger.Debug("getRealtimeReport", "after end", end.Minutes()) 161 | 162 | log.DefaultLogger.Debug("getRealtimeReport", "real start", int64(start.Minutes())) 163 | log.DefaultLogger.Debug("getRealtimeReport", "real end", int64(end.Minutes())) 164 | req := analyticsdata.RunRealtimeReportRequest{ 165 | Metrics: Metrics, 166 | Dimensions: Dimensions, 167 | MinuteRanges: []*analyticsdata.MinuteRange{ 168 | { 169 | EndMinutesAgo: int64(end.Minutes()), 170 | StartMinutesAgo: int64(start.Minutes()), 171 | }, 172 | }, 173 | } 174 | if len(query.Dimensions) > 0 { 175 | req.OrderBys = []*analyticsdata.OrderBy{ 176 | { 177 | Dimension: &analyticsdata.DimensionOrderBy{ 178 | DimensionName: query.Dimensions[0], 179 | }, 180 | }, 181 | } 182 | } 183 | if !(query.DimensionFilter.OrGroup == nil && query.DimensionFilter.AndGroup == nil && query.DimensionFilter.Filter == nil && query.DimensionFilter.NotExpression == nil) { 184 | req.DimensionFilter = &query.DimensionFilter 185 | } 186 | log.DefaultLogger.Debug("Doing GET request from analytics reporting", "req", req) 187 | // Call the BatchGet method and return the response. 188 | report, err := client.analyticsdata.Properties.RunRealtimeReport(query.WebPropertyID, &req).Do() 189 | if err != nil { 190 | return nil, fmt.Errorf(err.Error()) 191 | } 192 | // TODO 페이지 네이션 193 | log.DefaultLogger.Debug("Do GET report", "report len", report.RowCount, "report", report) 194 | 195 | if report.RowCount > (query.Offset + GaReportMaxResult) { 196 | query.Offset = query.Offset + GaReportMaxResult 197 | newReport, err := client.getReport(query) 198 | if err != nil { 199 | return nil, fmt.Errorf(err.Error()) 200 | } 201 | 202 | report.Rows = append(report.Rows, newReport.Rows...) 203 | return report, nil 204 | } 205 | return report, nil 206 | } 207 | 208 | // func printResponse(res *reporting.GetReportsResponse) { 209 | // log.DefaultLogger.Debug("Printing Response from analytics reporting", "") 210 | // for _, report := range res.Reports { 211 | // header := report.ColumnHeader 212 | // dimHdrs := header.Dimensions 213 | // metricHdrs := header.MetricHeader.MetricHeaderEntries 214 | // rows := report.Data.Rows 215 | 216 | // if rows == nil { 217 | // log.DefaultLogger.Debug("no data", "") 218 | // } 219 | // for _, row := range rows { 220 | // dims := row.Dimensions 221 | // metrics := row.Metrics 222 | 223 | // for i := 0; i < len(dimHdrs) && i < len(dims); i++ { 224 | // log.DefaultLogger.Debug("%s: %s", dimHdrs[i], dims[i]) 225 | // } 226 | 227 | // for _, metric := range metrics { 228 | // // We have only 1 date range in the example 229 | // // So it'll always print "Date Range (0)" 230 | // // log.DefaultLogger.Defaultlog.DefaultLogger.Infof("Date Range (%d)", idx) 231 | // for j := 0; j < len(metricHdrs) && j < len(metric.Values); j++ { 232 | // log.DefaultLogger.Debug("%s: %s", metricHdrs[j].Name, metric.Values[j]) 233 | // } 234 | // } 235 | // } 236 | // } 237 | // log.DefaultLogger.Info("Completed printing response", "", "") 238 | // } 239 | 240 | func (client *GoogleClient) getMetadata(propertyID string) (*analyticsdata.Metadata, error) { 241 | if propertyID == "" { 242 | propertyID = "0" 243 | } 244 | nameid := "properties/" + propertyID + "/metadata" 245 | metadata, err := client.analyticsdata.Properties.GetMetadata(nameid).Do() 246 | if err != nil { 247 | return nil, err 248 | } 249 | return metadata, nil 250 | } 251 | 252 | func (client *GoogleClient) getAccountSummaries(nextPageToekn string) ([]*analyticsadmin.GoogleAnalyticsAdminV1betaAccountSummary, error) { 253 | accountSummaries, err := client.analyticsadmin.AccountSummaries.List().PageSize(GaAdminMaxResult).PageToken(nextPageToekn).Do() 254 | if err != nil { 255 | log.DefaultLogger.Error("getAccountSummary fail", "error", err.Error()) 256 | return nil, err 257 | } 258 | 259 | nextPageToken := accountSummaries.NextPageToken 260 | 261 | if nextPageToken != "" { 262 | nextAccountSummaries, err := client.getAccountSummaries(nextPageToken) 263 | if err != nil { 264 | return nil, err 265 | } 266 | accountSummaries.AccountSummaries = append(accountSummaries.AccountSummaries, nextAccountSummaries...) 267 | } 268 | 269 | return accountSummaries.AccountSummaries, nil 270 | } 271 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2020 blackcowmoo 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------