Integrating Google Analytics
The Google Analytics plugin collects data through Google Analytics from which Brightspot generates reports as described in Viewing analytics at the item level and Site-level analytics. When you integrate Brightspot with Google Analytics, you need to make configuration settings on your Google Analytics account as well as within Brightspot.
Configuring Google Analytics
To integrate Google Analytics with Brightspot, you must have a Google Analytics account and a Google API Service account, both of which generate values that you need to configure Brightspot.
Administrators perform these tasks.
Creating a Google Analytics account
Create a Google Analytics account as described in Get started with Analytics.
In your Google Analytics account, make the following settings:
Tracking ID, for example
UA-118898499-1
View ID, for example
174965383
Creating a Google API service account
Create a service account on the Google APIs site https://console.developers.google.com/apis/credentials.
Create credentials using a service account key that is a JSON key type. The key is downloaded as a JSON file.
Configuring a Brightspot site
If your version of Brightspot is missing this feature, contact your Brightspot representative.
From the Navigation Menu, select Sites & Settings.
In the Sites widget, select a site to integrate with Google Analytics, or select Global to integrate all your sites.
Under Front-End, expand Integrations.
Click |mi-add_circle_outline|, and select Google Analytics.
In the Tracking ID field, enter the tracking ID you assigned as described in the section Creating a Google Analytics account. The tracking ID generates a JavaScript snippet that is incorporated into your site pages. It enables tracking of your site by Google Analytics.
Still under Front-End, expand Google Analytics.
Using the following table as a reference, enter settings for the Google Analytics plugin.
Click Save.
Field | Description |
---|---|
Credentials | Private JSON key that was downloaded from the Google API service account as described in Creating a Google API service account. Copy the entire contents of the file into this field, including the opening and closing braces. |
View ID | ID of the view that was generated in your Google Analytics account as described in Creating a Google Analytics account. You can configure your account to have multiple views, each with a different set of data. |
Page Report | Integrates page views into Brightspot:
|
Site Reports | Type of reports to generate about traffic to your site:
|
Accessing reports
Brightspot uses the class GoogleAnalyticsProcessTask to retrieve data from the Google Analytics service using the configuration you make as described in Configuring a Brightspot site. When the GoogleAnalyticsProcessTask
is complete, it sends a GoogleAnalyticsProcessTask.TASK_COMPLETE
message to Brightspot. The message can be used to trigger further processing, such as recalculating indexed methods and retrieving report data.
The following snippet shows how a modification to a Page
can listen for TASK_COMPLETE
messages from GoogleAnalyticsProcessTask
and then perform a task.
import com.psddev.dari.db.Modification; import com.psddev.dari.db.Recordable; import com.psddev.dari.util.StringUtils; import com.psddev.google.analytics.GoogleAnalytics; import com.psddev.dari.util.ObjectUtils; @Recordable.FieldInternalNamePrefix("googleAnalytics.") 1 public class GoogleAnalyticsPageModification extends Modification<Page> { @Indexed 2 Date lastUpdate; @Override protected void receiveMessage(String key, Object... args) { 3 if (!StringUtils.isBlank(key) && key.startsWith(GoogleAnalytics.MESSAGE_REPORT_PAGE)) { if (!ObjectUtils.isBlank(args)) { Object endDate = args[0]; if (args[0] != null && args[0] instanceof Date) { Date newLastUpdate = (Date) args[0]; if (!ObjectUtils.equals(lastUpdate, newLastUpdate)) { lastUpdate = newLastUpdate; this.save(); } } } } } }
Prefixes the internal names of this page's fields with | |
Indexes the | |
Overrides the |
You can also use modifications to access Google report data for objects that you want to report on. Use the TimeSeries and TimeSeriesValues classes to specify the time scope of data to be retrieved and reported on.
The following example calculates total page views for the last 24 hours for Page
.
import com.psddev.cms.db.Site; import com.psddev.dari.db.Modification; import com.psddev.dari.db.Recordable; import com.psddev.dari.util.StringUtils; import com.psddev.google.analytics.GoogleAnalytics; import com.psddev.timeseries.TimeSeries; import com.psddev.timeseries.TimeSeriesValues; import com.psddev.dari.util.ObjectUtils; @Recordable.FieldInternalNamePrefix("googleAnalytics.") public class GoogleAnalyticsPageModification extends Modification<Page> { @Indexed Date lastUpdate; @Indexed 1 public float getPageViewsLast24Hours() { long pageViews = 0; 2 Site.ObjectModification siteModification = this.as(Site.ObjectModification.class); 3 if (siteModification != null) { if (siteModification.isGlobal()) { 4 TimeSeries pageViewTimeSeries = new TimeSeries(GoogleAnalytics.PAGE_VIEWS_TIME_SERIES_NAME); if (pageViewTimeSeries != null) { pageViews += TimeSeriesValues.ofHours(24, getId(), pageViewTimeSeries).sum(); } } Site owner = siteModification.getOwner(); 5 if (owner != null) { TimeSeries pageViewTimeSeries = new TimeSeries(GoogleAnalytics.PAGE_VIEWS_TIME_SERIES_NAME + "." + owner.getId()); if (pageViewTimeSeries != null) { pageViews += TimeSeriesValues.ofHours(24, getId(), pageViewTimeSeries).sum(); } } for (Site site : siteModification.getConsumers()) { 6 TimeSeries pageViewTimeSeries = new TimeSeries(GoogleAnalytics.PAGE_VIEWS_TIME_SERIES_NAME + "." + site.getId()); if (pageViewTimeSeries != null) { pageViews += TimeSeriesValues.ofHours(24, getId(), pageViewTimeSeries).sum(); } } } return pageViews; 7 } }
Indexes the method | |
Initializes the | |
Links the | |
Increments the | |
Increments the | |
Increments the | |
Returns the counter, reflecting the cumulative number of page views across all of the sites. |