Reading un-sampled reports in Google Analytics using Management API - php

Accessing un-sampled reports requires the Report Id as input.
How do I list ids of all the un-sampled reports?

Doing an Unsampledreport.list will return a list of the unsampled reports.
GET https://www.googleapis.com/analytics/v3/management/accounts/accountId/webproperties/webPropertyId/profiles/profileId/unsampledReports
{
"kind": "analytics#unsampledReports",
"username": string,
"totalResults": integer,
"startIndex": integer,
"itemsPerPage": integer,
"previousLink": string,
"nextLink": string,
"items": [
management.unsampledReports Resource
]
}

Related

How do I check if a channel exists on YouTube?

If I make a curl request for this URL:
https://www.googleapis.com/youtube/v3/channels?part=snippet%2CcontentDetails%2Cstatistics&id=UC_x5XG1OVP6uZZ5FSM9Ttw&key=...
I'll get the output as:
{
"kind": "youtube#channelListResponse",
"etag": "J801W-IQ15sDpy3GjDfjlUgoVxA",
"pageInfo": {
"resultsPerPage": 0
}
}
Does this mean that the YouTube channel doesn't exist?
I don't get any error; how to find whether this is a valid channel or not?
Likewise, I want the list of videos of a given channel. If I make a curl request using this URL:
https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UCIJIhBwx4XjNUtQGZTGSVnA&maxResults=20&order=date&key=[YOUR_API_KEY]
I'll get the output as:
{
"kind": "youtube#searchListResponse",
"etag": "q5r0QewUnrg2C7BdwuxbJxb9b8c",
"regionCode": "IN",
"pageInfo": {
"totalResults": 0,
"resultsPerPage": 20
},
"items": []
}
This is a valid channel ID, but I get an empty result, not an error; how to know the search is valid or not?
Question no. 1
For what concerns your first question:
Given a channel ID -- $CHANNEL_ID --, test whether the respective channel exists or not.
I would recommend invoking curl on the following URL:
https://www.googleapis.com/youtube/v3/channels?part=id&fields=items/id&id=$CHANNEL_ID&key=$APP_KEY
Note that invoking the Channels.list endpoint through the URL above -- which contains the parameters part=id and fields=items/id -- will return only the channel's ID.
Although not documented explicitly, tests show that you'll get back from the endpoint the ID you passed on to it, if and only if that channel does actually exist.
For example, in case of your channel ID above -- UC_x5XG1OVP6uZZ5FSM9Ttw --, the API response is trivially empty:
{}
becase this channel does not exist (only have to click on this link to see that yourself).
On the other hand, in case of NBCNews' channel -- UCeY0bbntWzzVIaj2z3QigXg -- the response is:
{
"items": [
{
"id": "UCeY0bbntWzzVIaj2z3QigXg"
}
]
}
showing that indeed this channel is live and kicking.
Question no. 2
For what concerns the second question of your post:
Given a channel by its ID $CHANNEL_ID, do list the videos of that channel.
I recommend you to consult the answer I gave recently to this very question.
In terms of curl, you'll have to invoke the following URL:
https://www.googleapis.com/youtube/v3/channels?part=contentDetails&fields=items/contentDetails/relatedPlaylists/uploads&id=$CHANNEL_ID&key=$APP_KEY
for to obtain that channel's uploads playlist ID. For example, in the case of NBCNews' channel, the API response is:
{
"items": [
{
"contentDetails": {
"relatedPlaylists": {
"uploads": "UUeY0bbntWzzVIaj2z3QigXg"
}
}
}
]
}
Then take out that ID from the JSON response as $PLAYLIST_ID and invoke curl on the following URL repeatedly, implementing pagination:
https://www.googleapis.com/youtube/v3/playlistItems?part=id,snippet,contentDetails,status&maxResults=50&playlistId=$PLAYLIST_ID&key=$APP_KEY.
In case of NBCNews' uploads playlist, the output of the first page would look like:
{
"kind": "youtube#playlistItemListResponse",
"etag": "5DW9uT73DWmJtDoJ-rSw3AqHKpc",
"nextPageToken": "CAUQAA",
"items": [
{
"kind": "youtube#playlistItem",
"etag": "_X3LvLIRvEBM3RetizOGtB03ja0",
"id": "VVVlWTBiYm50V3p6VklhajJ6M1FpZ1hnLjE5NjU4N0NGQkY5M0M3MjI=",
"snippet": {
"publishedAt": "2020-09-12T06:11:59Z",
"channelId": "UCeY0bbntWzzVIaj2z3QigXg",
"title": "Watch NBC News NOW Live - September 11",
"description": "NBC News NOW is live, reporting breaking news and ...",
"thumbnails": {
...
},
"channelTitle": "NBC News",
"playlistId": "UUeY0bbntWzzVIaj2z3QigXg",
"position": 0,
"resourceId": {
"kind": "youtube#video",
"videoId": "yXO2hQXC5Dw"
}
},
"contentDetails": {
"videoId": "yXO2hQXC5Dw",
"videoPublishedAt": "2020-09-12T06:11:59Z"
},
"status": {
"privacyStatus": "public"
}
},
{
"kind": "youtube#playlistItem",
"etag": "PGyhZonOjiRzqHu7DKDPk6gcMTo",
"id": "VVVlWTBiYm50V3p6VklhajJ6M1FpZ1hnLjY2RTJFNDA4MDA0NDREQTU=",
"snippet": {
"publishedAt": "2020-09-12T02:48:48Z",
"channelId": "UCeY0bbntWzzVIaj2z3QigXg",
"title": "Gaza Sees Spike In Coronavirus Cases, Severe Shortage Of Supplies | NBC News NOW",
"description": "NBC News’ Kelly Cobiella reports on the surge in Gaza ...",
"thumbnails": {
...
},
"channelTitle": "NBC News",
"playlistId": "UUeY0bbntWzzVIaj2z3QigXg",
"position": 1,
"resourceId": {
"kind": "youtube#video",
"videoId": "I0lHV0ZVPAs"
}
},
"contentDetails": {
"videoId": "I0lHV0ZVPAs",
"videoPublishedAt": "2020-09-12T02:48:48Z"
},
"status": {
"privacyStatus": "public"
}
},
...
],
"pageInfo": {
"totalResults": 20000,
"resultsPerPage": 50
}
}
Do notice the property nextPageToken within the JSON response text above; the value of this property -- CAUQAA -- would have to be passed to the second invocation of the endpoint as the parameter pageToken=CAUQAA added to the initial URL above.
For to obtain the n-th page, you'll extract the value of nextPageToken from the n-1-th page, for to pass that value to the the n-th URL as pageToken=....
There is no request in the API that will tell you 100% if a channel exits or not.
Even doing a do a video search for the channel wont help you as. If it exists then it would return the videos for that channel, if not then you get back 0 videos.
request
curl \
'https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UCMPpVtxOI8RtfurfvCcxgyA&order=date&key=[YOUR_API_KEY]' \
--header 'Accept: application/json' \
--compressed
response
{
"kind": "youtube#searchListResponse",
"etag": "oaf4zpPSG6Ho_cSxZedNhuVhjkw",
"nextPageToken": "CAUQAA",
"regionCode": "DK",
"pageInfo": {
"totalResults": 1721,
"resultsPerPage": 5
},
"items": [
no channel
In the event no channel exists the above code will return total results of 0
{
"kind": "youtube#searchListResponse",
"etag": "opQHhlM-mBA_i0h80B_gmHdKgCE",
"regionCode": "DK",
"pageInfo": {
"totalResults": 0,
"resultsPerPage": 5
},
"items": []
}
Another option
Another option would be to not use the API at all. Just make a call to against YouTube itself.
If you load this page it loads the actual channel https://www.youtube.com/c/NBCNews However if you try to load https://www.youtube.com/c/lafjdajdskfadkfj it redirects you to https://www.youtube.com/error?src=404 i would think that would be something simple to detect.

retrieve the total likes and dislikes of a youtube channel using php

I am trying to get the total likes and dislikes of a company channel youtube using API and php but as for now I get only subscribers , total views, and total videos.Does anybody knows how can I proceed, I need to push all these informations to a DB.
"statistics": {
"viewCount": "793781",
"commentCount": "0",
"subscriberCount": "3861",
"hiddenSubscriberCount": false,
"videoCount": "149"
}
Look at the documentation : you can use the getRating endpoint like this :
GET https://www.googleapis.com/youtube/v3/videos/getRating
And you will get somethings like this
{
"kind": "youtube#videoGetRatingResponse",
"etag": etag,
"items": [
{
"videoId": string,
"rating": string
}
]
}

Google Classroom - Get Folder ID for Course

I am having a very difficult time figuring out how to retrieve the Google Drive folder associated with a particular Course (ID).
My understanding is that this value is available as the teacherFolder value:
See Here:
https://developers.google.com/classroom/reference/rest/v1/courses
"id": string,
"name": string,
"section": string,
"descriptionHeading": string,
"description": string,
"room": string,
"ownerId": string,
"creationTime": string,
"updateTime": string,
"enrollmentCode": string,
"courseState": enum(CourseState),
"alternateLink": string,
"teacherGroupEmail": string,
"courseGroupEmail": string,
"teacherFolder": {
object(DriveFolder)
},
But this is returned as an object that contains:
{
"id": string,
"title": string,
"alternateLink": string,
}
I am trying to get the Google Drive folder associated with the course in Google Classroom (by Course ID) so that I can upload a resource to the appropriate folder.
Please point me in the right direction.
(I am hoping to accomplish this via php.)
Based from the documentation:
Resource: Course
{
"id": string,
"name": string,
"section": string,
"descriptionHeading": string,
"description": string,
"room": string,
"ownerId": string,
"creationTime": string,
"updateTime": string,
"enrollmentCode": string,
"courseState": enum(CourseState),
"alternateLink": string,
"teacherGroupEmail": string,
"courseGroupEmail": string,
"teacherFolder": {
object(DriveFolder)
},
"courseMaterialSets": [
{
object(CourseMaterialSet)
}
],
"guardiansEnabled": boolean,
}
You can check out the CourseMaterialSet property which hold all related marterials to the course.
CourseMaterialSet
A set of materials that appears on the "About" page of the course. These materials might include a syllabus, schedule, or other background information relating to the course as a whole.
{
"title": string,
"materials": [
{
object(CourseMaterial)
}
],
}
CourseMaterial
A material attached to a course as part of a material set.
{
// Union field material can be only one of the following:
"driveFile": {
object(DriveFile)
},
"youTubeVideo": {
object(YouTubeVideo)
},
"link": {
object(Link)
},
"form": {
object(Form)
},
// End of list of possible types for union field material.
}
Hope this helps.

ajax call that returns json file

im trying to figure out what the easiest way would be. i have a to do an api call for data i get a json file in return but im trying to parse the data to php so that my website would just pull from the database. do i have to create all the same fields that the api call is using for instance name gender age height. and then do i name it something else so i can call from my webpage. because in order to get a nfl player it gives me a 16 digit code and i want it just to be say tom brady
i have used javascript to pull the data but dont know what to do from there
"players": [{
"name": "Kyle Rudolph",
"jersey": "82",
"last_name": "Rudolph",
"first_name": "Kyle",
"abbr_name": "K.Rudolph",
"preferred_name": "Kyle",
"birth_date": "1989-11-09",
"weight": 265.0,
"height": 78,
"status": "A01",
"id": "1059e9dc-97df-4643-9116-883a0573d8b1",
"position": "TE",
"birth_place": "Cincinnati, OH, USA",
"high_school": "Elder (OH)",
"college": "Notre Dame",
"college_conf": "Independent",
"rookie_year": 2011,
"draft": {
"year": 2011,
"round": 2,
"number": 43,
"team": {
"name": "Vikings",
"market": "Minnesota",
"alias": "MIN",
"id": "33405046-04ee-4058-a950-d606f8c30852"
}
},
John, you don't have to use the same names. Once your PHP receives data from the API, parse it into whatever names you want, ignore the values you don't need etc... For example, suppose the API gives you a first and last name, but in your database you only care about full name. As another example, if the API gives you player weight in pounds but you need it in kilograms:
$api_result = file_get_contents($url);
$api_data = json_decode($api_result);
$name = "$api_data->last, $api_data->first"
$weight = $api_data->weight * 0.454; //convert pounds to kg for storage
Now you can store $name and $weight in your DB as you like. When your website pulls data from your backend, the shape of the data produced by the API doesn't matter because you stored it in the shape that is most helpful to your application

Parsing FB Graph API data Cleanly

I am currently working to parse through FB api data. Take the following for example...
{
"data": [
{
"id": "ID",
"name": "Creative!"
},
{
"id": "ID",
"name": "a name"
}
],
"paging": {
"cursors": {
"after": "fdsagfsganhdfs==",
"before": "gfdwiolrukjhteqrfgbh"
}
},
"summary": {
"total_count": 2
}
}
This is an example of what is returned when the Graph API is queried for likes. The issue I am having is that I want a clean way to get the total_count out of this data. Often times it will come in without the summary field if there are no 'likes'. This is easily parsed by doing a few if isset() and if array_key_exists but I will be dealing with a lot of data and this use case applies to many different type of data from FB. Any advice on just getting the total_count field? FQL would work but seems to be deprecated. Thanks.
If the total_count field is in fact the sum of the number of elements in the data array then you can simply use the count function in PHP to count the length of the array.
$json_array = json_decode($fb_json_string, true);
$total_count = (isset($json_array['total_count']))? $json_array['total_count']:((isset($json_array['summary']['total_count']))? $json_array['summary']['total_count']:0);
I don't know if I can make it any uglier, it's simply a suggestion though.

Categories