Enrich your records with Google Analytics data
Learn how to add Google Analytics data to your records for a better custom ranking.
You can use data from Google Analytics to enrich your records, and boost search results based on their popularity or any other metric available in Google Analytics. This can help to improve the relevance of your search.
Because both Algolia and GA are API-based platforms, you can create scripts that update Algolia records to include any data from Google Analytics.
This guide creates a script that fetches the ga:uniquePageViews
metric from Google Analytics, and add it to a pageviews
attribute in your Algolia records.
If you use the Crawler, you can link your Google Analytics account to it to enrich your records with analytics data faster and easier.
Google credentials
The first step is to get the credentials to use the Google Analytics API. Google’s APIs can be authenticated in many ways but this guide uses service accounts. These are specifically meant for server-to-server applications.
Create a service account
First, create a service account through Google API Console.
- Create a project (or select an existing one) from Google API Console.
- Activate the Analytics Reporting API in that project.
- In the Credentials section, create a new service account. You can skip the optional steps.
- Open the service account and click Add key -> Create new key. Select JSON, and download the resulting JSON file.
Grant access to Google Analytics data
Now, an administrator of your Google Analytics account has to provide read access to your service account. To do this, follow these steps as an administrator:
- Log in to Google Analytics.
- Select the account, property, and view that contain the analytics of your website.
- Go to the Admin tab.
- In the View panel (on the right side of the screen), click View User Management.
- Click the + button, then click Add users.
- Enter the email address of the service account that was generated when creating the service account.
- In the Permissions panel, make sure that only the “Read & Analyze” permission is enabled.
- Click the Add button to confirm.
Get the view ID
To keep the script short, the view ID of your Google Analytics account will be manually retrieved.
- Return to the Admin tab of your Google Analytics View and click View Settings.
- Copy the View ID number. You have to put this in the
GA_PARAMETERS
object of the script later on.
Prepare your records
The only constraint for this guide, is that your records must have an attribute that contains the full URL of their associated page.
By default, the script expects the url
attribute to hold this, but you can change that if needed.
Create the script
Now that you have your credentials and your records ready, you can create a script to fetch the Google Analytics data of your view and inject them into existing records. For that, use:
Fetch Google Analytics data
To fetch the Google Analytics (GA) data of your view, use the Google API endpoint batchGet
method.
The script specifies the following parameters for this method:
- The
viewId
. - The
dateRanges
to specify the period you want to fetch the data for. - The
dimensions
: you need thega:hostname
andga:pagePath
to rebuild full URL of the page. - The
metrics
that you want to use in your custom ranking strategy. You can choose any metrics from the complete list of available metrics. orderBys
: In the example, pages are ordered byuniquePageViews
.pageSize
andpageToken
are used for pagination.batchGet
can only return a maximum of 100,000 rows. To fetch more rows, you need to use pagination.
In the example, GA data fetching is implemented by a MetricsFetcher
class, which has two methods:
- A
next()
method, which:- Performs calls to
batchGet
, - Keeps track of the pagination cursor,
- Transforms the complex GA response into simple JSON objects.
- Performs calls to
- A
fetchAll()
method, which:- Iterates over the
next()
method, until the requested number of records are fetched, - Builds a big JSON object with the full URLs as keys. This will be useful in the second step of the script, where the analytics data associated to a specific URL is retrieved.
- Iterates over the
The MetricsFetcher
also handles the authentication with the Service Account JSON file you’ve downloaded during the creation of your Google Service Account.
Update Algolia records
To update records, use the browse
and partialUpdateObjects
methods.
Then browse the records one by one to see if the URL the record belongs to has any GA data.
If so, create a partial record with new fields containing the GA metrics.
When the whole index has been browsed, call a partialUpdateObjects
with the partial records.
Finalize the script
You’re using the Node.js clients of both API clients to build the final script.
The main
function:
- Fetch the GA data with the
MetricsFetcher
- Updates the Algolia records using
browse
andpartialUpdateObjects
.
Note: You must update the variables in the // Script parameters
section of the final script:
APP_ID
,API_KEY
andINDEX_NAME
are your Algolia credentials.URL_ATTRIBUTE
is the name of the attribute in your Algolia records that contain the URL.- The
GA_PARAMETERS
object contains all parameters related to GA. You must include yourviewId
, but you can add and change other parameters as well.
With the script on your machine, you can run it by running the following command (assuming the script is named ga_connector.js
). The path/to/your-service-account-file.json
file has to point the JSON file downloaded when you created a Service Account: