Closed belchiorplan closed 10 months ago
Same issue. Can anyone help pleas?
In addition, what is $VIEW_ID in GA4?
// Replace with your view ID, for example XXXX.
$VIEW_ID = "<REPLACE_WITH_VIEW_ID>";
property-id / STREAM ID / any other ID?
@belchiorplan it seems to be a permissions issue. Check the permissions for Account access management
as well as Property access management
in the UI portal. Also experiment with adding more scopes in your code, such as ANALYTICS_EDIT
, ANALYTICS
and ANALYTICS_READONLY
, some users have seen success with that approach. defined here
@tamarz-sig pre-GA4 launch, a UA property had views, link, now after GA4 launch, it doesn't.
Views seem to be available only via Reporting APIs and Property analytics (for GA4) seems to be available via Data APIs
Hopefully it will unblock you, feel free to re-open otherwise.
import { google } from "googleapis";
export async function GET() {
const auth = new google.auth.GoogleAuth({
credentials: {
client_email: process.env.GOOGLE_CLIENT_EMAIL,
private_key: process.env.GOOGLE_PRIVATE_KEY?.replace(/\\n/g, "\n"),
},
scopes: ["https://www.googleapis.com/auth/analytics.readonly"],
});
const analytics = google.analytics("v3");
const response = await analytics.data.ga.get({
auth: auth,
ids: "ga:<PROPERTY_ID>",
"start-date": "30daysAgo",
"end-date": "today",
metrics: "ga:sessions,ga:pageviews,ga:users",
});
console.log(response.data);
return Response.json(response.data);
}
I am facing the same issue here. I am unable to find the view ID, so instead, I am using the property ID. However, when I use this code, I get a 403 error:
errors: [
{
message: 'User does not have sufficient permissions for this profile.',
domain: 'global',
reason: 'insufficientPermissions'
}
]
@vishwarajanand
I'm using version 2.14.0, Laravel 10 and PHP 8.2 but I'm getting the error:
{ "error": { "code": 403, "message": "User does not have sufficient permissions for this profile.", "errors": [ { "message": "User does not have sufficient permissions for this profile.", "domain": "global", "reason": "forbidden" } ], "status": "PERMISSION_DENIED" } }
I'm using service account authentication, I also added the service account email in the Google Analytics access properties, but I still get the permission error, here's the code: