Google Analytics API - Get page view information for specific URLs - google-analytics

I am looking to implement a page view tracking system on one of my websites.
The website is a directory where suppliers can advertise their services. Suppliers have a unique profile page on the site, e.g mysite.com/directory/abc-profile.html
The idea is that suppliers can log in to their account area on the site and view stats on how many people are visiting their profile. Ideally I want to display this as :
Total views | Total today | This week | This month | This year
It does not matter if the data is not completely up to date.
I already have Google Analytics tracking code installed on the site. Is it possible to use the Google Analytics API to retrieve this data? If so, what kind of query do I need to make? I had a look on the documentation but could not figure whether the functions allow this or not.
I am using PHP and MySQL on the server.

Yes - you will need to use the Google Analytics API for this. I would suggest checking out the Query Explorer to get a feel for the queries you will need to create.
You will require numerous queries to get all the data you need (adjusting the starting date):
- Total Views
- This Year
- This Month
- This Week (i.e. last 7 days - from which you could also get Total Today).
Here is an example query:
https://www.googleapis.com/analytics/v3/data/ga?ids=ga:1234456789&dimensions=ga:pagePath&metrics=ga:pageviews&filters=ga:pagePath==/about-us.html&start-date=2013-10-15&end-date=2013-10-29&max-results=50
Alternatively, you might want to consider www.embeddedanalytics.com (disclosure - I work with them). We have a service/platform that allows website owners to embed GA based charts/statistics without having to learn the GA API. We have a CMS version which will do exactly what you need (where you script the call to pass the page path). We have done something like this with a number of podcast sharing sites.

Google suggests using Reporting API V4 now. The accepted answer uses V3.
Here is a V4 request example:
POST https://analyticsreporting.googleapis.com/v4/reports:batchGet?key={YOUR_API_KEY}
{
"reportRequests": [
{
"viewId": "YOUR_VIEW_ID",
"dimensions": [
{
"name": "ga:pagePath"
}
],
"metrics": [
{
"expression": "ga:pageviews"
}
],
"dimensionFilterClauses": [
{
"filters": [
{
"operator": "EXACT",
"dimensionName": "ga:pagePath",
"expressions": [
"/your-path"
]
}
]
}
],
"dateRanges": [
{
"startDate": "2009-12-31",
"endDate": "2016-09-28"
}
]
}
]
}
where
YOUR_API_KEY - for auth related things follow this page
YOUR_VIEW_ID - you can use the Account Explorer to find a View ID. (or Admin -> View -> View Settings -> View ID).
For more documentation details and a "Try it!" console follow this page.

You should be able to add a filter on the landing page. I am assuming that each user's site has its own start page. This returns only the data for that user. If you want the code on how to do this I suggest you google: Google analytics core reporting API PHP tutorial
Another idea would be to let the user add there Google Analytics account to there profile. Then you can output the google analytics code onto there page. Then they can track there own google analytics data and you won't need to deal with any of it.

Thought I would provide an updated version as the others are showing for V3 and V4 - for anyone using the latest API for Google Analytics Data API (GA4). This is just grabbing the total users, over a date range for a specific path.
Note that the POST is hitting the v1 beta address as this was the latest at the time.
POST https://analyticsdata.googleapis.com/v1beta/properties/PROPERTY_ID:runReport
{
"dateRanges": [
{
"startDate": "YYYY-MM-DD",
"endDate": "YYYY-MM-DD"
}
],
"dimensions": [
{
"name": "pagePath"
}
],
"dimensionFilter": {
"filter": {
"fieldName": "pagePath",
"stringFilter": {
"matchType": "CONTAINS",
"value": "/YOUR/PATH"
}
}
},
"metrics": [
{
"name": "totalUsers"
}
]
}

Related

Fetch company posts from linkedin API

