A lightweight Express.js API for retrieving pageview data from Google Analytics 4 properties, initially designed for use with Glance.
Report Bug
·
Request Feature
Table of Contents
- Fetch pageviews data for a single GA4 property
- Fetch pageviews data for multiple GA4 properties in one request
- Supports both GET and POST requests
- Returns formatted pageview counts with property names
- Simple, RESTful API endpoints
- Node.js (v14 or higher)
- npm or yarn
- Google Analytics 4 property(s)
- Google Cloud service account with appropriate permissions
- Clone this repository:
git clone https://github.com/ssyyhhrr/ga4-reporter.git
cd ga4-reporter
- Install dependencies:
npm install
-
Setup Google Analytics service account (see below)
-
Create a
.envfile (see below) -
Start the server:
npm start
- From the service accounts list in your Google Cloud project, select your service account.
- Go to the Keys tab
- Click Add Key > Create new key
- Select JSON as the key type and click Create
- Move the key file to the project directory
- Go to your Google Analytics account
- Navigate to Admin > Property > Property Access Management
- Click the + button to add a user
- Enter the email address of your service account (found in the service account details in Google Cloud Console)
- Select the role Viewer
- Click Add
- Repeat this process for each GA4 property you want to access
Create a .env file in the root directory with the following variables
PORT=3000
GOOGLE_APPLICATION_CREDENTIALS=service-account-key.json # Name of your service account key
GET /healthcheck
Returns a simple status check to verify the API is running.
GET /api/pageviews/:propertyid
Returns pageviews for a single GA4 property.
Example Response:
{
"pageviews": 12345
}GET /api/pageviews?ids=123456789,987654321
Returns pageviews for multiple GA4 properties using comma-separated IDs.
Example Response:
{
"properties": [
{
"propertyId": "123456789",
"propertyName": "My Website",
"pageviews": 12345,
"pageviewsFormatted": "12,345"
},
{
"propertyId": "987654321",
"propertyName": "My Other Website",
"pageviews": 67890,
"pageviewsFormatted": "67,890"
}
],
"total": 80235
}POST /api/pageviews
Body:
{
"propertyIds": ["123456789", "987654321"]
}Returns the same format as the GET endpoint for multiple properties.
- Go to your Google Analytics account
- Select the property you want to use
- Go to Admin > Property Settings
- The Property ID is displayed at the top (format: "123456789")
# Get pageviews for a single property
curl http://localhost:3000/api/pageviews/123456789
# Get pageviews for multiple properties
curl http://localhost:3000/api/pageviews?ids=123456789,987654321
# POST request for multiple properties
curl -X POST http://localhost:3000/api/pageviews \
-H "Content-Type: application/json" \
-d '{"propertyIds": ["123456789", "987654321"]}'// Get pageviews for multiple properties
fetch('http://localhost:3000/api/pageviews?ids=123456789,987654321')
.then(response => response.json())
.then(data => console.log(data));
// POST request for multiple properties
fetch('http://localhost:3000/api/pageviews', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
propertyIds: ['123456789', '987654321']
}),
})
.then(response => response.json())
.then(data => console.log(data));- Ensure your service account key is securely stored
- Implement rate limiting if exposing the API publicly
- Consider adding authentication if the API will be accessed by multiple clients
If you have a suggestion that would make this project better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request
Distributed under the MIT License. See LICENSE.txt for more information.
- Authentication errors: Ensure your service account has been properly granted access to the GA4 properties and the key file is correctly referenced in your .env file.
- No data returned: Check that your property IDs are correct and that there is data for the time period (the API fetches data from yesterday to today).
- "Property not found" errors: Verify that the property ID is correct and that your service account has access to it.
- API rate limiting: The Google Analytics Data API has quotas. If you're making many requests, you might hit these limits.
Rhys Bishop - https://sy.hr/ - mail@rhysbi.shop
Project Link: https://github.com/ssyyhhrr/ga4-reporter