Solved

When does Klaviyo create a profile?

  • 25 January 2023
  • 1 reply
  • 273 views

Badge +1

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_result['exists']) {
    // Get the current URI
    $current_uri = $_SERVER['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 :)

icon

Best answer by Brian Turcotte 26 January 2023, 04:29

View original

1 reply

Userlevel 7
Badge +36

Hi @cesvi87 and welcome to the Community!

 

This is a very interesting use case, so I’m happy to look into it for you. Just for some context, here’s our Help Center Article describing Created vs. First Active dates:

 

The key distinction there is that First Active represents the first date that a user proactively interacts with your site, whereas Profile Created represents the date that profile was created within your Klaviyo account. These dates can thereby differ depending on how your data was originally synced.

 

In order for a profile to be created, the site visitor’s browser has to be cookied somehow, which is usually done when a user enters their email in a signup form or checkout field. In regard to using the APIs to automatically assign a language property, I will have to look into that further and update the thread as soon as I dig up more information, since this isn’t a documented feature. It’s also possible that one of our wonderful Klaviyo Official Partners may be able to assist you with this development task.

 

In the meantime, here are some of the ways we usually suggest to collect users’ language preference:

 

Thanks for using the Community!

- Brian

Reply