Coder Social home page Coder Social logo

tcorcor1 / google-analytics-express-ts Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 0.0 11 KB

Express / TypeScript project designed for interaction with Google Analytics Reporting API v4

Home Page: https://tldr-dynamics.com/blog/google-analytics-express-typescript

License: MIT License

TypeScript 100.00%
node express typescript google-analytics-api

google-analytics-express-ts's Introduction

Google Analytics Reporting API v4 Project Boilerplate

Designed with Node/Express & TypeScript to demonstrate interaction with Core Reporting API v4

๐Ÿšง ๐Ÿšง DEPRECATED ๐Ÿšง ๐Ÿšง

The Google Analytics v4 Core Reporting API was built for Universal Analytics properties which are now deprecated and will stop collecting data in July 2023. If you wish to continue working with your GA data in your apps you must transition to the Google Analytics Data API v1.

Should you still be interested in this project, I have created a super simple API that hits this new endpoint and gets your top 50 results. It can be found here but be aware their client library is still in beta so if you are reading this some time after writing there is likely an updated package you should install.

Getting started

For more information on using this project please see my blog post on tldr-dynamics.com.

Create a Google Cloud Platform account and service credentials

  1. Create a Google Cloud Platform account if you don't already have one and enable the Google Analytics Reporting API.
  2. Select APIs / Services > Credentials > Create Credentials > Service Account
  3. Give Service Account 'Monitoring Admin' role
  4. Create a key (JSON) for Service Account
  5. Add this user to your Google Analytics account and make sure it has permissions to read the view you will be using in the app. Also, make note of the view id as we need that later

Installation

$ git clone https://github.com/tcorcor1/google-analytics-express.git
$ npm install
  1. Rename example.env to .env and update GA_PRIVATE_KEY and GA_CLIENT_EMAIL to the values in your JSON key
  2. Update the view id in /src/services/google-analytics/index.ts
  3. Before leaving the Google Analytics service review the dimensionsFilterClauses prop in the report request as it might not fit your situation. Currently, the request will be filtered to only pages that contain a /blog/ slug in their path. This may be completely wrong for your situation or require a simple update to something like /posts/. Comment it out or replace with whatever is appropriate for your situation.
$ npm start

Review the data

Review your data by hitting http://localhost:5000/api/posts. You will want to adjust the query params according to your needs.

In the sample you can use ?top=10 to return the top 10 posts and/or filter your dataset by last 30 days by using query param ?days-prev=30.

If nothing is provided ?top=undefined and ?days-prev=2011-01-01 so make adjustments to PostQueryParams class or set query params however you prefer.

You will have to provide a start date to GA so if you dont pass a days-prev value you should at least update the class to have the start date of when you created your GA account as the fallback.

I wanted any easy way to have 'top posts last 30 days' and 'top 5 posts last 365 days' but this is really just for demonstration purposes.

I hope you will find this a useful project for getting started with the Google Analytics API!

Recommend Projects

  • React photo React

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

  • Vue.js photo Vue.js

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

  • Typescript photo Typescript

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

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

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

Recommend Topics

  • javascript

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

  • web

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

  • server

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

  • Machine learning

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

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

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

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

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

  • D3 photo D3

    Data-Driven Documents codes.