Skip to main content
Solved

How to add custom properties to a Profile with a value that is NOT an array?

  • December 11, 2024
  • 3 replies
  • 35 views

Forum|alt.badge.img

When I use this endpoint, any custom properties I try to create have their values transformed into an array that holds that value as its first value. Does anyone know a workaround or a way to escalate this to engineering to fix?
https://developers.klaviyo.com/en/reference/create_or_update_profile

Example:

$payload = [
    'data' => [
        'type'       => 'profile',
        'attributes' => $attributes,
        'meta'       => [
            'patch_properties' => [
                'append' => [
                    'GN_custom' => 'value goes here',
                ],
            ],
        ],
    ],
];
$response = $this->client->Profiles->createOrUpdateProfile($payload);


I would expect my custom property `GN_custom` to have a value of "value goes here" but instead it is wrapped in an array like this ["value goes here"].

At first I thought it was a problem with the PHP SDK that we’re using, but after debugging it appears to be just the way the API endpoint handles custom properties. IMO it’s broken.

Thanks in advance!

Best answer by sgruenholz

OK, after doing some more testing, I finally figured out a combination that works:

  1. Set the properties array in the attributes. (This is the first thing I tried but doesn’t work by itself)
  2. In the meta you must both `append` and `unappend` those same properties.
// email, name, etc go in here.
$attributes    = [];

$properties    = [
    'winner' => "chicken dinner",
];

$attributes['properties'] = $properties;

$payload = [
    'data' => [
        'type'       => 'profile',
        'attributes' => $attributes,
        'meta'       => [
            // You have to append and then unappend the same properties
            'patch_properties' => [
                'append'   => $properties,
                'unappend' => $properties,
            ],
        ],
    ],
];

$response = $this->client->Profiles->createOrUpdateProfile($payload);

And the custom properties finally get set correctly.

Really weird way to do things but it works. Hope this helps someone else!

View original
Did this topic or the replies in the thread help you find an answer to your question?

3 replies

ArpitBanjara
Principal User I
Forum|alt.badge.img+36
  • Principal User I
  • 371 replies
  • December 12, 2024

Hey ​@sgruenholz 

I am not entirely sure about this, but can you try to use the set operation instead of append. The set operation replaces or sets the value of a property without wrapping it in an array.

$payload = [
    'data' => [
        'type'       => 'profile',
        'attributes' => $attributes,
        'meta'       => [
            'patch_properties' => [
                'set' => [
                    'GN_custom' => 'value goes here',
                ],
            ],
        ],
    ],
];
$response = $this->client->Profiles->createOrUpdateProfile($payload);

let me know if this works.

thanks

Arpit


Forum|alt.badge.img
  • Author
  • Contributor II
  • 3 replies
  • December 12, 2024

Thanks for the suggestion! I tried that but the API fails with a 400 Bad Request. 

Based on the descriptions in the docs, it sure seems like I’m using the correct signature. They only list three patch commands: 

  • append
  • unappend
  • unset

 


Forum|alt.badge.img
  • Author
  • Contributor II
  • 3 replies
  • Answer
  • December 12, 2024

OK, after doing some more testing, I finally figured out a combination that works:

  1. Set the properties array in the attributes. (This is the first thing I tried but doesn’t work by itself)
  2. In the meta you must both `append` and `unappend` those same properties.
// email, name, etc go in here.
$attributes    = [];

$properties    = [
    'winner' => "chicken dinner",
];

$attributes['properties'] = $properties;

$payload = [
    'data' => [
        'type'       => 'profile',
        'attributes' => $attributes,
        'meta'       => [
            // You have to append and then unappend the same properties
            'patch_properties' => [
                'append'   => $properties,
                'unappend' => $properties,
            ],
        ],
    ],
];

$response = $this->client->Profiles->createOrUpdateProfile($payload);

And the custom properties finally get set correctly.

Really weird way to do things but it works. Hope this helps someone else!