google-api-php-client icon indicating copy to clipboard operation
google-api-php-client copied to clipboard

Cannot Unset Google Analytics Goal Url Destination Details

Open judahnator opened this issue 5 years ago • 7 comments

Environment details

  • OS: Ubuntu 18.04.2 LTS via Laravel Homestead (Vagrant Box)
  • PHP version: 7.3.4
  • Package name and version: google/apiclient v2.2.2

Steps to reproduce

  1. Create a goal within Google Analytics (either via the API or online dashboard, dashboard is easier IMO) * This goal should be the DESTINATION type * Fill out the rest of the information relevant to that event type, the details don't matter, just the goal type
  2. Attempt to update the goal via this API using the example below to an EVENT type
  3. I did not test to see if this is an issue with other event types, just DESTINATION -> EVENT

Expected Result

I expect that the existing DESTINATION type goal would be updated to an EVENT type goal with the information provided.

Actual Result

An exception is thrown (see the exception JSON example below) describing the urlDestinationDetails must not be provided for the EVENT goal type, even though that information is not actually provided.

Proposed Solution

Although I don't know if the problem is client or server side, if it is client side then maybe the ability to explicitly set the urlDestinationDetails as null or void would help. You can see here in the Google PHP Client Service library that we have the option to set the destination details, but no option to unset it.

Code example

An excerpt of my code:

// This block sets up the Analytics goal event details
// The process is a bit verbose but I am including for debugging
// Skip to where we define $googleGoal if you don't care about this bit
$category = new Google_Service_Analytics_GoalEventDetailsEventConditions();
$category->setExpression("Foo Category");
$category->setMatchType('EXACT');
$category->setType('CATEGORY');

$action = new Google_Service_Analytics_GoalEventDetailsEventConditions();
$action->setExpression("Foo Action");
$action->setMatchType('EXACT');
$action->setType('ACTION');

$label = new Google_Service_Analytics_GoalEventDetailsEventConditions();
$label->setExpression("Foo Label");
$label->setMatchType('EXACT');
$label->setType('LABEL');

$events = new Google_Service_Analytics_GoalEventDetails();
$events->setEventConditions([
    $category,
    $action,
    $label
]);
$events->setUseEventValue(true);

// Ok, now we can set up the actual goal
$googleGoal = new Google_Service_Analytics_Goal();
$googleGoal->setActive(true);
$googleGoal->setName("Foo Name");
$googleGoal->setType('EVENT');
$googleGoal->setEventDetails($events);

// In this specific case I am replacing an inactive goal, and the id might not actually be no.1
// The goal I am replacing is the "DESTINATION" type
$googleGoal->setId(1);

// I am defining $googleAnalytics, $webproperty, and $profile elsewhere
/** @var Google_Service_Analytics $googleAnalytics */
/** @var Google_Service_Analytics_Webproperty $webproperty */
/** @var Google_Service_Analytics_Profile $profile */

// Perform the patch event, this throws a Google_Service_Exception exception, see the exception below
$googleAnalytics->management_goals->patch(
    $webproperty->getAccountId(),
    $webproperty->getId(),
    $profile->getId(),
    $googleGoal->getId(),
    $googleGoal
);

The exception I am seeing:

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

judahnator avatar May 14 '19 15:05 judahnator

Hello! I have you tried the following?

$googleGoal->urlDestinationDetails = Google_Model::NULL_VALUE;

bshaffer avatar May 14 '19 21:05 bshaffer

I had not thought to try that, as that property does not exist within the Google_Service_Analytics_Goal class. It did the trick though.

Would the Google folks be receptive to a pull request on the client services library to address this issue? The fix would be as simple as this:

<?php

class Google_Service_Analytics_Goal extends Google_Model
{
  // snip...
  
  protected $urlDestinationDetails = Google_Model::NULL_VALUE;
  
  // snip...

  /**
   * @param Google_Service_Analytics_GoalUrlDestinationDetails|null
   */
  public function setUrlDestinationDetails(?Google_Service_Analytics_GoalUrlDestinationDetails $urlDestinationDetails)
  {
    $this->urlDestinationDetails = $urlDestinationDetails ?: Google_Model::NULL_VALUE;
  }
  /**
   * @return Google_Service_Analytics_GoalUrlDestinationDetails|null
   */
  public function getUrlDestinationDetails()
  {
    return $this->urlDestinationDetails instanceof Google_Service_Analytics_GoalUrlDestinationDetails 
      ? $this->urlDestinationDetails
      : null;
  }

  // snip...

}

judahnator avatar May 15 '19 14:05 judahnator

Unfortunately the classes in that repository are auto-generated, so modifications to them would be overwritten by the next update. We are tracking this issue for future improvements to the code generator to make this easier to use and understand. Apologies for the inconvenience, and thanks for the offer of help! :)

Since this is fixed now, I'm going to close the issue. Please let us know if you have any other questions!

jdpedrie avatar May 15 '19 15:05 jdpedrie

@jdpedrie , I'd like to leave this open :).

@bshaffer and I have been discussing some options that would address this case more gracefully, as setting a public property isn't an ideal solution.

dwsupplee avatar May 15 '19 15:05 dwsupplee

Also happening for the visitTimeOnSiteDetails property. Would be nice if those defaulted to null so you did not have to set nonexistant properties to null values for it to work.

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

judahnator avatar May 23 '19 15:05 judahnator

Any movement on this? Anything I can do to help?

judahnator avatar Jun 10 '19 16:06 judahnator

Its been a few days shy of a year since this issue was opened. Has there been any progress? Anything I can do to help?

judahnator avatar May 11 '20 14:05 judahnator