Skip to content

Commit f668bda

Browse files
authored
Merge pull request #3 from dotCMS/feature/rest-endpoint
Add REST endpoint for headless Google Analytics queries
2 parents f9c81c7 + 9612316 commit f668bda

6 files changed

Lines changed: 398 additions & 11 deletions

File tree

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ Before submitting a PR, verify:
136136
- [ ] Plugin builds without errors: `./gradlew clean jar`
137137
- [ ] JAR uploads successfully to dotCMS
138138
- [ ] OSGi bundle starts without errors (check logs for "Starting Google Analytics OSGI plugin")
139-
- [ ] Viewtool is available in Velocity (`$analytics`)
139+
- [ ] Viewtool is available in Velocity (`$googleanalytics`)
140140
- [ ] Can create analytics request and query GA4 data
141141
- [ ] No breaking changes to existing Velocity code (or documented if necessary)
142142
- [ ] Works with dotCMS 23.01.10 and newer

README.md

Lines changed: 90 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ OSGi plugin that integrates Google Analytics 4 (GA4) Data API with dotCMS, enabl
66

77
## What It Does
88

9-
This plugin provides a `$analytics` viewtool in Velocity templates for querying Google Analytics 4 data directly from your dotCMS pages. Retrieve metrics like sessions, active users, page views, and more—filtered by dimensions like date, page path, device category, etc.
9+
This plugin provides a `$googleanalytics` viewtool in Velocity templates for querying Google Analytics 4 data directly from your dotCMS pages. Retrieve metrics like sessions, active users, page views, and more—filtered by dimensions like date, page path, device category, etc.
1010

1111
**Note:** This plugin *fetches* analytics data from Google Analytics. It does NOT add tracking code to your site.
1212

