How to get data from Google analytics api v4 with access token in nodejs - google-analytics-api

How do I get the analytics data from Google Analytics API(v4), I already have authenticated the user from Google now I have an access token how can I make a request to get the data from API, I am using Nodejs in my web server.

you need to use the Google Analytics Data API (GA4) to get the data (The API is in preview version at the time of writing).
https://developers.google.com/analytics/devguides/reporting/data/v1
To Generate the report , You can use the Method: runReport (https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1alpha/TopLevel/runReport)
POST https://analyticsdata.googleapis.com/v1alpha:runReport
A Sample Body to Generate Report for "screenPageViews" would be:
{
"entity": {
"propertyId": "<your property id>"
},
"dimensions": [
{
"name": "thereportData",
"dimensionExpression": {
"lowerCase": {
"dimensionName": "pageTitle"
}
}
}
],
"metrics": [
{
"name": "screenPageViews"
}
],
"dateRanges": [
{
"startDate": "2021-04-01",
"endDate": "2021-04-07"
}
],
"keepEmptyRows": true,
"returnPropertyQuota": true
}
To Get the property Id for your account , Use the Following endpoint (https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accountSummaries/list)
GET https://analyticsadmin.googleapis.com/v1alpha/accountSummaries
Make sure to enable Google Analytics and Google Admin Api in your Google cloud Console.

Related

Facebook Workplace Account Management API - Update user

I have a workplace application,
I wish to change emailIds of the user via API,
I found that Account Management API
can be used to modify user details via API calls.
My use-case is to modify user email via the Account Management API, which comes under urn:scim:schemas:core:1.0 schema extension,
I wish to overwrite the existing email with the one I would specify in the requestBody,
From the documentation, I've come up with the following request -
Url endpoint -
https://someCompanyName.facebook.com/scim/v1/Users/ HTTP/1.1
Method type -
POST
Request body-
{
"schemas": [
"urn:scim:schemas:core:1.0",
"urn:scim:schemas:extension:facebook:auth_method:1.0"
],
"userName": "abc",
"name": {
"formatted": "Julius Caesar"
},
"emails": ["abc#gmail.com"],
"urn:scim:schemas:extension:facebook:auth_method:1.0": {
"auth_method": "password"
}
}
Is it correct? What modifications do I need to make to the request?
in order to change the emails of a user you have to do a PUT request to the address https://www.facebook.com/scim/v1/Users/{userId}
and you have to change in your payload the email address:
{
"schemas": [
"urn:scim:schemas:core:1.0",
"urn:scim:schemas:extension:facebook:auth_method:1.0"
],
"userName": "abc",
"name": {
"formatted": "Julius Caesar"
},
"emails": [
{
"primary": true,
"type": "work",
"value": "newemail#gmail.com"
}
],
"urn:scim:schemas:extension:facebook:auth_method:1.0": {
"auth_method": "password"
}
}
Hope it helps

How to further filter Facebook Graph API query on Graph API Explorer for my ad account?

I'm trying to get the ad spend and mobile app installs for my app using the Facebook Graph API v2.11 for marketing. In the Graph API Explorer, when I try
/act_<my account>/campaigns?fields=insights{actions,spend}&time_range={'since':'2017-07-07','until':'2017-12-12'}
In the output, under "insights", I get an object of this type:
"data": [
{
"actions": [
{
"action_type": "comment",
"value": "3"
},
{
"action_type": "like",
"value": "33"
},
{
"action_type": "link_click",
"value": "1531"
},
{
"action_type": "mobile_app_install",
"value": "1049"
}
],
"spend": "8621.03",
"date_start": "2017-10-28",
"date_stop": "2017-11-26"
}
]
If I want it to fetch only the actions where action type is "mobile_app_install", how can I further filter my query?
There is possible to filter that on Facebook side just call it like that:
/act_<yourAdAccount>/insights
?level=campaign
&fields=actions,spend
&time_increment=all_days
&time_range={'since':'2017-07-07','until':'2017-12-12'}
&filtering=[{field: "action_type",operator:"IN", value: ['mobile_app_install']}]
&use_account_attribution_setting=true
I have gone through the documentation as well as the adsinsights.py file on github but I cannot find a way to limit the number of action_types returned. You will simply have to do the filtering after you have retrieved the data from the api.
https://developers.facebook.com/docs/marketing-api/insights/parameters
https://github.com/facebook/facebook-python-ads-sdk/blob/master/facebookads/adobjects/adsinsights.py

Getting the user context id for a Facebook Open Graph API query

