Google Analytics for short urls - javascript

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:
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.

Related

YouTube Analytics API not returning demographic data for channel owner

I am using YouTube Analytics API with oauth2.0 authorization to get channel owner demographic(ageGroup and gender).
It does not return any values for the ageGroup and gender. The login(authentication) part works and GAPI loads perfectly as well, but, no values are returned (even though channel analytics show agegroup and gender).
Surprisingly, when I use the basic dimensions (views, likes, comments etc.), it returns the values properly.
Here is how the response look like:
This is the code:
function execute() {
return gapi.client.youtubeAnalytics.reports.query({
"ids": "channel==MINE",
"startDate": "2019-01-01",
"endDate": "2022-03-27",
"metrics": "viewerPercentage",
"dimensions": "ageGroup,gender",
"filters": "subscribedStatus==SUBSCRIBED",
"sort": "ageGroup,gender"
})
.then(function(response) {
console.log("Response", response);
},
function(err) { console.error("Execute error", err); });
}
That's because not enough data:
Understand limited data in YouTube Analytics
We want to give creators
useful data to make informed decisions about their channel, but some
data may be limited in YouTube Analytics.
Note: If your video or channel doesn’t have enough traffic during a selected time period, you may not see your data. If you've selected a specific filter, such as geography or gender, you may also not see data
If you want to see more, try extending the time period, or removing filters and breakdowns. Changing these settings may increase the amount of data in the full report.
Types of limited data The following info may be limited:
Demographics data
Audience data
Geography data
Search terms and URLs
source: https://support.google.com/youtube/answer/9101241?hl=en
related post: YouTube Analytics API - Demographics Minimum Views Seem to Be Required for Query

How to get all google reviews using business api

