Search code examples
phpgoogle-analyticsgoogle-analytics-apihttp-status-code-403

Create Google Analytics Goal 403 Error Message: Your project does not have access to this feature


I am trying to create a goal inside of a Google Analytics account. To do this, I needed to use the insert method which required me to request access to the beta api.

I requested access to the Google Analytics Beta API (twice) and I am still getting the following error:

Google_Service_Exception in REST.php line 118: {"error":{"errors":[{"domain":"global","reason":"insufficientPermissions","message":"Your project does not have access to this feature."}],"code":403,"message":"Your project does not have access to this feature."}}

Here's my create goal code:

public function createGoal( $brand, $value, $steps = null )
{
    $goal = new Google_Service_Analytics_Goal();
    $goal->setActive(TRUE);
    $goal->setType('URL_DESTINATION');
    $goal->setName('Goal conversion count.');
    $goal->setValue($value);

    $urlDestinationDetails = new Google_Service_Analytics_GoalUrlDestinationDetails();
    $urlDestinationDetails->setUrl(trim($steps[count($steps)-1]['url']));
    $urlDestinationDetails->setCaseSensitive(False);
    $urlDestinationDetails->setMatchType("BEGINS_WITH");
    $urlDestinationDetails->setFirstStepRequired(True);        

    $urlDestinationDetailsStepsArray = [];

    foreach ($steps as $step)
    {
        $urlDestinationDetailsSteps = new Google_Service_Analytics_GoalUrlDestinationDetailsSteps();
        $urlDestinationDetailsSteps->setName($step['name']);
        $urlDestinationDetailsSteps->setNumber($step['number']);
        $urlDestinationDetailsSteps->setUrl(trim($step['url']));

        array_push($urlDestinationDetailsStepsArray,$urlDestinationDetailsSteps);
    }

    $urlDestinationDetails->setSteps($urlDestinationDetailsStepsArray);

    $goal->setUrlDestinationDetails($urlDestinationDetails);

    try
    {
        $analytics = new Google_Service_Analytics($this->initializeAnalytics($brand,['https://www.googleapis.com/auth/analytics.readonly','https://www.googleapis.com/auth/analytics.edit']));
        var_dump($analytics);
        var_dump($goal);
        $analytics->management_goals->insert($this->accountId,$this->trackingId,$this->viewId,$goal);            
    }
    catch (apiServiceException $e)
    {
        print 'There was an Analytics API service error ' . $e->getCode() . ':' . $e->getMessage();
    }
    catch (apiException $e)
    {
        print 'There was a general API error ' . $e->getCode() . ':' . $e->getMessage();
    }
}

This line here throws the error:

$analytics->management_goals->insert($this->accountId,$this->trackingId,$this->viewId,$goal);

I can do anything that doesn't involve creating goals, but as soon as i try to insert (create) a goal, it errors.

I checked the google-analytics-api group but that has been closed since 2015. I am not sure what else to do. I tried creating new keys and submitting for approval again but I am still receiving this error. Any help would be greatly appreciated.


Solution

  • As you said you need to request access to the beta @ https://developers.google.com/analytics/devguides/config/mgmt/v3/account-management#limits .

    According (403) Your project does not have access to this feature it can take up to multiple weeks (2 years ago it took 3 weeks) till you get a confirmation message. I assume you got this message?

    After this I would minimize the possibilities of code and try it over https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtReference/management/goals/insert#try-it

    In the Management API, the write operations (e.g., create, update, delete, and patch)—for Web Property, View (Profile), and Goal resources—are in limited beta. To use them, request access to the beta. If you try to use the following embedded APIs explorer to call the write operations, you'll get an error message because it uses its own project and credential. To set up the stand-alone APIs explorer to work with your project and credential to call them, see Can I use my own API key and/or OAuth 2.0 client ID with the Explorer?.