3

I want to run a google analytics report using runReport. I have followed the instructions at https://developers.google.com/analytics/devguides/reporting/data/v1/quickstart-client-libraries and made a copy of the code that uses json-credentials at https://github.com/googleapis/nodejs-analytics-data/blob/main/samples/quickstart_json_credentials.js

I have one Analytics Account with two Properties & Apps - DEV and STAGE.

In each of them I have created a service account with OWNER permissions. After that I created a key and downloaded the generated JSON-file.

I can test and run the API from the "Try this API" link at https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport for both my properties and it works well for both of them. (But then I use OAuth authentication of course).

However when I run the code using the JSON-credentials the DEV always work. The STAGE always fails. I have tried running the code both from my machine as well as from https://shell.cloud.google.com/ with the same result. The failure message is:

7 PERMISSION_DENIED: User does not have sufficient permissions for this property. To learn more about Property ID, see https://developers.google.com/analytics/devguides/reporting/data/v1/property-id.

I have checked several times that the DEV and STAGE properties are correct and that the I use the JSON-credentials file associated with the correct property.

In STAGE, where calls always fail I have several times created new service accounts (with Basic->Owner credentials), created keys and downloaded JSON-credentials. But all with the same result. STAGE always fails.

I have compared the permissions for the service accounts in DEV and STAGE and they seems to be identical.

I have read and tried tips from other people with "permission denied" issues here at stack overflow but none that solves my issue.

Are there any authorization logs that I read from google console? I'm kind of stuck now.

The code I run (properties and file name obfuscated):

"use strict";
function main(propertyId = "YOUR-GA4-PROPERTY-ID", credentialsJsonPath = "") {
  propertyId = "27..DEV property";
  credentialsJsonPath = "./DEVcredentials.json";
  //   propertyId = "27..STAGE property";
  //   credentialsJsonPath = "./STAGEcredentials.json";
  const { BetaAnalyticsDataClient } = require("@google-analytics/data");
  const analyticsDataClient = new BetaAnalyticsDataClient({
    keyFilename: credentialsJsonPath,
  });
  async function runReport() {
    const [response] = await analyticsDataClient.runReport({
      property: `properties/${propertyId}`,
      dateRanges: [
        {
          startDate: "2020-03-31",
          endDate: "today",
        },
      ],
      dimensions: [
        {
          name: "country",
        },
      ],
      metrics: [
        {
          name: "activeUsers",
        },
      ],
    });
    console.log("Report result:");
    response.rows.forEach((row) => {
      console.log(row.dimensionValues[0], row.metricValues[0]);
    });
  }

  runReport();
}

process.on("unhandledRejection", (err) => {
  console.error(err.message);
  process.exitCode = 1;
});
main(...process.argv.slice(2));

1 Answer 1

2

User does not have sufficient permissions for this property.

Means that the user who you have authenticated with does not have permissions to access that view. In this case we are talking about properties/${propertyId}

If you are using a service account make sure to grant it permissions at the account level not the view level.

Permissions need to be granted thorough the google analytics website in the admin section

enter image description here

4
  • Thanks for your answer. Two follow up questions. 1) I have granted the service account Owner-permissions on project-level (there seems to be no way to add at account level) 2) Why does an account with Owner permissions lack sufficient permissions? Verified permissions with the following query: console.cloud.google.com/iam-admin/analyzer/… { "role": "roles/owner", "members": [ "serviceAccount:[email protected]", "user:[email protected]", ] } Commented Oct 18, 2021 at 8:21
  • Check my edit. Permissions need to be granted in the admin section of the google analytics website. Look for account access managment and add the email address for the service account there. Commented Oct 18, 2021 at 8:28
  • 1
    Thanks! Now it works. I could not find this procedure in the documentation though. Will be sure to document the procedure in our internal documentation. Commented Oct 18, 2021 at 15:26
  • I will ping the team to let them know they need to address the documentation. Commented Oct 18, 2021 at 16:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.