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

Administrators perform these tasks.

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.

Creating a Google Analytics account

  1. Create a Google Analytics account as described in Get started with Analytics.
  2. 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

  1. Create a service account on the Google APIs site https://console.developers.google.com/apis/credentials.
  2. 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 any of the following configuration options, contact your Brightspot developer.

To configure Brightspot to retrieve Google Analytics:

  1. From the Navigation menu, select Admin > Sites & Settings.

  2. In the Sites widget, select a site to integrate with Google Analytics, or select Global to integrate all your sites.

  3. Under Front-End, expand Integrations.

  4. Click add_circle_outline, and select Google Analytics.

    ../../../_images/google-analytics-integrations.png
  5. 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.

  6. Still under Front-End, expand Google Analytics.

    ../../../_images/google-analytics-credentials.png
  7. Using the following table as a reference, enter settings for the Google Analytics plugin.

  8. Click Save.

Google Analytics Settings—Front-End tab
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:

  • Device categories—Indicates types of devices visitors are using on your site.
  • Demographics—Provides gender and age brackets.
  • Referrals—Indicates how visitors arrive at your site, such as referral traffic from other sites or clicking links in search engines.
  • Visits—Provides general information about site visits, such as number of sessions, users, and session duration.

Accessing reports

Developers perform this task.

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package brightspot.core.page;
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;
import java.util.Date;

@Recordable.FieldInternalNamePrefix("googleAnalytics.")
public class GoogleAnalyticsPageModification extends Modification<Page> {

   @Indexed
   Date lastUpdate;

   @Override
   protected void receiveMessage(String key, Object... args) {
      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();
                 }
             }
         }
     }
   }
}

In the previous snippet—

  • Line 9 uses the FieldInternalNamePrefix annotation to prefix the internal names of the fields with googleAnalytics.
  • Line 12 indexes the lastUpdate field, which contains the date that analytic data was last collected for the page.
  • Lines 15–31 override the receiveMessage method, which listens for the TASK_COMPLETE message from GoogleAnalyticsProcessTask. The method compares the passed-in date to the date last saved on the page. If the passed-in date is newer than the saved date, then the method saves the passed-in date.

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package brightspot.core.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;
import java.util.Date;

@Recordable.FieldInternalNamePrefix("googleAnalytics.")
public class GoogleAnalyticsPageModification extends Modification<Page> {

   @Indexed
   Date lastUpdate;

   @Indexed
   public float getPageViewsLast24Hours() {
      long pageViews = 0;

      Site.ObjectModification siteModification = this.as(Site.ObjectModification.class);

      if (siteModification != null) {
         if (siteModification.isGlobal()) {

             TimeSeries pageViewTimeSeries = new TimeSeries(GoogleAnalytics.PAGE_VIEWS_TIME_SERIES_NAME);

             if (pageViewTimeSeries != null) {
                 pageViews += TimeSeriesValues.ofHours(24, getId(), pageViewTimeSeries).sum();
             }
         }

         Site owner = siteModification.getOwner();
         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()) {

             TimeSeries pageViewTimeSeries = new TimeSeries(GoogleAnalytics.PAGE_VIEWS_TIME_SERIES_NAME + "." + site.getId());
             if (pageViewTimeSeries != null) {
                 pageViews += TimeSeriesValues.ofHours(24, getId(), pageViewTimeSeries).sum();
             }
         }
      }
      return pageViews;
   }
}

In the previous snippet—

  • Line 19 indexes the method getPageViewsLast24Hours. When RecalculationTask runs, the page views for the page instance is updated.
  • Line 21 initializes the pageViews counter.
  • Line 23 links the Page state to an instance of Site.ObjectModification.class, enabling the following code to get the number of page views for all of the sites that have access to the page: global, the owner site, and any other sites that access to the page.
  • Lines 26–33 increment the pageViews counter with the number of page views for the Global site.
  • Lines 35–41 increment the pageViews counter with the number of page views for the owner site.
  • Lines 43–49 increment the pageViews counter with the number of page views for all other sites with access to the page.
  • Line 51 returns the counter, reflecting the cumulative number of page views across all of the sites.

See also: