Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .kokoro/tests/run_test_java.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ SCRIPT_DIR="$(dirname $0)/"

# Fail the tests if no Java version was found.
POM_JAVA=$(grep -oP '(?<=<maven.compiler.target>).*?(?=</maven.compiler.target>)' pom.xml)
ALLOWED_VERSIONS=("1.8" "11" "17" "21")
ALLOWED_VERSIONS=("1.8" "11" "17" "21", "25")
# shellcheck disable=SC2199
# shellcheck disable=SC2076
if [[ "$POM_JAVA" = "" ]] || [[ ! " ${ALLOWED_VERSIONS[*]} " =~ " ${POM_JAVA} " ]]; then
Expand Down
24 changes: 24 additions & 0 deletions appengine-java25/ee11/analytics/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Google Analytics sample for Google App Engine

<a href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&page=editor&open_in_editor=appengine-java25/ee11/analytics/README.md">
<img alt="Open in Cloud Shell" src ="https://gstatic.com/cloudssh/images/open-btn.png"></a>

Integrating App Engine with Google Analytics using EE11.

## Project setup, installation, and configuration

- Register for [Google Analytics](http://www.google.com/analytics/), create
an application, and get a tracking Id.
- [Find your tracking Id](https://support.google.com/analytics/answer/1008080?hl=en)
and set it as an environment variable in [`appengine-web.xml`](src/main/webapp/WEB-INF/appengine-web.xml).

## Running locally
This example uses the
[Maven Cloud CLI based plugin](https://cloud.google.com/appengine/docs/java/tools/using-maven).
To run this sample locally:

$ mvn appengine:run

## Deploying

$ mvn clean package appengine:deploy
141 changes: 141 additions & 0 deletions appengine-java25/ee11/analytics/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<!--
Copyright 2017 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
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">
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<groupId>com.example.appengine</groupId>
<artifactId>appengine-analytics-j25-ee11</artifactId>

<!--
The parent pom defines common style checks and testing strategies for our samples.
Removing or replacing it should not effect the execution of the samples in anyway.
-->
<parent>
<groupId>com.google.cloud.samples</groupId>
<artifactId>shared-configuration</artifactId>
<version>1.2.0</version>
</parent>

<properties>
<maven.compiler.target>25</maven.compiler.target>
<maven.compiler.source>25</maven.compiler.source>
<appengine.sdk.version>3.0.1</appengine.sdk.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<artifactId>libraries-bom</artifactId>
<groupId>com.google.cloud</groupId>
<scope>import</scope>
<type>pom</type>
<version>26.28.0</version>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<!-- Compile/runtime dependencies -->
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-api-1.0-sdk</artifactId>
<version>${appengine.sdk.version}</version>
</dependency>

<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.14</version>
</dependency>

<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.0.0</version>
<type>jar</type>
<scope>provided</scope>
</dependency>

<!-- Test Dependencies -->
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-testing</artifactId>
<version>${appengine.sdk.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-api-stubs</artifactId>
<version>${appengine.sdk.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>1.1.5</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<!-- for hot reload of the web application -->
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.4.0</version>
</plugin>
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>2.7.0</version>
<configuration>
<projectId>GCLOUD_CONFIG</projectId>
<version>analytics</version>
<deploy.promote>true</deploy.promote>
<deploy.stopPreviousVersion>true</deploy.stopPreviousVersion>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2015 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.appengine.analytics;

// [START gae_java25_analytics_track]

import com.google.appengine.api.urlfetch.URLFetchService;
import com.google.appengine.api.urlfetch.URLFetchServiceFactory;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import org.apache.http.client.utils.URIBuilder;

@SuppressWarnings("serial")
// With @WebServlet annotation the webapp/WEB-INF/web.xml is no longer required.
@WebServlet(
name = "analytics",
description = "Analytics: Send Analytics Event to Google Analytics",
urlPatterns = "/analytics")
public class AnalyticsServlet extends HttpServlet {

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException {
String trackingId = System.getenv("GA_TRACKING_ID");
URIBuilder builder = new URIBuilder();
builder
.setScheme("http")
.setHost("www.google-analytics.com")
.setPath("/collect")
.addParameter("v", "1") // API Version.
.addParameter("tid", trackingId) // Tracking ID / Property ID.
// Anonymous Client Identifier. Ideally, this should be a UUID that
// is associated with particular user, device, or browser instance.
.addParameter("cid", "555")
.addParameter("t", "event") // Event hit type.
.addParameter("ec", "example") // Event category.
.addParameter("ea", "test action"); // Event action.
URI uri = null;
try {
uri = builder.build();
} catch (URISyntaxException e) {
throw new ServletException("Problem building URI", e);
}
URLFetchService fetcher = URLFetchServiceFactory.getURLFetchService();
URL url = uri.toURL();
fetcher.fetch(url);
resp.getWriter().println("Event tracked.");
}
}
// [END gae_java25_analytics_track]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- [START_EXCLUDE] -->
<!--
Copyright 2015 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- [END_EXCLUDE] -->
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<runtime>java25</runtime>
<app-engine-apis>true</app-engine-apis>
<service>appengine-analytics-j25-ee11</service>
<system-properties>
<property name="appengine.use.EE11" value="true"/>
</system-properties>
<env-variables>
<env-var name="GA_TRACKING_ID" value="YOUR-GA-TRACKING-ID" />
</env-variables>
</appengine-web-app>
23 changes: 23 additions & 0 deletions appengine-java25/ee11/analytics/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- [START_EXCLUDE] -->
<!--
Copyright 2015 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- [END_EXCLUDE] -->
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd"
version="6.0">
<welcome-file-list>
<welcome-file>analytics</welcome-file>
</welcome-file-list>
</web-app>
24 changes: 24 additions & 0 deletions appengine-java25/ee8/analytics/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Google Analytics sample for Google App Engine

<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">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The open_in_editor URL for "Open in Cloud Shell" points to a java21 path. It should be updated to point to the correct path for this Java 25 sample.

Suggested change
<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">
<a href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&page=editor&open_in_editor=appengine-java25/ee8/analytics/README.md">

<img alt="Open in Cloud Shell" src ="https://gstatic.com/cloudssh/images/open-btn.png"></a>

Integrating App Engine with Google Analytics using EE8.

## Project setup, installation, and configuration

- Register for [Google Analytics](http://www.google.com/analytics/), create
an application, and get a tracking Id.
- [Find your tracking Id](https://support.google.com/analytics/answer/1008080?hl=en)
and set it as an environment variable in [`appengine-web.xml`](src/main/webapp/WEB-INF/appengine-web.xml).

## Running locally
This example uses the
[Maven Cloud CLI based plugin](https://cloud.google.com/appengine/docs/java/tools/using-maven).
To run this sample locally:

$ mvn appengine:run

## Deploying

$ mvn clean package appengine:deploy
Loading