Skip to content

Commit 58b326a

Browse files
committed
feat: add appengine-java25 samples
1 parent 39962cb commit 58b326a

File tree

22 files changed

+1368
-0
lines changed

22 files changed

+1368
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Google Analytics sample for Google App Engine
2+
3+
<a href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&page=editor&open_in_editor=appengine-java21/analytics/README.md">
4+
<img alt="Open in Cloud Shell" src ="https://gstatic.com/cloudssh/images/open-btn.png"></a>
5+
6+
Integrating App Engine with Google Analytics using EE11.
7+
8+
## Project setup, installation, and configuration
9+
10+
- Register for [Google Analytics](http://www.google.com/analytics/), create
11+
an application, and get a tracking Id.
12+
- [Find your tracking Id](https://support.google.com/analytics/answer/1008080?hl=en)
13+
and set it as an environment variable in [`appengine-web.xml`](src/main/webapp/WEB-INF/appengine-web.xml).
14+
15+
## Running locally
16+
This example uses the
17+
[Maven Cloud CLI based plugin](https://cloud.google.com/appengine/docs/java/tools/using-maven).
18+
To run this sample locally:
19+
20+
$ mvn appengine:run
21+
22+
## Deploying
23+
24+
$ mvn clean package appengine:deploy
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<!--
2+
Copyright 2017 Google LLC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
<project xmlns="http://maven.apache.org/POM/4.0.0"
17+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
18+
<modelVersion>4.0.0</modelVersion>
19+
<packaging>war</packaging>
20+
<version>1.0-SNAPSHOT</version>
21+
<groupId>com.example.appengine</groupId>
22+
<artifactId>appengine-analytics-j25-ee11</artifactId>
23+
24+
<!--
25+
The parent pom defines common style checks and testing strategies for our samples.
26+
Removing or replacing it should not effect the execution of the samples in anyway.
27+
-->
28+
<parent>
29+
<groupId>com.google.cloud.samples</groupId>
30+
<artifactId>shared-configuration</artifactId>
31+
<version>1.2.0</version>
32+
</parent>
33+
34+
<properties>
35+
<maven.compiler.target>25</maven.compiler.target>
36+
<maven.compiler.source>25</maven.compiler.source>
37+
<appengine.sdk.version>2.0.29</appengine.sdk.version>
38+
</properties>
39+
40+
<dependencyManagement>
41+
<dependencies>
42+
<dependency>
43+
<artifactId>libraries-bom</artifactId>
44+
<groupId>com.google.cloud</groupId>
45+
<scope>import</scope>
46+
<type>pom</type>
47+
<version>26.28.0</version>
48+
</dependency>
49+
</dependencies>
50+
</dependencyManagement>
51+
52+
<dependencies>
53+
<!-- Compile/runtime dependencies -->
54+
<dependency>
55+
<groupId>com.google.appengine</groupId>
56+
<artifactId>appengine-api-1.0-sdk</artifactId>
57+
<version>${appengine.sdk.version}</version>
58+
</dependency>
59+
60+
<dependency>
61+
<groupId>jstl</groupId>
62+
<artifactId>jstl</artifactId>
63+
<version>1.2</version>
64+
</dependency>
65+
66+
<dependency>
67+
<groupId>org.apache.httpcomponents</groupId>
68+
<artifactId>httpclient</artifactId>
69+
<version>4.5.14</version>
70+
</dependency>
71+
72+
<dependency>
73+
<groupId>jakarta.servlet</groupId>
74+
<artifactId>jakarta.servlet-api</artifactId>
75+
<version>6.0.0</version>
76+
<type>jar</type>
77+
<scope>provided</scope>
78+
</dependency>
79+
80+
<!-- Test Dependencies -->
81+
<dependency>
82+
<groupId>com.google.appengine</groupId>
83+
<artifactId>appengine-testing</artifactId>
84+
<version>${appengine.sdk.version}</version>
85+
<scope>test</scope>
86+
</dependency>
87+
<dependency>
88+
<groupId>com.google.appengine</groupId>
89+
<artifactId>appengine-api-stubs</artifactId>
90+
<version>${appengine.sdk.version}</version>
91+
<scope>test</scope>
92+
</dependency>
93+
94+
<dependency>
95+
<groupId>junit</groupId>
96+
<artifactId>junit</artifactId>
97+
<version>4.13.2</version>
98+
<scope>test</scope>
99+
</dependency>
100+
<dependency>
101+
<groupId>org.mockito</groupId>
102+
<artifactId>mockito-core</artifactId>
103+
<version>4.11.0</version>
104+
<scope>test</scope>
105+
</dependency>
106+
<dependency>
107+
<groupId>com.google.truth</groupId>
108+
<artifactId>truth</artifactId>
109+
<version>1.1.5</version>
110+
<scope>test</scope>
111+
</dependency>
112+
</dependencies>
113+
114+
<build>
115+
<!-- for hot reload of the web application -->
116+
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
117+
<plugins>
118+
<plugin>
119+
<groupId>org.apache.maven.plugins</groupId>
120+
<artifactId>maven-compiler-plugin</artifactId>
121+
<version>3.13.0</version>
122+
</plugin>
123+
<plugin>
124+
<groupId>org.apache.maven.plugins</groupId>
125+
<artifactId>maven-war-plugin</artifactId>
126+
<version>3.4.0</version>
127+
</plugin>
128+
<plugin>
129+
<groupId>com.google.cloud.tools</groupId>
130+
<artifactId>appengine-maven-plugin</artifactId>
131+
<version>2.7.0</version>
132+
<configuration>
133+
<projectId>GCLOUD_CONFIG</projectId>
134+
<version>analytics</version>
135+
<deploy.promote>true</deploy.promote>
136+
<deploy.stopPreviousVersion>true</deploy.stopPreviousVersion>
137+
</configuration>
138+
</plugin>
139+
</plugins>
140+
</build>
141+
</project>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2015 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.appengine.analytics;
18+
19+
// [START gae_java21_analytics_track]
20+
import com.google.appengine.api.urlfetch.URLFetchService;
21+
import com.google.appengine.api.urlfetch.URLFetchServiceFactory;
22+
import java.io.IOException;
23+
import java.net.URI;
24+
import java.net.URISyntaxException;
25+
import java.net.URL;
26+
import jakarta.servlet.ServletException;
27+
import jakarta.servlet.annotation.WebServlet;
28+
import jakarta.servlet.http.HttpServlet;
29+
import jakarta.servlet.http.HttpServletRequest;
30+
import jakarta.servlet.http.HttpServletResponse;
31+
import org.apache.http.client.utils.URIBuilder;
32+
33+
@SuppressWarnings("serial")
34+
// With @WebServlet annotation the webapp/WEB-INF/web.xml is no longer required.
35+
@WebServlet(
36+
name = "analytics",
37+
description = "Analytics: Send Analytics Event to Google Analytics",
38+
urlPatterns = "/analytics")
39+
public class AnalyticsServlet extends HttpServlet {
40+
41+
@Override
42+
public void doGet(HttpServletRequest req, HttpServletResponse resp)
43+
throws IOException, ServletException {
44+
String trackingId = System.getenv("GA_TRACKING_ID");
45+
URIBuilder builder = new URIBuilder();
46+
builder
47+
.setScheme("http")
48+
.setHost("www.google-analytics.com")
49+
.setPath("/collect")
50+
.addParameter("v", "1") // API Version.
51+
.addParameter("tid", trackingId) // Tracking ID / Property ID.
52+
// Anonymous Client Identifier. Ideally, this should be a UUID that
53+
// is associated with particular user, device, or browser instance.
54+
.addParameter("cid", "555")
55+
.addParameter("t", "event") // Event hit type.
56+
.addParameter("ec", "example") // Event category.
57+
.addParameter("ea", "test action"); // Event action.
58+
URI uri = null;
59+
try {
60+
uri = builder.build();
61+
} catch (URISyntaxException e) {
62+
throw new ServletException("Problem building URI", e);
63+
}
64+
URLFetchService fetcher = URLFetchServiceFactory.getURLFetchService();
65+
URL url = uri.toURL();
66+
fetcher.fetch(url);
67+
resp.getWriter().println("Event tracked.");
68+
}
69+
}
70+
// [END gae_java21_analytics_track]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- [START_EXCLUDE] -->
3+
<!--
4+
Copyright 2015 Google LLC
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
-->
15+
<!-- [END_EXCLUDE] -->
16+
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
17+
<runtime>java25</runtime>
18+
<app-engine-apis>true</app-engine-apis>
19+
<service>appengine-analytics-j25-ee11</service>
20+
<system-properties>
21+
<property name="appengine.use.EE11" value="true"/>
22+
</system-properties>
23+
<env-variables>
24+
<env-var name="GA_TRACKING_ID" value="YOUR-GA-TRACKING-ID" />
25+
</env-variables>
26+
</appengine-web-app>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- [START_EXCLUDE] -->
3+
<!--
4+
Copyright 2015 Google LLC
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
-->
15+
<!-- [END_EXCLUDE] -->
16+
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
17+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
18+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
19+
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
20+
<welcome-file-list>
21+
<welcome-file>analytics</welcome-file>
22+
</welcome-file-list>
23+
</web-app>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Google Analytics sample for Google App Engine
2+
3+
<a href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&page=editor&open_in_editor=appengine-java21/analytics/README.md">
4+
<img alt="Open in Cloud Shell" src ="https://gstatic.com/cloudssh/images/open-btn.png"></a>
5+
6+
Integrating App Engine with Google Analytics using EE8.
7+
8+
## Project setup, installation, and configuration
9+
10+
- Register for [Google Analytics](http://www.google.com/analytics/), create
11+
an application, and get a tracking Id.
12+
- [Find your tracking Id](https://support.google.com/analytics/answer/1008080?hl=en)
13+
and set it as an environment variable in [`appengine-web.xml`](src/main/webapp/WEB-INF/appengine-web.xml).
14+
15+
## Running locally
16+
This example uses the
17+
[Maven Cloud CLI based plugin](https://cloud.google.com/appengine/docs/java/tools/using-maven).
18+
To run this sample locally:
19+
20+
$ mvn appengine:run
21+
22+
## Deploying
23+
24+
$ mvn clean package appengine:deploy

0 commit comments

Comments
 (0)