# Ontoportal Google analytics configuration  

:::warning
You need to have at least version  [5.26.0](https://github.com/ncbo/ncbo_cron/releases/tag/v5.26.0)  of NBCO CRON to be compatible with Google Analytics v4

:::

## 1 - Get Google Analytics Keys  JSON file

Google Cloud requires **Service Account Credentials** to connect to the APIs. You will use the **JSON key file** to connect to most services with google-analytics-data.

If you are not running this client within [Google Cloud Platform environments](https://cloud.google.com/ruby/docs/reference/google-analytics-data/latest/AUTHENTICATION#google-cloud-platform-environments), you need a Google Developers service account.


1. Visit the [Google Cloud Console](https://console.cloud.google.com/project?_ga=2.132195598.1445660687.1703532941-1233407111.1702507818).
2. Create a new project or click on an existing project.
3. Activate the menu in the upper left and select **APIs & Services**. From here, you will enable the APIs that your application requires.

   *Note: You may need to enable billing to use these services.*
4. Select **Credentials** from the side navigation.

   Find the "Create credentials" drop-down near the top of the page, and select "Service account" to be guided through downloading a new JSON key file.

   You can easily generate a new key file if you want to re-use an existing service account. Select the account you wish to re-use, click the pencil tool on the right side to edit the service account, select the **Keys** tab, and then select **Add Key**.

   This library will use the key file you download to authenticate API requests and should be stored in a secure location.


:::info
Source: <https://cloud.google.com/ruby/docs/reference/google-analytics-data/latest/AUTHENTICATION#creating-a-service-account>

:::


## 2- Configure NCBO CRON

Modify  `ncbo_cron/config/config.rb`


```javascript

# Google Analytics CRON config
config.enable_ontology_analytics         = true
config.cron_ontology_analytics           = '30 */4 * * *'
# GA4 Key file used for authentication with Google
config.analytics_path_to_key_file        = "config/myontoportal_analytics_key.json"
# your GA4 property ID    
config.analytics_property_id             = "111111111"
# path to the file that stores your Universal Analytics (UA) data (if exists)
config.analytics_path_to_ua_data_file    = "data/op_ua_data.json"
# path to the file that will store your entire Google Analytics Data
# it's a backup copy of your data in addtion to storing it in Redis
# this file will be auto-created if it doesn't exist already
config.analytics_path_to_ga_data_file    = "data/op_ga_data.json"
```


### Importing Existing Universal Analytics (UA) Data


:::warning
The following block is optional and **available till July 1st, 2024.** it only concerns  appliances  already  having the old Google Analytics version ( called Universal Analytics) and wanting to import there data to the new version of Google Analytics

:::

If you have existing Google Analytics data that you have been collecting till now, you can import it from Google (available till July 1st, 2024) so it’s merged with your Google Analytics Data (GA4) data and displayed in OntoPortal.

Make sure your Universal Analytics configuration is set up properly

```javascript
config.analytics_service_account_email_address = 'myontoportal@developer.gserviceaccount.com'
config.analytics_path_to_key_file              = 'config/myontoportal-analytics.p12'
config.analytics_profile_id                    = 'ga:1234567'
config.analytics_app_name                      = 'MyOntoPortal'
config.analytics_app_version                   = '1.0.0'
config.analytics_start_date                    = '2020-01-01'
config.analytics_filter_str                    = '' 
```

  
Run the following script:

```javascript
sudo su - ontoportal
cd /srv/ontoportal/ncbo_cron
bundle exec ruby ./bin/generate_ua_analytics_file.rb
```

  
At the end of the run, the script will generate the file that you specified in:

```javascript
config.analytics_path_to_ua_data_file    = "data/op_ua_data.json"
```

This file needs to be generated ONLY ONCE, and it will be used by your Google Analytics CRON job to merge the “old” data with the new data from GA4. Once this file is generated, you can safely delete the section of the configuration related to Universal Analytics.


## 3- Enabling Analytics Cron Job

The Analytics data is stored in a `Redis` data store and is scheduled to be refreshed weekly. The refresh is done via an automatically scheduled Cron job. By **default, that Cron job is disabled** in the OntoPortal Appliance.

Below are the steps to enable the Analytics refresh job:


* Restart ncbo_cron services:

```javascript
sudo systemctl stop ncbo_cron
sudo systemctl start ncbo_cron
```

* By default, the Analytics refresh job is scheduled to run on Mondays at 12:15 am. If you need to change the frequency or the run time, you can add the parameter: `config.cron_ontology_analytics = "your cron expression"` to the `NcboCron.config do |config|` block:

```javascript
config.cron_ontology_analytics = "15 0 * * 1" 
```

  
If you modify this setting, be sure to restart ncbo_cron services (see previous step).

If you need to manually refresh the Analytics data without having to wait for the Cron job to execute, you can run the ncbo_ontology_analytics_rebuild script, located in the ncbo_cron/bin folder:


```javascript
sudo su - ontoportal

cd /srv/ontoportal/ncbo_cron

./bin/ncbo_ontology_analytics_rebuild
```

  
The script does not require any arguments; simply execute it, and your Analytics data will get pulled from Google Analytics into the Redis datastore and will be immediately picked up by your OntoPortal services. Note that this step is optional. It should be used only when you need the absolute latest Analytics data, and you don’t want to wait until the Analytics Cron job gets executed.


  
  

### How Ontoportal is using  GA

 ![](https://wiki.agroportal.eu/api/files.get?sig=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJrZXkiOiJ1cGxvYWRzL2MyODRmYzA0LTA4ZmUtNGE1Yi05ODM4LWM5MjhmYTFlNjhjNi9jOTI5MjYxNS05NmNmLTRlODUtOWU1Yy0xMTJiYzU0MmYyOTMvaW1hZ2UucG5nIiwidHlwZSI6ImF0dGFjaG1lbnQiLCJpYXQiOjE3OTM4OTQ0MDAsImV4cCI6MTc5Mzk4MDgwMH0.RWRnV_dTKb7zCKz_jQf4hnnbGvXwjwldzTx0hwfiC8Y " =757x718")

---

**Documents**

- [💁‍♂️ Acknowledgments](https://wiki.agroportal.eu/s/e6158eda-c109-4385-852c-51a42de9a412/doc/acknowledgments-15GdRXLQdm)
- [📚Publications and references](https://wiki.agroportal.eu/s/e6158eda-c109-4385-852c-51a42de9a412/doc/publications-and-references-87tEoeoGKy)
- [🗽Terms & Conditions](https://wiki.agroportal.eu/s/e6158eda-c109-4385-852c-51a42de9a412/doc/terms-conditions-naDsDo2Zxq)
- [🧞‍♂️Metadata guidelines and recommendations](https://wiki.agroportal.eu/s/e6158eda-c109-4385-852c-51a42de9a412/doc/metadata-guidelines-and-recommendations-pOFbK9sIfp)
- [📜 AgroPortal Release Notes](https://wiki.agroportal.eu/s/e6158eda-c109-4385-852c-51a42de9a412/doc/agroportal-release-notes-btKjZk5tU2)
- [ 👩‍🚀 User Guide](https://wiki.agroportal.eu/s/e6158eda-c109-4385-852c-51a42de9a412/doc/user-guide-ZszoBwfjGF)
- [📎 API Documentation](https://wiki.agroportal.eu/s/e6158eda-c109-4385-852c-51a42de9a412/doc/api-documentation-kMCurGKD97)
- [🤖 Developer Documentation](https://wiki.agroportal.eu/s/e6158eda-c109-4385-852c-51a42de9a412/doc/developer-documentation-LhsrucqGTa)
- [🗃External Documentation](https://wiki.agroportal.eu/s/e6158eda-c109-4385-852c-51a42de9a412/doc/external-documentation-vNPQCdArrr)
- [AgroPortal infrastructure](https://wiki.agroportal.eu/s/e6158eda-c109-4385-852c-51a42de9a412/doc/agroportal-infrastructure-ietsAU4kPs)