Hi,
I’m trying to develop a Wordpress script that adds a language custom property to a user depending if the website it’s showing in English, Spanish or Portuguese.
I know that from the first moment a user is visiting a website Klaviyo retrieves it’s data and prepares the data model until user gives his email. My question is, in which moment can I add a custom property to the user? For the moment I’ve developed it like when the user clicks on PLACE ORDER (assuming email has been introduced) then I add ‘language’ property to the user.
But the question is? I could do it before? How could I get the current user that’s surfing my website?
Here’s a piece of the code I’m developing:
function setKlaviyoLanguage() {
// Get the current user's email
global $current_user;
get_currentuserinfo();
$email = $current_user->user_email; // ???
// Check if the user exists in Klaviyo
$api_key = 'API_KEY';
$check_url = 'https://a.klaviyo.com/api/v2/customer/' . $email . '/exists?api_key=' . $api_key;
$check_result = file_get_contents($check_url);
$check_result = json_decode($check_result, true);
if ($check_resultt'exists']) {
// Get the current URI
$current_uri = $_SERVERR'REQUEST_URI'];
// Check if the URI contains /en/
if (strpos($current_uri, '/en/') !== false) {
$language = 'en';
}
// Check if the URI contains /pt-br/
elseif (strpos($current_uri, '/pt-br/') !== false) {
$language = 'pt-br';
}
// If neither /en/ or /pt-br/ is present, set language to 'es'
else {
$language = 'es';
}
// Connect to Klaviyo API
$data = array(
'api_key' => $api_key,
'email' => $email,
'properties' => array(
'lan' => $language
)
);
$url = 'https://a.klaviyo.com/api/v2/customer/';
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
}
}
Red text it’s where I am assuming that I’ll get the user in base of the mail introduced at checkout fields by the user.
Hope to hear from you, Klaviyo Support Team :)