Google Client API - Inserting Goals in Analytics

0

How well with you? Well, I do not know if anyone has already gone through this, but let's ... Within the Google Analytics dashboard, you can create Goals that are events that track the conversions that are performed on your site.

So far this is ok, but I need to go further, and in this way I have created a panel that will be able to register the goals and via API I will consume Analytics.

In thesis should work. The problem is that the Google documentation did not find out the way it should be done when the Meta TYPE is of type "EVENT".

At this link: Doc Google

It shows how you can insert a goal of TYPE "VISIT_ON_SITE", however, above said type "EVENT".

In front of this, browsing in Google's PHP classes I found some things and based on VISIT_ON_SITE I got to this code snippet:

$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($model->type);
        $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);       

        $details->setEventConditions($event);
        $details->setUseEventValue(true);

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

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

The problem is that I'm having this error return:

"{"error":{"errors":[{"domain":"global","reason":"required","message":"Field eventDetails.eventConditions is required."}],"code":400,"message":"Field eventDetails.eventConditions is required."}}" on line 118 of /usr/local/var/www/htdocs/plataforma/vendor/google/apiclient/src/Google/Http/REST.php

My question is, could anyone give me strength in this mistake? I do not know what to do anymore ...

Thanks everyone!

    
asked by anonymous 13.06.2018 / 15:55

0 answers