Google Analytics for short urls
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
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.
链接地址: http://www.djcxy.com/p/68128.html