在Google Analytics中创建目标Google API Cliente

时间:2018-06-13 19:48:10

标签: php google-api google-analytics-api google-client

我正在尝试在Google Analytics帐户中创建目标。为此,我需要使用insert方法。

但是Google文档不完整,它没有显示我如何输入EVENT类型的新目标。

我在其文档中使用了Google的示例,其中包含为EVENT转换的VISIT_ON_TIME类型。

我的代码:

public function insert($model){
        $analytics = $this->init();

        $param = $this->getCompany($model);

        // Construct the body of the request.
        $goal = new \Google_Service_Analytics_Goal();
        $goal->setId($model->google_id);
        $goal->setActive($model->active);
        $goal->setType('VISIT_TIME_ON_SITE');
        $goal->setName($model->name);

        // Construct the time on site details.
        $details = new \Google_Service_Analytics_GoalEventDetails();

        $event = new \Google_Service_Analytics_GoalEventDetailsEventConditions();
        $event->setExpression($model->name);
        $event->setMatchType('EXACT');
        $event->setType('CATEGORY');
        $event->setComparisonType(null);
        $event->setComparisonValue(null);       

        $arr =[];
        $arr[] = [
            'eventConditions' => [              
                'comparisonType' => 'EQUAL',
                'comparisonValue' => $model->form,
                'expression' => $model->name,
                'matchType' => 'EXACT',
                'type' => 'CATEGORY'
            ]
        ];

        // I tried using $ARR and $EVENT - but not unsuccessfully
        $details->setEventConditions($arr);
        $details->setUseEventValue(false);

        // Set the time on site details.
        $goal->setEventDetails($details);

        $timeOn = new \Google_Service_Analytics_GoalVisitTimeOnSiteDetails();
        $timeOn->setComparisonType('GREATER_THAN');
        $timeOn->setComparisonValue(300.0);

        // //Set the time on site details.
        $goal->setVisitTimeOnSiteDetails($timeOn);

        try {
            $analytics->management_goals->insert($param['google_account'],$param['google_property'],$param['google_analytics'],
              $goal);

            $model->response = 'success';
            $model->save();
        } catch (apiServiceException $e) {
            $model->response = 'There was an Analytics API service error '
              . $e->getCode() . ':' . $e->getMessage();
            $model->save();       
        } catch (apiException $e) {
            $model->response = 'There was a general API error '
              . $e->getCode() . ':' . $e->getMessage();
            $model->save(); 
        }
    }

我的错误返回:

{"error":{"errors":[{"domain":"global","reason":"invalidValue","message":"Value must not be set for field eventDetails."}],"code":400,"message":"Value must not be set for field eventDetails."}}

0 个答案:

没有答案