创建 Google 分析目标 403 错误消息:您的项目无权访问此功能

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

我正在尝试在 Google Analytics 帐户中创建一个目标。为此,我需要使用插入方法,这需要我请求访问测试版 api.

我请求访问 Google Analytics Beta API(两次),但我仍然收到以下错误:

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."}}

这是我的创建目标代码:

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();
    }
}

此处这一行抛出错误:

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

我可以做任何不涉及创建目标的事情,但是一旦我尝试插入(创建)一个目标,它就会出错。

我查看了 google-analytics-api 组,但该组自 2015 年以来已关闭。我不确定还能做什么。我尝试创建新密钥并再次提交以供批准,但我仍然收到此错误。任何帮助将不胜感激。

正如您所说,您需要请求访问测试版 @ https://developers.google.com/analytics/devguides/config/mgmt/v3/account-management#limits .

根据 (403) Your project does not have access to this feature,您最多可能需要数周(2 年前需要 3 周)才能收到确认消息。我想你收到了这条消息?

在这之后我会尽量减少代码的可能性并重新尝试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?.