---
# System prepended metadata

title: Setup Google Analytics for ckan instance

---

# Setup Google Analytics for ckan instance

## Google Analytics Account Creation
> Skip to [Setup ckanext-googleanalytics](#Setup-ckanext-googleanalytics) if there is already an existing account

### Create a GA account

[Create a Google analytics account](https://analytics.google.com/analytics/web/#/provision/create) at `https://analytics.google.com/`

* Enter the account name and click next. Accounts can contain more than one project or website. You can skip this step when adding to an existing account.
    ![](https://i.imgur.com/uv8FP19.png)
* Enter the property name. Properties represent each website or app that you intend to collect data for.
    ![](https://i.imgur.com/8FuOlvF.png)
* Select **`Show advanced options`** and enable `Create a Universal Analytics 4 property`. Enter the website url in the `Website URL` field under the Universal Analytics 4 section. and click next.
    > As at the time of writing this documentation, there is a warning that: `Universal Analytics properties will stop collecting data starting July 1, 2023. It’s recommended that you create a Google Analytics 4 property instead.` so the `Create both a Google Analytics 4 and a Universal Analytics property` option is selected.

    ![](https://i.imgur.com/vmKfLlB.png)
* Fill in your business information and click **`Create`**
    ![](https://i.imgur.com/rDKKvRB.png)


## Setup ckanext-googleanalytics

### Create a service account to access the analytics

> If there is already an existing service account with the generated `credentials.json` file you wish to use, you can skip to the [Give Service Account necessary permissions](#Give-Service-Account-necessary-permissions) section

* Go to the [Service Accounts console page in GCP](https://console.cloud.google.com/iam-admin/serviceaccounts) and select a project. If there is no existing project, you can create a project.
    > The project is arbitrary is simply a GCP requirement to create a service account.
* In the Service Accounts page, select **`+ CREATE SERVICE ACCOUNT`**
    ![](https://i.imgur.com/bA4ZkpC.png)
* Fill in the Service Account name and select **`DONE`**. (The name is also arbitrary)
    ![](https://i.imgur.com/orth21a.png)
* Select the service account just created and navigate to the **`KEYS`** tab.
    ![](https://i.imgur.com/GgYlBoz.png)
* Select the **`ADD KEY`** dropdown and `Create new key` option and select `JSON` as the key type
    ![](https://i.imgur.com/XqbcVe6.png)
* A `json` file is generated and downloaded to your pc.
    The contents of the json file should be similar to this:
    ```json
    {
      "type": "service_account",
      "project_id": "project_name",
      "private_key_id": "some_id",
      "private_key": "-----BEGIN PRIVATE KEY-----        something",
      "client_email": "test-service-account@datopian-ckan-test-project.iam.gserviceaccount.com",
      "client_id": "client_id",
      "auth_uri": "https://accounts.google.com/o/oauth2/auth",
      "token_uri": "https://oauth2.googleapis.com/token",
      "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
      "client_x509_cert_url": "some_url"
    }
    ```
* Rename the downloaded file to `credentials.json` and save this somewhere for use later.


### Give Service Account necessary permissions

* Go back to the google analytics console and select the admin settings. it should be a settings icon at the bottom left of the analytics page
    ![](https://i.imgur.com/GyoiQYl.png)
* Select the **`Account Access Management`** option under the *Account* section.
    ![](https://i.imgur.com/QmvY7jf.png)

* Select the **`+`** icon on the top left and select `Add User`
    ![](https://i.imgur.com/Lt6m5xP.png)

* Add the service account email and give `Administrator` access
    ![](https://i.imgur.com/hCatOkz.png)

* Now the credentials json file can be used in the portal as the `credentials.json file`. Deploy the `credentials.json file` gotten from the service account to the running instance. It is best to add it to the build so it is always available even after re-deployments.
* Ensure the [Google analytics API is enabled](https://console.developers.google.com/apis/api/analytics.googleapis.com/overview)

## Add ckanext-googleanalytics environment variables

The following variables **MUST** be added to the ckan instance's ini config file. Other googleanalytics variables are not mandatory for the extension to work:

* **`googleanalytics.id`**: This should be the property id for the property that was created for the site.
    > If the **`Create both a Google Analytics 4 and a Universal Analytics property`** option was selected when creating the property, then 2 properties would have been created. The first is the Google analytics 4 id which is all numbers and the *Universal Analytics* id which has a **`UA`** prefix. For example: **`UA-123456789-1`**. Note that the UA id should be used as the `googleanalytics.id` option.
* **`googleanalytics.account`**: This **MUST** correspond to the name of the google analytics account.