@@ -46,14 +46,14 @@ This plugin provides a `$analytics` viewtool in Velocity templates for querying
4646
#set($propertyId = "123456789")
4747
4848
## Create and configure request
49-
#set($gaRequest = $analytics.createAnalyticsRequest($propertyId))
49+
#set($gaRequest = $googleanalytics.createAnalyticsRequest($propertyId))
5050
$gaRequest.setStartDate("2026-02-09")
5151
$gaRequest.setEndDate("2026-02-16")
5252
$gaRequest.setMetrics("sessions,activeUsers")
5353
$gaRequest.setDimensions("date")
5454
5555
## Execute query
56-
#set($gaResponse = $analytics.query($gaRequest))
56+
#set($gaResponse = $googleanalytics.query($gaRequest))
5757
5858
## Display results
5959
<table>
@@ -72,6 +72,58 @@ $gaRequest.setDimensions("date")
7272
</table>
7373
```
7474

75+
### REST API Usage
76+
77+
Query Google Analytics data via REST endpoint:
78+
79+
```bash
80+
curl -X POST http://localhost:8080/api/v1/googleanalytics/query \
81+
-H "Content-Type: application/json" \
82+
-u admin@dotcms.com:admin \
83+
-d '{
84+
"propertyId": "123456789",
85+
"startDate": "2026-02-09",
86+
"endDate": "2026-02-16",
87+
"metrics": ["sessions", "activeUsers"],
88+
"dimensions": ["date", "pagePath"],
89+
"filters": {
90+
"dimension": [
91+
{"field": "pagePath", "value": "/products", "operator": "CONTAINS"}
92+
]
93+
},
94+
"sort": "sessions",
95+
"maxResults": 100
96+
}'
97+
```
98+
99+
**Response:**
100+
101+
```json
102+
{
103+
"rowCount": 7,
104+
"dimensions": ["date", "pagePath"],
105+
"metrics": ["sessions", "activeUsers"],
106+
"rows": [
107+
{
108+
"date": "20260209",
109+
"pagePath": "/products",
110+
"sessions": "150",
111+
"activeUsers": "120"
112+
},
113+
{
114+
"date": "20260210",
115+
"pagePath": "/home",
116+
"sessions": "200",
117+
"activeUsers": "180"
118+
}
119+
],
120+
"metadata": {
121+
"currencyCode": "USD",
122+
"timeZone": "America/New_York"
123+
}
124+
}
125+
```
126+
75127
## Documentation
76128

77129
For complete setup instructions including Google Cloud configuration, Google Analytics permissions, advanced usage, and troubleshooting:
@@ -86,16 +138,46 @@ For complete setup instructions including Google Cloud configuration, Google Ana
86138
- **Troubleshooting** - OSGi issues, metric errors, variable name conflicts
87139
- **Available Metrics & Dimensions** - GA4 API schema reference
88140

89-
## Available Metrics
141+
## Understanding Dimensions and Metrics
142+
143+
When querying Google Analytics, you combine **dimensions** and **metrics** to get the data you need:
144+
145+
### Dimensions (What to group by)
90146

91-
Common GA4 metrics you can query:
92-
- `sessions` - Number of sessions
147+
Dimensions are categorical attributes that describe your data—they answer "what are we breaking this down by?"
148+
149+
Common dimensions:
150+
- `date` - When the activity happened (e.g., "20260209")
151+
- `pagePath` - Which page was viewed (e.g., "/products")
152+
- `country` - Where users are located (e.g., "United States")
153+
- `deviceCategory` - Device type (e.g., "desktop", "mobile", "tablet")
154+
- `browser` - Browser used (e.g., "Chrome", "Safari")
155+
- `city` - User's city (e.g., "New York")
156+
- `source` - Traffic source (e.g., "google", "facebook", "direct")
157+
158+
### Metrics (What to measure)
159+
160+
Metrics are the numerical measurements you want to analyze:
161+
- `sessions` - Number of sessions (visits)
93162
- `activeUsers` - Number of distinct users
94-
- `screenPageViews` - Total page and screen views
163+
- `screenPageViews` - Total page views
95164
- `bounceRate` - Percentage of single-page sessions
96165
- `averageSessionDuration` - Average session duration in seconds
97166

98-
See the [GA4 API Schema](https://developers.google.com/analytics/devguides/reporting/data/v1/api-schema) for the full list.
167+
### Example Query
168+
169+
"Show me sessions and active users, broken down by date and page path"
170+
171+
```json
172+
{
173+
"dimensions": ["date", "pagePath"],
174+
"metrics": ["sessions", "activeUsers"]
175+
}
176+
```
177+
178+
Each row in the response represents one unique combination of dimension values with its associated metrics.
179+
180+
See the [GA4 API Schema](https://developers.google.com/analytics/devguides/reporting/data/v1/api-schema) for the complete list of available dimensions and metrics.
99181

100182
## Version History
101183

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ plugins {
88

99

1010
sourceCompatibility = JavaVersion.VERSION_11
11-
version = '0.4.1'
11+
version = '0.5.0'
1212

1313

1414
repositories {
@@ -36,6 +36,7 @@ dependencies {
3636
//compileOnly('org.apache.httpcomponents:httpclient:4.5.9')
3737
// https://mvnrepository.com/artifact/javax.servlet/servlet-api
3838
compileOnly group: 'javax.servlet', name: 'servlet-api', version: '2.5'
39+
compileOnly group: 'javax.ws.rs', name: 'javax.ws.rs-api', version: '2.1.1'
3940

4041
}
4142

src/main/java/com/dotcms/google/analytics/osgi/Activator.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.dotcms.google.analytics.osgi;
22

33
import com.dotcms.google.analytics.app.AnalyticsAppService;
4+
import com.dotcms.google.analytics.rest.GoogleAnalyticsResource;
45
import com.dotcms.google.analytics.view.AnalyticsToolInfo;
56
import com.dotmarketing.business.CacheLocator;
67
import com.dotmarketing.loggers.Log4jUtil;
@@ -45,6 +46,9 @@ public final void start(final BundleContext bundleContext) throws Exception {
4546
// copy the yaml
4647
copyAppYml();
4748

49+
// Register REST resources
50+
publishBundleServices(bundleContext);
51+
4852
// Register all ViewTool services
4953
registerViewToolService(bundleContext, new AnalyticsToolInfo());
5054

0 commit comments

Comments
 (0)