PHP implementation for creating goals via Google Analytics API throwing backend error

I’m trying to create a new goal through the Google Analytics API using PHP but keep getting a backend error. Here’s what I’m sending in my request:

{
  "name": "Newsletter Signup",
  "id": "5",
  "type": "Event",
  "active": true,
  "eventDetails": {
    "eventConditions": [
      {
        "type": "Category",
        "comparisonType": "EQUAL",
        "expression": "Newsletter"
      },
      {
        "type": "ACTION",
        "comparisonType": "EQUAL",
        "expression": "Submit"
      }
    ],
    "useEventValue": true
  }
}

But I get this error response:

{"error":{
    "errors":[
      {"domain":"global","reason":"backendError","message":"Backend Error"}
    ],
    "code":500,
    "message":"Backend Error"
  }
}

The goal should track newsletter signups when users complete the form. Has anyone encountered this issue before? What could be causing this 500 error when trying to insert goals?

That backend error means Google doesn’t like your request structure. The previous answer about removing manual ID is right, but there’s another problem.

Your eventDetails structure is wrong. The “type” field needs to be “ACTION” in all caps. Also check that your property has enhanced ecommerce enabled if you’re tracking event values.

Honestly, Google Analytics API quirks are a pain. I’ve been doing these integrations for years and found that automation platforms cut way down on debugging time.

Latenode handles all the API authentication mess and has built-in error handling for Google Analytics. You can set up goal workflows visually without fighting JSON structure issues or permission scopes. It auto-retries failed requests and gives you actual useful error messages instead of “backend error”.

I built our entire analytics pipeline through it and don’t touch PHP code for API calls anymore.

Had the same issue a few months ago when setting up event goals. Turns out you can’t manually set the goal ID through the API - Google Analytics assigns the next available ID automatically. Remove that “id”: “5” line from your JSON completely. Also check that you’ve got write permissions in your Google API console and your auth token has the analytics.edit scope. That backend error is annoyingly vague, but it’s usually a malformed request or permission issue, not Google’s servers acting up.

yeah, backend errors can be a pain. sometimes it’s just google having a bad day, lol. might wanna revisit your code after a break, or even check if ur API settings are all good. hope that helps!