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:
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
Simply visit goo.gl, sign-in and then create a shortened link by pasting your target URL into the box and clicking the SHORTEN URL button. This will generate your shortened link and add it to your library of previous ones.
There are a number of ways you can reveal the full URL behind a shortened URL: Use the shortening service preview feature. Type the shortened URL in the address bar of your web browser and add the characters described below to see a preview of the full URL: tinyurl.com.
Bitly works as a layer on top of Google Analytics. We offer a quick way for businesses and individuals of all analytical skill levels to pull real-time data.
The shorter the better. According to Backlinko, “Shorter URLs tend to rank better than long URLs.” To prove this, they performed some extensive testing on one million Google search results. Here's a graph that shows how Google rankings decline as URL length gets longer.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With