The GoogleAnalytics connector was implemented using java and consuming Google Analytics core V3 API. The connector exposes Generate Report operation.
Application/Service | Version |
---|---|
Mule Runtime |
4.x |
Google Analytics Core |
Reporting API V3 |
Java |
1.8 and later |
This document assumes that you are familiar with Mule, Anypoint Connectors, Anypoint Studio, Mule concepts, elements in a Mule flow, and Global Elements.
You need login credentials to test your connection to your target resource.
For hardware and software requirements and compatibility information, see the Connector Release Notes.
To use this connector with Maven, view the pom.xml dependency information in the Dependency Snippets in Anypoint Exchange.
-
Support for Mule4 has been added.
-
Register your application in the Google API Console.
-
Authorize access to Google Analytics Data.
-
Go to Google API Console Credentials Page and click Select a project, then NEW PROJECT, and enter a name for the project, and optionally, edit the provided project ID. Click Create.
-
To create a Client ID using an Service Account go to Service account page in Google API Console select the project if prompted.
-
In the Create service account window, type a name for the service account, and select Furnish a new private key. If you want to grant G Suite domain-wide authority to the service account, also select Enable G Suite Domain-wide Delegation. Then click Save.
-
When prompted for the Key type select JSON, and save the generated key as client_secrets.json.
-
Add service account to Google Analytics account by The newly created service account will have an email address, <projectId>-<uniqueId>@developer.gserviceaccount.com; Use this email address to add a user to the Google analytics account you want to access via the API.
-
Go back to Anypoint Studio and in a Anypoint Mule Application create a new GoogleAnalytics Connector configuration.
-
Fill the Json File Path , Application Name .
-
To get the Application name login to your Google API console and navigate to credentials and then to OAuth Consent Screen you can find Application name.
-
Save this Application name for usage.
Json File Path xxxxx.json Application Name
xxxxxxxxxxx
Save configuration. Your configuration should look like:
<google-analytics:config name="Google_Analytics_Config" doc:name="Google Analytics Config" doc:id="9cb86789-1ca1-4eda-b007-e2ca4bd63a6b" > <google-analytics:connection jsonFilePath="xxxxxx.json" applicationName="#['xxxxxxx']"/> </google-analytics:config>
-
To authorize a user in your Mule application, you should create a flow like the following calling the "authorize" operation:
<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="xxxxx" > <http:listener-connection host="localhost" port="8081" /> </http:listener-config> <flow name="generateReportFlow" doc:id="xxxxxxxxxxx" > <http:listener doc:name="Listener" doc:id="xxxxxxxxxxx" config-ref="HTTP_Listener_config" path="/test"/> <google-analytics:generate-report doc:name="Generate report" doc:id="xxxxxxxxxxxxx" config-ref="Google_Analytics_Config"/> </flow>
Then in a browser enter to http://localhost:8081/test and authorize your user.
-
For more information about the Google Analytics API Authorization : https://developers.google.com/analytics/devguides/reporting/core/v3/quickstart/service-java
The unique table ID is the Analytics view (profile) ID for which the query will retrieve the data.
Example: UA-xxxxx-xxx
. Create an Google Analytics Account.
. Sign in to your Account and click Admin
. Select an account from the menu in the ACCOUNT column.
. Select a property from the menu in the PROPERTY column.
. Under PROPERTY, click Tracking Info > Tracking Code. Your Google Analytics ID is displayed at the top of the page.
Start date for fetching Analytics data. Requests can specify a start date formatted as YYYY-MM-DD, or as a relative date (e.g., today, yesterday, or NdaysAgo where N is a positive integer).
Values must match [0-9]{4}-[0-9]{2}-[0-9]{2}|today|yesterday|[0-9]+(daysAgo).
The earliest valid start-date is 2005-01-01. There is no upper limit restriction for a start-date. Relative dates are always relative to the current date at the time of the query and are based on the timezone of the view (profile) specified in the query.
Google analytics connector has a facility to select value from the dropdown list or user can give the date manually according to the matching pattern.
Example: today,7daysAgo,2019-01-30
All Analytics data requests must specify a date range. If you do not include start-date and end-date parameters in the request, the server returns an error. Date values can be for a specific date by using the pattern YYYY-MM-DD or relative by using today, yesterday, or the NdaysAgo pattern. Values must match [0-9]{4}-[0-9]{2}-[0-9]{2}|today|yesterday|[0-9]+(daysAgo). The earliest valid end-date is 2005-01-01. There is no upper limit restriction for an end-date. Relative dates are always relative to the current date at the time of the query and are based on the timezone of the view (profile) specified in the query.
Google analytics connector has a facility to select value from the dropdown list or user can give the date manually according to the matching pattern.
Example: today,7daysAgo,2019-01-30
Metrics are the actual numbers google analytics measures from your website . whether thats number of sessions , time on page or the bounce rate.
The aggregated statistics for user activity to your site, such as clicks or pageviews. If a query has no dimensions parameter, the returned metrics provide aggregate values for the requested date range, such as overall pageviews or total bounces. However, when dimensions are requested, values are segmented by dimension value. For example, pageviews requested with country returns the total pageviews per country. When requesting metrics, keep in mind.
-
Any request must supply at least one metric; a request cannot consist only of dimensions.
-
You can supply a maximum of 10 metrics for any query.
-
Most combinations of metrics from multiple categories can be used together, provided no dimensions are specified
To know more about metrics access the link : metrics parameters
-
Click on the plus sign to build the query
-
Select metrics values from the dropdown list
-
Save the Selected values to build the query
+
<google-analytics:metrix-parameters>
<google-analytics:metrics-parameter value="Sessions" />
<google-analytics:metrics-parameter value="Bounces" />
</google-analytics:metrix-parameters>
A metrics are actual measurements produced on your website, dimensions are rules in which you can compile those measurements and convert them into actual, readable number relevant to your business.
To know more about Dimensions access the link : Dimensions parameters
Building dimension query is similar to building metrics query adding the required values from the dropdown.
+
<google-analytics:dimension-parameters>
<google-analytics:dimension-parameter value="Browser" />
<google-analytics:dimension-parameter value="City" />
</google-analytics:dimension-parameters>
A list of dimensions and metrics indicating the sorting order and sorting direction for the returned data.
-
can be sorted either in ascending order or descending order. By default it has ascending order.
<google-analytics:sortparms > <google-analytics:sort-parameter-type sortparamValue="Sessions" sortOrder="DESCENDING" /> </google-analytics:sortparms>
The filters query string parameter restricts the data returned from your request. To use the filters parameter, supply a dimension or metric on which to filter, followed by the filter expression.
-
To build a filter query click on the plus sign it opens a new window
-
key parameters shows list of available filter metrics and dimensions values
-
Operator parameter shows list of available operator in metrics like greaterThan or lessThan ..etc as shown in the table below
Operator Description ==
Equal to or exact match
!=
Not equal to or is not an exact match
<
Less than
⇐
Less than or equal to
>
Greater Than
>=
Greater Than or equal to
=@
Contains substring
!@
Does not contain substring
=~
Contains a match for regular expression
!~
Does not contain a match for regular expression
-
Value parameter is user defined and show be given by user and can be of integer or String.
example: 1, United States...etc
-
To use multiple filters we can combine them using AND , OR operation.
+
<google-analytics:filter-parameter>
<google-analytics:filter-params>
<google-analytics:filter-parameter-type key="Sessions" operator="GreaterThan" value="1" operation="AND" />
<google-analytics:filter-parameter-type key="Country" operator="Equals" value="United States" operation="AND" />
</google-analytics:filter-params>
</google-analytics:filter-parameter>
A segment is a subset of your Analytics data. For example, of your entire set of users, one segment might be users from a particular country or city. Another segment might be users who purchase a particular line of products or who visit a specific part of your site.
Segments let you isolate and analyze those subsets of data, so you can examine and respond to the component trends in your business.
-
To build a filter query click on the plus sign it opens a new window
-
Segment key gives user to build an query using sessions or users condition.
-
Segment type gives an option to select one or more conditions and/or sequences once you determine to use segment users or sessions.
-
Segment filter gives list of dimensions and metrics values to choose from dropdown list.
-
Segment Operator helps in choosing required operations to the segments. the list of operations available are shown below
Operator | Description |
---|---|
== |
Equal to or exact match |
!= |
Not equal to or is not an exact match |
< |
Less than |
⇐ |
Less than or equal to |
> |
Greater Than |
>= |
Greater Than or equal to |
<> |
Between (value is between the given range) |
[] |
In list (value is one of the listed values) |
=@ |
Contains substring |
!@ |
Does not contain substring |
=~ |
Contains a match for regular expression |
!~ |
Does not contain a match for regular expression |
-
Segment value can be of type integer or string which is given by user.
-
example: Chrome, 2 …etc
-
-
The complete query looks like
<google-analytics:segmentparameter >
<google-analytics:segmentparams >
<google-analytics:segment-parameter-type segmentKey="Users" segmentType="Condition" segmentFilter="Browser" segmentOpertor="EqualTo" segmentValue="Chrome" />
</google-analytics:segmentparams>
</google-analytics:segmentparameter>
The desired sampling levels. user can select from the following.
option | value |
---|---|
DEFAULT |
Returns response with a sample size that balances speed and accuracy. |
FASTER |
Returns a fast response with a smaller sample size. |
HIGHER_PRECISION |
Returns a more accurate response using a large sample size, but this may result in the response being slower. |
Example: DEFAULT
The first row of data to retrieve, starting at 1. Use this parameter as a pagination mechanism along with the max-results parameter.
Example: 10
The desired output type for the Analytics data returned in the response. Acceptable values are json and dataTable(Default: json).
Example: json
-
Create a new Mule Project in Anypoint Studio and fill in the Google Analytics credentials in
src/main/resources/mule-app.properties
.config.jsonPath= <JSON_PATH> config.application_name=<APPLICATION_NAME>
-
Drag an HTTP connector onto the canvas and leave the default values for Host and Port and set the path to
/test
. -
In the general tab fill the required query parameters by using valid Google Analytics ID , start-date, end-date and build Metrics Query.
-
In the Advance tab you can use optional query parameters such as sorting, filter, dimensions , segments to get more precise data.
-
Run the app. In a browser, use the following URL
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:google-analytics="http://www.mulesoft.org/schema/mule/google-analytics"
xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/google-analytics http://www.mulesoft.org/schema/mule/google-analytics/current/mule-google-analytics.xsd">
<configuration-properties file="mule-app.properties" />
<google-analytics:config name="Google_Analytics_Config"
doc:name="Google Analytics Config" doc:id="9cb86789-1ca1-4eda-b007-e2ca4bd63a6b">
<google-analytics:connection jsonFilePath="${config.jsonPath}" applicationName="#${config.application_name}"/>
</google-analytics:config>
<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="6641e0fd-1048-470f-9e8b-f42a5e07310b" >
<http:listener-connection host="0.0.0.0" port="8081" />
</http:listener-config>
<flow name="generateReportFlow" doc:id="c71676e1-21db-4aab-9745-1bc945381b69" >
<http:listener doc:name="Listener" doc:id="7b3c8178-f8a1-48ea-95e7-2acfdb8ba7d6" config-ref="HTTP_Listener_config" path="/test"/>
<google-analytics:generate-report doc:name="Generate report" doc:id="7d1826c8-1465-43e4-b5ca-c126f2b57381" config-ref="Google_Analytics_Config" profileId="#['xxxxxxx']" startDate="#['2019-01-15']" endDate="#['2019-02-15']">
<google-analytics:metrix-parameters>
<google-analytics:metrics-parameter value="Sessions" />
<google-analytics:metrics-parameter value="Bounces" />
</google-analytics:metrix-parameters>
<google-analytics:dimension-parameters>
<google-analytics:dimension-parameter value="Country"/>
</google-analytics:dimension-parameters>
<google-analytics:filter-parameter>
<google-analytics:filter-params>
<google-analytics:filter-parameter-type key="Sessions" operator="GreaterThan" value="1" operation="AND" />
</google-analytics:filter-params>
</google-analytics:filter-parameter>
<google-analytics:segmentparameter >
<google-analytics:segmentparams >
<google-analytics:segment-parameter-type segmentKey="Users" segmentType="Condition" segmentFilter="Sessions" segmentOpertor="GreaterThan" segmentValue="1" />
</google-analytics:segmentparams>
</google-analytics:segmentparameter>
</google-analytics:generate-report>
</flow>
</mule>
-
Reference for : Google Analytics API
-
To contact team : Ksquare.