Giter Club home page Giter Club logo

ga-hit-count-serverless's Introduction

🍐 ga-hit-count-serverless

Google Analytics hit count API for static websites. Deployed on Vercel.

中文说明:Hit count:用 Google Analytics + Vercel Serverless 为文章添加浏览量统计

See it in action

How to deploy?

Getting started

First, fork this project to your own GitHub account.

Second, acquire a Google API credential file following the procedure explained here: Analytics Reporting API v4 - Enable the API. You will get a JSON credential which looks like:

{
  // ...
  "project_id": "ga-hit-count",
  "private_key": "-----BEGIN PRIVATE KEY-----\nxxx-----END PRIVATE KEY-----\n",
  "client_email": "[email protected]",
  // ...
}

You will need to add the client_email to your Google Analytics user, and grant permissions Read and Analyze if you haven't done so already. Details: Add, edit, and delete users and user groups.

Finally, change api/config.ts according to your own Google API details.

key value
viewId Check Google Analytics Dashboard: Admin » View » View Settings
projectId Your Google API project ID. (From credentials JSON file.)
privateKey Your Google API private Key. DO NOT CHANGE THIS! (We will add this private key via Vercel environment variables.)
clientEmail Your Google API client email. (From credentials JSON file.)
allFilter The filter you wish to apply for your posts. (Default ['/20'], because my posts' path begin with /2019 or /2020.)
startDate The start date you wish to query your Google Analytics API. (Default 2010-01-01, any date old enough should work.)

Deploying to Vercel

Deploy to Vercel

After importing your forked version of this project to Vercel, you'll need to add your Google API private key to Vercel environment variable. Create a new variable named PRIVATE_KEY and copy the private key into it. Your private key should look like this (i.e., remove all \n and replace with real linebreaks.):

-----BEGIN PRIVATE KEY-----
dageWvAIBADANBAokdP8WgkqhkiGkk
...
afROdsafbliOjPA==1Hk3mdsafEdBa
-----END PRIVATE KEY-----

After this, trigger a new deploy to production.

API

Base URL:

https://{VERCEL_DOMAIN_NAME}.vercel.app

Query all posts

Request:

/api/ga

Response demo:

[
  {
    "page": "/2019/11/tiny-tiny-rss/",
    "hit": "698"
  },
  {
    "page": "/2019/11/weibo-to-twitter/",
    "hit": "531"
  },
  {
    "page": "/2020/03/ttrss-noteworthy/",
    "hit": "357"
  },
  // ...
]

Query specific post

Request:

/api/ga?page={WEBSITE_PAGE_PATH}

Request demo:

/api/ga?page=/2020/03/substats/

Response demo:

[
  {
    "page": "/2020/03/substats/",
    "hit": "311"
  }
]

🍐 GA Hit Count API for Vercel ©Spencer Woo. Released under the MIT License.

Authored and maintained by Spencer Woo.

@Portfolio · @Blog · @GitHub

ga-hit-count-serverless's People

Contributors

dependabot[bot] avatar spencerwooo avatar

Stargazers

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

Watchers

 avatar  avatar  avatar

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.