I'm currently building an experimental social network to study the influence of social medias on customers behaviors.
My users log in through Facebook OAuth provider. I store their access tokens and query the Open Graph API to fetch info about them.
I'm trying to use the all_mutual_friends edge for a given user context, using the access token of one of my users, but I found nowhere how to get the user-context-id that is required to get that info.
It is not the id of the user you want to retrieve the mutual friends from. I've tried that and got an error.
I've skimmed trough Open Graph docs and found nothing about that user-context-id.
How can I retrieve it?
Odly enough, the docs do not specify how to get the context id, but I managed to get it by digging a little.
This is how you would do to get the all_mutual_friends data:
GET /{user-id}?fields=context.fields%28all_mutual_friends%29&access_token={other-user-access-token}
Then, the context id would be given in the response.
{
"context": {
"all_mutual_friends": {
"data": [
{
"id": "1381033095543871",
"name": "Dave Amiaaehjighi Putnamwitz",
"token": <user token>,
"picture": {
"data": {
"is_silhouette": true,
"url": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xpf1/v/t1.0-1/c15.0.50.50/p50x50/10354686_10150004552801856_220367501106153455_n.jpg?oh=82c2ce067f9c7f2c0a66d1efb16730e7&oe=5670F72F&__gda__=1450287355_17925d7381c8da5663c2349483f4b032"
}
}
}
],
"paging": {
"cursors": {
"before": "MTM4MTAzMzA5NTU0Mzg3MQZDZD",
"after": "MTM4MTAzMzA5NTU0Mzg3MQZDZD"
}
},
"summary": {
"total_count": 1
}
},
"id": <Context id>
},
"id": "1375274282788732",
}
That context id could then be used for
GET /{user-context-id}/all_mutual_friends/
But this step is pretty useless since we get the data in the first query.
Would be nice if Facebook updated their docs to reflect reality.

Retrieve comments on Facebook ads through the API

Is it even possible to retrieve comments on Facebook ads through the API (Graph or FQL)? If yes, do I need a whitelisted app to do it? Also, if I have a page access token, is it enough, or I need a user access token with ads_management permission?
Regarding the token usage, a page_token is extended by a given user to a given app. The page_token will have at most the same privileges as the user on his authorized pages and ad_accounts.
Generating a page_token can be done automatically, in behalf of the user, if you already have a user_token with manage_pages permission. Again, this page_token will have at most the same privileges that the user_token you used to generate it.
In other words, having a page_token doesn't amount for any additional privileges than the ones already present in the user_token.
Having said that, you need
a token with access to a given ad_account
that the app for which that token was generated, has that ad_account active in its settings
If the second requisite isn't met, for example for ad_account act_123456789 you'll get the error:
{
"error": {
"message": "(#274) The ad account is not enabled for usage in Ads API. Please add it in developers.facebook.com/apps -> select your app -> settings -> advanced -> advertising accounts -> Ads API. Account 123456789 not enabled for this application.",
"type": "OAuthException",
"code": 274,
"fbtrace_id": "xxxxx"
}
}
You can try, for example, generating a token with all privileges for the Graph API Explorer App. It will throw the error above because said App doesn't have the ad_account act_123456789 enabled.
So, given you have a token with ads_read extended for an app that has this ad_account listed, and you know the ad_id, you can procede as I explained in thread Get comments from facebook ads (marketing)
Basically, you use the ad_id (for example 123000000) to get the promoted post id and its actions
/123000000?fields=creative.fields(effective_object_story_id),insights.fields(actions)
The answer will contain an effective_object_story_id (for example, let's say it is 456000000_789000000) and its actions broken down by type:
{
"creative": {
"effective_object_story_id": "456000000_789000000",
"id": "123000000"
},
"insights": {
"data": [
{
"actions": [
{
"action_type": "comment",
"value": "12"
},
{
"action_type": "like",
"value": "2"
},
{
"action_type": "post",
"value": "3"
},
{
"action_type": "post_reaction",
"value": "29"
},
{
"action_type": "video_view",
"value": "558"
},
{
"action_type": "page_engagement",
"value": "604"
},
{
"action_type": "post_engagement",
"value": "602"
}
],
"date_start": "2017-08-14",
"date_stop": "2017-08-20"
}
],
"paging": {
"cursors": {
"before": "xxx",
"after": "xxx"
}
}
}
}
If you want to also distinguish paid comments from regular comments, you can query the comments edge of the post_id 456000000_789000000 to get the overall comment count which should be greater than the comments generated by the ad.
You can't pull the actual words of the comment via the API - would make it too easy to scrape.
Yes, Facebook ads that have comments are because the ad is a promoted page post, whether that page post is hidden or not. Page post comments are available via the Facebook API and does not require having Ads API access. Use the graph API endpoint /v2.1/{page-id}/comments to retreive page post comments. See the Facebook Graph API documentation on object comments for more information.

Facebook smart friendlist via API

Trying to retrieve friends on smart (auto-generated) friendlists via the API, but it is returning no data.
Via graph explorer: /<friendlist id>?fields=members
If this is a custom list (not auto-generated) this returns:
{
"id": "<friendlist id>",
"members": {
"data": [
{
"id": "<friend id>",
"name": "<name>"
}
]
}
}
However, with auto-generated lists I seem to get just the id back and no members.
This seems to be a known bug for smart lists:
http://developers.facebook.com/bugs/471978009483097/