Skip to content

Wrong format for userProperties sent to GA4. #25

@bnoss

Description

@bnoss

UserProperties::export() returns data in wrong format, which ends with an error

"Unable to parse Measurement Protocol JSON payload. invalid JSON in google.analytics.measurement.Measurement @ user_properties: message google.analytics.measurement.Measurement.UserPropertiesEntry, near 1:333 (offset 332): unexpected character: '['; expected '{'"

The data has to be sent as an key->value paired array/object to analytics' endpoint (as described in its docs).

Suggest alternative to the original lines

    public function export(): array
    {
        return array_reduce($this->getUserPropertiesList(), function ($last, UserProperty $userProperty) {
            return array_merge($last, $userProperty->export());
        }, []);
    }

as in the following sample:

    public function export(): array
    {
        $return = [];
        foreach ($this->getUserPropertiesList() as $userProperty) {
            $return[array_keys($userProperty->export())[0]] = array_values($userProperty->export())[0];
        }
        return $return;
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions