I'm looking for some examples of how to create a custom metric with the Google Analytics sensor.

Google Analytics Sensor https://www.paessler.com/manuals/prtg/google_analytics_v2_sensor

There are some other KB's that point to the GA Dimensions & Metrics API https://developers.google.com/analytics/devguides/reporting/core/dimsmets

And there is the GA Realtime Reporting API https://developers.google.com/analytics/devguides/reporting/realtime/v3/

The standard values are easy enough to add, i.e. users/sessions etc, but, custom examples are harder to find. Specifically, I'm looking for the following two custom metrics: Active Users on the website Active Users on a specific page


Article Comments

I found what I believe to be the metric desired for Active Users. Link: https://developers.google.com/analytics/devguides/reporting/realtime/v3/reference/data/realtime/get Metric: rt:activeUsers

Though not sure if PRTG works with GA's Realtime Reporting API?


Feb, 2019 - Permalink

Tried to see if I could enter the Realtime metric, but, it automatically looks for metrics starting with "ga:"

Received the following warning on the GA sensor added: "Invalid dimension or metric: ga:rt:activeUsers"

Again, not sure PRTG works with GA Realtime Reporting API at present.


Feb, 2019 - Permalink

The other possible option is to use the Python Script Advanced Sensor https://www.paessler.com/manuals/prtg/python_script_advanced_sensor

Now to determine if this will generate output in the format PRTG requires.


Feb, 2019 - Permalink

Hi there,

I guess "Invalid dimension or metric: ga:rt:activeUsers" is a valid error as there is indeed no dimension or metric with this name as far as I can tell, see here.

Kind regards,

Erhard


Feb, 2019 - Permalink

Yes that does appear to be a valid error. Though I did not add the "ga:", it was automatically added by PRTG. Which leads me to believe that the sensor only works with the Core API and not the Realtime Reporting API.

I'm looking into creating a python script to pull that data. Will update with any progress.


Feb, 2019 - Permalink

Found several independent resources providing examples of how to leverage Python to access the Google Analytics Realtime Reporting API. The following appears to be the most concise. Working through, but, wanted to post so as to not leave ticket "abandoned". Will update with details if/when I get working for reference.

https://github.com/sfleming92/python-realtime-gapi


Feb, 2019 - Permalink

Circling back, I was able to write the Python script to pull Realtime Reporting API data from Google Analytics ... locally. I found some of the initial challenges were in utilizing the current Google Analytics API's correctly. As a number of the examples I found online referenced deprecated API's etc.

Here are a few of the resources that ultimately provided the most help: Google API Client Libraries - Python https://developers.google.com/api-client-library/python/

OAuth2 Service Account (setting up a Service Account) https://developers.google.com/identity/protocols/OAuth2ServiceAccount

Google Analytics API v3 Explorer https://developers.google.com/apis-explorer/#p/analytics/v3/analytics.data.realtime.get

Here's the script that I've gotten to work (on a locally installed Python):

# Python script to pull Active User count on website from Google Analytics via Realtime Reporting API
from google.oauth2 import service_account
from googleapiclient.discovery import build

API_NAME = 'analytics'
API_VERSION = 'v3'
ACCOUNT_EMAIL = '<svc-account>@<project>.iam.gserviceaccount.com' #generated from Google Developer
KEYFILE = './client_secrets.json' #downloaded from Google Developer
ANALYTICS_VIEW_ID = 'ga:<#########>' #gathered from GA account
SCOPES = ['https://www.googleapis.com/auth/analytics.readonly']
RT_ACTIVE_USERS = 'rt:activeUsers'
RT_DIMENSIONS = 'rt:medium'

credentials = service_account.Credentials.from_service_account_file(KEYFILE, scopes=SCOPES)
service = build(API_NAME, API_VERSION, credentials=credentials)

results = service.data().realtime().get(ids=ANALYTICS_VIEW_ID, metrics=RT_ACTIVE_USERS, dimensions=RT_DIMENSIONS).execute()
totals = results.get('totalsForAllResults')
active_users = int(totals.get("rt:activeUsers"))
print str(active_users)

Next challenge/step - edit script to use json and paepy modules and configure Python on PRTG probe to add Google APIs
Python Sensor Example -> C:\Program Files (x86)\PRTG Network Monitor\Custom Sensor\python\sensor_example.py
Add Google Module -> C:\Program Files (x86)\PRTG Network Monitor\Python34\Lib

Viewing the example python script sensor provided in PRTG install shows:
# -*- coding: utf-8 -*-

import sys
import json
# get CustomSensorResult from paepy package
from paepy.ChannelDefinition import CustomSensorResult

if __name__ == "__main__":
    # interpret first command line parameter as json object
    data = json.loads(sys.argv[1])

    # create sensor result
    result = CustomSensorResult("This sensor is added to " + data['host'])

    # add primary channel
    result.add_channel(channel_name="Percentage", unit="Percent", value=87, is_float=False, primary_channel=True,
                       is_limit_mode=True, limit_min_error=10, limit_max_error=90,
                       limit_error_msg="Percentage too high")
    # add additional channel
    result.add_channel(channel_name="Response Time", unit="TimeResponse", value="4711")
    # print sensor result to std
    print(result.get_json_result())

Mar, 2019 - Permalink

Great job, KryPRTGadmin. Thank you for sharing this.


Mar, 2019 - Permalink