I am trying to track user activity with Customer.io on my site. I am sending curl requests with user info, page views and triggered events (purchases). For some reason the curl requests are being sent out twice.
I have the below bit of code in my footer.php file. (purchase curl req is not shown as its in a different file).
<?php
$current_user = wp_get_current_user();
if ($current_user instanceof WP_User) {
$userId = $current_user->ID;
$userEmail = $current_user->user_email;
$userRegisterDate = strtotime($current_user->user_registered);
$userFirstName = $current_user->user_firstname;
if (($userId && $userEmail && $userRegisterDate && $userFirstName)) {
$session = curl_init();
$customer_id = $userId;
$customerio_url = 'https://track.customer.io/api/v1/customers/';
$site_id = 'xxxxxxxx';
$api_key = 'xxxxxxxx';
$data = array('email' => $userEmail, 'first_name' => $userFirstName, 'created_at' => $userRegisterDate);
curl_setopt($session, CURLOPT_URL, $customerio_url . $customer_id);
curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($session, CURLOPT_HTTPGET, 1);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($session, CURLOPT_VERBOSE, 1);
curl_setopt($session, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($session, CURLOPT_USERPWD, $site_id . ':' . $api_key);
curl_exec($session);
curl_close($session);
$session = curl_init();
$customerio_url = 'https://track.customer.io/api/v1/customers/' . $customer_id . '/events';
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$data = array('name' => $actual_link, 'type' => 'page', 'data[referrer]' => 'http://www.spanishvirtually.com');
curl_setopt($session, CURLOPT_URL, $customerio_url);
curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_VERBOSE, 1);
curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($session, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($session, CURLOPT_USERPWD, $site_id . ':' . $api_key);
curl_exec($session);
curl_close($session);
}
}
?>
When I check my activity log in customer.io, all of the page event views are duplicated. It seems that the curl requests are being sent out twice. I am assuming that the footer.php page is loading/being called twice.
How can I prevent that from happening?
I also have similar code in a custom woocommerce template file (theme/woocommerce/checkout/thankyou.php and it too is sending the curl request twice)
This isn’t standard behavior, but you can stop it by wrapping the code in a statement to see if it’s already ran.