I need all google reviews for particular location but I am unable to use business api from google. Here is url for get request
https://mybusiness.googleapis.com/v3/accounts/account_name/locations/location_name/reviews
Now my question is what is the value for param account_name and location_name
How can I get that.
Please answer with sample location example
I think first of all you need to white list your google my business api for whatever project you are working on in your project as its private api. Google my business api will work on the locations associated with your account so make sure you verified the LOCATIONS from any account you know. Then you can try out the api call you mentioned in OAuthplayground.
Follow steps mentioned in below documentation URL to set it up:
https://developers.google.com/my-business/content/prereqs
After the setup and etc you will automatically understand the account id and location id.
Also few more urls you can go to understand it better.
https://console.developers.google.com (here you will setup your project)
https://business.google.com/manage (here you will add/can see the locations - for which you need reviews)
https://developers.google.com/my-business/content/basic-setup (Steps after completing the prereq)
https://developers.google.com/oauthplayground (You will test the my
business api here after approval)
When you make a request to https://mybusiness.googleapis.com/v3/accounts it gives you a list of accounts. On those accounts they have a field called name. That field is accounts/account_name.
{
"state": {
"status": "UNVERIFIED"
},
"type": "PERSONAL",
"name": "accounts/1337",
"accountName": "example"
}
When you make a request to https://mybusiness.googleapis.com/v3/accounts/account_name/locations it gives you a list of locations. On those locations they have a field called name. That field is accounts/account_name/locations/location_name.
{
"locations": [
{
"languageCode": "en",
"openInfo": {
"status": "OPEN",
"canReopen": true
},
"name": "accounts/1337/locations/13161337",
...
}

Get list of circles of a user using Google Plus API

I am using google aouth with following in scope
scope : ['https://www.googleapis.com/auth/tasks', 'https://www.googleapis.com/auth/tasks.readonly', 'https://www.googleapis.com/auth/userinfo.profile', 'https://www.googleapis.com/auth/plus.me', 'https://www.googleapis.com/auth/plus.circles.read']
I tried to get information about user, I've used following API
var xhr = Ti.Network.createHTTPClient({
onload : function() {
var data = this.responseText;
var json = JSON.parse(data);
Ti.API.log('json: ' + JSON.stringify(json));
}
});
xhr.open("GET", "https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=" + googleAuth.getAccessToken());
xhr.send();
but it only gives information about users basic info, how can I get information about users friends in circle.
I replaced xhr.open("GET", "https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=" + googleAuth.getAccessToken()); with
xhr.open("GET", "https://www.googleapis.com/plusDomains/v1/people/me/circles?access_token=" + googleAuth.getAccessToken());
the responce text in console was like
json: {"error":{"errors":[{"domain":"global","reason":"forbidden","message":"Forbidden"}],"code":403,"message":"Forbidden"}}
I have enabled
Google+ API,
Blogger API,
Google+ Pages API,
Google+ Domains API,
The closest thing to this that the API has is People.list
https://www.googleapis.com/plus/v1/people/me/people/connected?key={YOUR_API_KEY}
Try testing the collection see which one suits your needs more. You can test the results at the bottom of the page. There is no way to get the name of the circle back.
Acceptable values are:
"connected": The list of visible people in the authenticated user's
circles who also use the requesting app. This list is limited to users
who made their app activities visible to the authenticated user.
"visible": The list of people who this user has added to one or more
circles, limited to the circles visible to the requesting application.
As of August 2018 the Google+ API endpoint https://www.googleapis.com/plus/v1/people/userId/people/collection is deprecated.
There is a new endpoint for getting all the contacts: https://people.googleapis.com/v1/people/me/connections.
There is a metadata key in the response, and for Google+ contacts it will look somewhat like this:
"metadata": {
"sources": [
{
"updateTime": "2013-01-13T19:16:50.668Z",
"etag": "...",
"type": "CONTACT",
"id": "..."
},
{
"etag": "...",
"type": "PROFILE",
"id": "...",
"profileMetadata": {
"userTypes": [
"GOOGLE_USER",
"GPLUS_USER"
],
"objectType": "PERSON"
}
}
],
"objectType": "PERSON"
}
Note the "GPLUS_USER" part.

fetch facebook user's friends with javascript

I am a new comer of StackOverFlow. As title, I 'd like to pick a subset of the user's friends, and I have tried that in the Graph API Explorer with → /v2.1/ me?fields=friends. However it return an empty json named data and the total count of the user's friends. What did I miss?? Below is my test in the facebook Graph API explorer.. ! Here comes the result in the graph api explorer.
{
"friends": {
"data": [
],
"summary": {
"total_count": 15
}
},
"id": "585768444863088"
}
You can also just use /me/friends instead, but the empty array is correct. You only get those friends who authorized your App too, you don´t get every friend anymore since end of April 2014 (v2.0 of the Graph API).
See here: https://developers.facebook.com/docs/apps/upgrading#upgrading_v2_0_user_ids
/me/friends returns the user's friends who are also using your app
There are ways to get the whole friendlist like invitable_friends and https://developers.facebook.com/docs/graph-api/reference/v2.1/user/taggable_friends, but those endpoints are supposed to get used for very specific tasks (for inviting in games and for tagging).

Getting like and comment counts per post of facebook page by graph api version 2

I have been getting like and comment counts per post of facebook page/group feed call by graph api separately using FQL but as version 2 of graph api released fql no longer working to serve purpose.
So i have to find new ways to get comment and like counts per post of page feed display. I will make a separate call to get comment and like counts per post of the fb page as it may not be possible to get things in same page feed call(or it is?).
So, searching through google, i found following way using graph api call -
..page_id/feed?fields=likes.limit(1).summary(true){id},comments.limit(1).summary(true)&limit=10
Is this the best and error free way?? Also besides id and summary fields i also get created_time, paging, likes data by the above call which is unexpected and redundant, how do i exclude these additional fields?
So please any FB employee show me light on what is the best way to retrieve like and comment count per post of page/group feed using graph api version 2.
If you want to retrieve Likes and comments count of a post on FB you can achieve this by using Id of the post Like this
..Your_Post_ID?fields=likes.limit(0).summary(true),comments.limit(0).summary(true)
the result will contain
Total numbers of likes of the post, total numbers of comments of the post, post ID and post created time.
The result will be like this
{
"likes": {
"data": [
],
"summary": {
"total_count": 550
}
},
"comments": {
"data": [
],
"summary": {
"order": "chronological",
"total_count": 858
}
},
"created_time": "2014-10-12T05:38:48+0000",
"id": "Your_Post_ID"
}

Categories