Search code examples
google-analyticspostmangoogle-analytics-apimetrics

How to pull the rt-source metric using google analytic API?


I am new to using APIs in general as well as using Google Analytics API. I am trying to call the google analytics traffic results from a website using google analytics API. When using the rt-source metric I get a missing parameter error. When I try to add the bounce rate as a parameter I get an unknown metric error. Please can someone advise what I am doing wrong or if the problem is at the client's side. See image for example of code. Traffic API code exampleThanks in advance!


Solution

  • If you check the documentation for realtime.get You will find that at least one metric is required

    enter image description here

    So as the error message says you need to add a metric. From what i can see you have only added a dimension

    curl \
      'https://analytics.googleapis.com/analytics/v3/data/realtime?ids=ga%3AUa-12445433&metrics=rt%3AactiveUsers' \
      --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
      --header 'Accept: application/json' \
      --compressed
    

    Consult the documentation for a full list of dimensions and metrics that can be used with the realtime api. You will find that bounce rate is not one of them.

    rt:source is valid though. try rt%3source remember it needs to be html encoded. Remember this is a dimension not a metric so must be sent in the dimension paramater

    curl \
      'https://analytics.googleapis.com/analytics/v3/data/realtime?ids=ga%XXXXXX&metrics=rt%3AactiveUsers&dimensions=rt%3Asource&key=[YOUR_API_KEY]' \
      --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
      --header 'Accept: application/json' \
      --compressed
    

    The real time report for traffic source by default uses rt:activeUsers Just look at the report its telling you what the metric is

    enter image description here