11

In javascript, I am trying to access analytics data for google shorten urls, for example. I tired 'URL Shortener API', which worked fine and I received data. But this data doesn't have analytics report for each hour in the day or for each day in the month, as its available on here. Here in response it have some properties for example 'clicks' and 'buckets' which contain the clicks count I need. Check the image below:

enter image description here

But these properties are not available in the data I received with the 'shortener API'. I might use Google analytics api for this purpose. Can anyone suggest me how can I use analytics api to get the analytics for any shorten url ?

Thanks

1 Answer 1

7
+25

Are you sure you're using the URL Shortener API correctly ?

If I check the example you provided which contains the data you need like reports for the past two hours (per hour does not exists) or past day, I can see for example:

  • 6 total clicks for the past two hours.
  • 1243 clicks for the past day.

If I try to get the same data for the same short url with the URL Shortener API:

curl -X "GET" "https://www.googleapis.com/urlshortener/v1/url?shortUrl=http://goo. gl/fbsS&projection=FULL&key=YOUR-API-KEY"

I'll get the same data:

{
  "kind": "urlshortener#url",
  "id": "http://goo. gl/fbsS",
  "longUrl": "http://www.google.com/",
  "status": "OK",
  "created": "2009-12-13T07:22:55.000+00:00",
  "analytics": {
    "allTime": /* ... */,
    "month": /* ... */,
    "day": {
      "shortUrlClicks": "1243",
      /* ... */,
    },
    "twoHours": {
      "shortUrlClicks": "6",
      /* ... */,
    }
  }
}

So I have 1243 clicks for the past day and 6 for the past two hours, the data are identical.

If you need to get all data from all time, you'll either have to store the data yourself or like you said use Google Analytics.

Google Analytics and short URLs can be pretty tricky to handle in Analytics because they redirect users from their website to your website which can cause Analytics to treat them as "direct" and not coming from any campaign you specified (newsletter, facebook, twitter, etc.).

You need to tag your URLs in order to properly track them. Usually, you'll need to use Google URL Builder to generate custom campaign parameters for your URLs.

There is no API for Google URL Builder but you can generate yourself valid URLs using the detailed informations provided on the previous link and append some or all of the parameters at the end of your non-short URLs like utm_source, utm_medium, utm_term, etc.

When your non-short URLs are properly tagged, you can then shorten them using any service you want.

To get the data back, you'll need to use the Google Analytics API and specifically the Reporting API.

Once authenticated,

var discoveryURL = 'https://analyticsreporting.googleapis.com/$discovery/rest?version=v4';

// Load the API
gapi.client.load(discoveryURL)
  .then(function() {
    // Returns Analytics data.
    gapi.client.analyticsreporting.reports.batchGet({
      "reportRequests": [
        {
          "viewId": VIEW_ID, 
          // View IDs can be fetched from the Analytics Account Explorer
          // https://ga-dev-tools.appspot.com/account-explorer/
          "dateRanges": [
            {
              "startDate": "7daysAgo",
              "endDate": "today"
            }
          ],
          "metrics": [
            {
              "expression": "ga:sessions"
            }
          ]
        }
      ]
    })
    .then(function(response) {
      var json = JSON.stringify(response.result, null, 2);

      // Do anything you want with the JSON returned.
    });
  });

The main function used here is batchGet and you can get every informations regarding the fields and options you can use on the Reporting API v4 reference.

You'll be able to toy with various fields like dates (DateRange), dimensions, etc. to get all the data you'll need in your application.

3
  • Yes, I am using it correctly. I am getting same set of data as you got. It doesnt get the data by date or by hours. It shows clicks for last 24 hours in "day" property, but not for last date or today. Lets say at 10am today by shorten api you will get clicks between 10am yesterday and 10am today. But It doesnt have clicks count individually for today, yesterday or any other day. Same applies for hours. But if you visit the shorten url analytics in the url i mentioned above, in the response it has data for each day, and each hour too.
    – shaaaa
    Jul 7, 2016 at 12:08
  • Ho sorry, didn't get that on the first read, I added more informations on my answer and an example to retrieve data from the Google Analytics API to fetch datas.
    – HiDeoo
    Jul 7, 2016 at 13:49
  • Hi, thanks for the update. However I am not able to use the information properly. Am I allowed to get analytics report for the websites that I dont own? To get a viewId, I added shorturl of google in my analytics, but I dont get any visitor report. I will appreciate if you can create an example on fiddle. Thanks :-)
    – shaaaa
    Jul 13, 2016 at 22:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.