3
votes

I am trying to gain access to Google Analytics API through OAuth2.

What i did:

  1. Open developers console > APIs and Auth > Credentials

  2. Create a new Client ID

  3. Generate p12 key

  4. Copy the key on the server

  5. Open google analytics page > admin > Account > User Management

  6. Add the email from generated Client ID, something like: [email protected]

  7. Give to this email Read and Analyze permissions

  8. Then when I go back to developers console > permission. The new email is added on Service accounts with Edit permissions

  9. Recheck if Google Analytics is enabled and data is going in.

Now I had installed widop/google-analytics-bundle and configure the bundle:

widop_google_analytics:
    client_id:        "[email protected]"
    profile_id:       "ga:12345678"
    private_key_file: "mykey.p12"
    http_adapter:     "widop_http_adapter.curl"

And the query I try to create is:

        $profileId = 'ga:12345678';
        $query = new Query($profileId);

        $query->setStartDate(new \DateTime('-2months'));
        $query->setEndDate(new \DateTime());

        $query->setMetrics(array('ga:visits' ,'ga:bounces'));
        $query->setDimensions(array('ga:browser', 'ga:city'));

        $query->setSorts(array('ga:country', 'ga:browser'));

        $query->setFilters(array('ga:browser=~^Firefox'));

        $query->setSegment('gaid::10');

        $query->setStartIndex(1);
        $query->setMaxResults(10000);
        $query->setPrettyPrint(false);
        $query->setCallback(null);

        $clientId = '[email protected]';
        $privateKeyFile = 'mykey.p12';
        $httpAdapter = new CurlHttpAdapter();

        $client = new Client($clientId, $privateKeyFile, $httpAdapter);
        $token = $client->getAccessToken();

        $service = new Service($client);
        $response = $service->query($query);

        return $response;

As a response I get this error:

User does not have sufficient permissions for this profile.

When I open developers console > overview > 1 hour (tab)

I had notice that requests are going in.

From all that - I assume that authentication and query is OK but the user has no permissions to get any kind of data which is weird because I had granted Read and Analyze permissions to [email protected]

What could by the reason for that exception?

1

1 Answers

1
votes

I do not know if you already solved this issue. The solution for me was use the view ID instead of account ID on the analytics account. The view ID is on the third column in settings, on Google Analytics administration panel.

Sorry for my english.