I am trying to fetch the posts of the company from the api, I have already applied to the marketing development platform and it was approved. I already got the token with the scope: r_organization_social and I'm calling the /shares api:
https://api.linkedin.com/v2/shares?q=owners&owners=urn:li:organization:{company_ID}&sharesPerOwner=100&count=25&sharesPerOwner=10
But I'm getting the following response:
{
"paging": {
"start": 0,
"count": 25,
"links": [
{
"type": "application/json",
"rel": "next",
"href": "/v2/shares?count=25&owners=urn%3Ali%3Aorganization%3A{company_ID}&q=owners&sharesPerOwner=10&sharesPerOwner=100&start=0"
}
],
"total": 242
},
"elements": []
}
I tried to change the query params and it's still the same
This end-point worked for me:
https://api.linkedin.com/v2/ugcPosts?q=authors&authors=List(urn%3Ali%3Aorganization%3A<ID_ORGANIZATION>)
See documentation: https://learn.microsoft.com/en-us/linkedin/marketing/integrations/community-management/shares/ugc-post-api?tabs=http#sample-request-6
Disclaimer: I've no access to the linkedin API and couldn't test. But these are some things I noticed:
Your url contains two times the paramater sharesPerOwner, try removing one.
In the docs it's recommended to set the sharesPerOwner to 1000 and the count to 50. I'd also include the start paramater, just to make sure:
Maybe try something like this:
GET https://api.linkedin.com/v2/shares?q=owners&owners=urn:li:organization:{id}&sharesPerOwner=1000&count=50&start=0
From the api-docs(https://learn.microsoft.com/en-us/linkedin/marketing/integrations/community-management/shares/share-api?tabs=http#find-shares-by-owner): "Note that the pagination excludes UGC and Direct Sponsored Content (DSC) posts". Make sure that the owner you are testing contains posts.
If this doesn't work. Could you provide some information on how you are sending the request? Have you tried accessing other parts of the api?

Google Calendar API - Can't add google meets to event

i'm using a service account to add events to my calendar, 1 month ago it was working with the code below, then after sometime it started to give me the error: 'invalid conference data type', i removed 'conferenceDataVersion' field and it started working again. Now it doesn't work both ways. When i test it from the Calendar API page it works fine with the same parameters, but on my app it's not working.
The event is added succesfully, but no google meets link is created.
I think that maybe i can't create an event of the type "hangoutsMeet" on a service account, while i was testing it worked because i was creating it from the same account. BTW i still can't figure out why it was working fine one month ago, nothing has changed. I was using the exact same configurations and code and i managed to create an event and to add a google meet conference to that evet.
calendar.events.insert( {
"calendarId" : 'primary',
"conferenceDataVersion": 1,
"resource":{
"end": {
"dateTime" : endDate.toISOString(),
'timeZone': 'Europe/Berlin'
},
"start": {
"dateTime" : startDate.toISOString(),
'timeZone': 'Europe/Berlin'
},
"conferenceData": {
"createRequest": {
"requestId": randomstring,
"conferenceSolutionKey": {
"type": "hangoutsMeet"
}
}
}
}
Any help appreciated. Thanks

How to retrieve Property ID and use utm marks in Google Analytics Data API?

I want to switch from Google Analytics Reporting API to Google Analytics Data API (GA4)
.
In Reporting API it was possible to retieve all view id for account (used to send requests) by special endpoint. In Analytics Data API there is no view id, but there is Property ID which is used to send requests. But I didn't find any endpoint to get Property ID through REST. Is it even possible or it's only available from https://analytics.google.com/ admin panel?
Also in Reporting API to send requests I used utm marks in dimensionFilterClauses array like this:
"dimensionFilterClauses":[
{
"operator":"AND",
"filters":[
{
"dimensionName":"ga:campaign",
"operator":"EXACT",
"expressions":[
"some_campaign_name_2021"
]
},
{
"dimensionName":"ga:adContent",
"operator":"EXACT",
"expressions":[
"multiformat"
]
},
{
"dimensionName":"ga:medium",
"operator":"EXACT",
"expressions":[
"cpm"
]
},
{
"dimensionName":"ga:source",
"operator":"EXACT",
"expressions":[
"facebook"
]
},
{
"dimensionName":"ga:keyword",
"operator":"EXACT",
"expressions":[
"car"
]
}
]
}
]
How can I do it with Google Analytics Data API (GA4)?
You can use the Admin API to list all GA4 Properties that you have access to. The method is accountSummaries.list.
The response will contain for example...
"propertySummaries": [
{
"property": "properties/215...",
"displayName": "You GA4 Property Name"
},
...
For the utm marks, you'll need to translate those dimension names from GA3 to GA4. This page provides the equivalent dimensions in GA4. For example, ga:source in GA3 is similar to source in GA4. Note that ga:keyword and ga:adContent are not yet available in GA4.

Related LinkedIn Target Audience facets

i am new this linked-in ad campaign API i am not able to find out which facet combination is correct to achieve the specific targeting audience
and there is no JSON enter image description hereavailable to figure it out for proper combination
For Example - if i am selecting company_size with other facet i am not able to create LinkedIn campaign with targeting audience.
So i am not able to find out what is combination for linkedIn target audience,
You can take a look at the example in the doc, a working example for your scenario is:
{
"include": {
"and": [{
"or": {
"urn:li:adTargetingFacet:staffCountRanges": [
"urn:li:staffCountRange:(2,10)"
]
}
}, {
"or": {
"urn:li:adTargetingFacet:industries": [
"urn:li:industry:4",
"urn:li:industry:51"
]
}
}, {
"or": {
"urn:li:adTargetingFacet:locations": [
"urn:li:country:us"
]
}
}]
}
}
I've created a campaign with this targeting and the related targeting audience looks like:
Hope this help
NB: A bug exists in the doc, I didn't remember if the doc was updated
UPDATE:
Referring to the doc:
These facets names and facet values mentioned below are discoverable
using the adTargetingFacets and adTargetingEntities API.
You can also refer to this page for Ads Targeting Discovery.
You can use the explore API in order to find the right key values

GA API differences with UI

Using segments, I am building an app that lets user enter a URL and check different metrics for it.
I see some significant differences between reports pulled with the API and report generated in the UI. Maybe I am misunderstanding something about segments.
For example, I have a segment designed to show only users that went on a specific page.
which map to a small fraction of my users:
When looking at the user age brackets, I see small numbers scattered across all categories.
Now to run the equivalent report in the API, I am using the payload below.
{
"reportRequests": [
{
"viewId": "#####",
"dateRanges": [
{ "startDate": "2017-03-01",
"endDate": "2017-04-27" }
],
"metrics": [
{"expression": "ga:pageviews"},
{"expression": "ga:sessions"},
{"expression": "ga:users"}
],
"segments": [
{
"dynamicSegment": {
"name": "Users of /apath/ofinterest/",
"userSegment": {
"segmentFilters": [
{
"simpleSegment": {
"orFiltersForSegment": {
"segmentFilterClauses": [
{
"dimensionFilter": {
"dimensionName": "ga:pagePath",
"operator": "EXACT",
"expressions": [
"/apath/ofinterest/"
]}}]}}}]}}}
],
"dimensions": [
{ "name": "ga:userAgeBracket" },
{ "name": "ga:segment" }
]
}
]
}
Which yield completely different results:
Only 2 age brackets, and weirdly, the same number of users in each (I tried with different time frame with the same behaviour).
Any ideas on what could be wrong? Could it be something in the settings of the segment? Related to "Sessions/User Include"?
Or could this warning below that I see in the UI have different impact in the UI and the API?
According to this comment, it sounds like numbers might be calculated differently for the API and the UI. Is that still the case?
Thanks a lot!
GA UI data is picked from the pre-aggregated tables.
Pre-calculated data -- pre-aggregated tables
These are the precalculated data that Google uses to speed up the UI. Google does not specify when this is done but it can be at any point of the time. These are known as pre-aggregated tables
So if you compare the numbers from GA UI to your GA API data, you will always see a discrepancy because the view might be aggregated at some early stages of the day, while your api data is fresh

Resources