Skip to content

Commit

Permalink
Merge pull request #18 from Synerise/GAS-1309
Browse files Browse the repository at this point in the history
GAS-1309: Not sending events without uuid
  • Loading branch information
kamilkanik authored Sep 8, 2023
2 parents d38de72 + 435c138 commit af2461e
Show file tree
Hide file tree
Showing 33 changed files with 155 additions and 89 deletions.
17 changes: 6 additions & 11 deletions lib/integration-core/Tracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ public function __construct(
*/
public function getClientUuid()
{
if ($this->isAdminStore()) {
return null;
}

if (!$this->clientUuid) {
$this->clientUuid = $this->getClientUuidFromCookie();
}
Expand Down Expand Up @@ -153,7 +149,7 @@ public function isAdminStore()
/**
* @param $email
* @return string|null
* @throws MergeException
* @throws InputException
*/
public function manageClientUuid($email)
{
Expand All @@ -162,17 +158,16 @@ public function manageClientUuid($email)
}

$uuid = $this->getClientUuidFromCookie();
$emailUuid = Uuid::generateUuidByEmail($email);

if(!$uuid) {
return;
return $emailUuid;
}

$emailUuid = Uuid::generateUuidByEmail($email);

// Email uuid already set
if ($uuid == $emailUuid) {
// email uuid already set
return;
return $emailUuid;
}

// reset uuid via cookie
Expand All @@ -181,7 +176,7 @@ public function manageClientUuid($email)
$identityHash = $this->getCookieParam('identityHash');
if ($identityHash && $identityHash != $this->hashString($email)) {
// Different user, skip merge.
return;
return $emailUuid;
}

try{
Expand All @@ -190,7 +185,7 @@ public function manageClientUuid($email)
$this->logger->error(Logger_Service::addExceptionToMessage('Client update with uuid reset failed', $e));
}

return $this->clientUuid;
return $emailUuid;
}

protected static function hashString($s)
Expand Down
2 changes: 1 addition & 1 deletion public/css/synerise-for-woocommerce.min.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/js/synerise-for-woocommerce.min.js

Large diffs are not rendered by default.

35 changes: 26 additions & 9 deletions src/Event/class-event-add-to-cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Synerise\Integration\Service\Product_Service;
use Synerise\Integration\Mapper\Client_Action;
use Synerise\Integration\Service\Cart_Service;
use Synerise\IntegrationCore\Uuid;

if (! defined('ABSPATH')) {
exit;
Expand All @@ -18,38 +19,54 @@ class Event_Add_To_Cart extends Abstract_Event

public function execute(string $cart_item_key, $quantity = 1)
{
if (!$this->is_event_enabled() || !$this->is_product_added($cart_item_key)) {
if (!$this->is_event_enabled()) {
return;
}

if(Product_Service::is_sku_item_key() && !Cart_Service::cart_item_has_sku($cart_item_key)){
return;
}

if (!$this->tracking_manager->getClientUuid()) {
return;
}

try {
$this->process_event($this->prepare_event($cart_item_key, $quantity));
$payload = $this->prepare_event($cart_item_key, $quantity);
if($payload){
$this->process_event($payload);
}
} catch (\Exception $e) {
$this->logger->error(Logger_Service::addExceptionToMessage('Synerise Event processing failed', $e));
}
}

public function prepare_event($cart_item_key, $quantity = 1): string
public function prepare_event($cart_item_key, $quantity = 1)
{
$defaultParams = [
'source' => Client_Action::get_source()
];

$uuid = $this->tracking_manager->getClientUuid();
if (!$uuid) {
$wp_user = wp_get_current_user();
if(!$wp_user || $wp_user->ID === 0){
return null;
}

$uuid = Uuid::generateUuidByEmail($wp_user->user_email);
}

$params = Cart_Service::prepare_add_to_cart_product_data($cart_item_key, $quantity);
$params = array_merge($params, $defaultParams);

return \GuzzleHttp\json_encode(array_filter([
'time' => Client_Action::get_time(new \DateTime()),
'label' => Client_Action::get_label(self::EVENT_NAME),
'client' => [
'uuid' => $this->tracking_manager->getClientUuid(),
'uuid' => $uuid,
],
'params' => $params
]));
}

protected function is_product_added($cart_item_key)
{
return !Product_Service::is_sku_item_key() || Cart_Service::cart_item_has_sku($cart_item_key);
}
}
22 changes: 17 additions & 5 deletions src/Event/class-event-cart-status.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Synerise\Integration\Logger_Service;
use Synerise\Integration\Mapper\Client_Action;
use Synerise\Integration\Service\Cart_Service;
use Synerise\IntegrationCore\Uuid;

if (! defined('ABSPATH')) {
exit;
Expand All @@ -22,17 +23,28 @@ public function execute()
}

try {
$this->process_event($this->prepare_event());

define('SYNERISE_CART_STATUS_SENT', true);
$payload = $this->prepare_event();
if($payload){
$this->process_event($payload);
define('SYNERISE_CART_STATUS_SENT', true);
}

} catch (\Exception $e) {
$this->logger->error(Logger_Service::addExceptionToMessage('Synerise Event processing failed', $e));
}
}

public function prepare_event(): string
public function prepare_event()
{
$uuid = $this->tracking_manager->getClientUuid();
if (!$uuid) {
$wp_user = wp_get_current_user();
if(!$wp_user || $wp_user->ID === 0){
return null;
}
$uuid = Uuid::generateUuidByEmail($wp_user->user_email);
}

$cart = WC()->cart;
$cart_status_params = Cart_Service::prepare_cart_status_params($cart);

Expand All @@ -42,7 +54,7 @@ public function prepare_event(): string
'action' => 'cart.status',
'label' => 'CartStatus',
'client' => [
"uuid" => $this->tracking_manager->getClientUuid(),
"uuid" => $uuid,
],
'params' => $cart_status_params
]
Expand Down
5 changes: 2 additions & 3 deletions src/Event/class-event-client-login.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Synerise\Integration\Logger_Service;
use Synerise\Integration\Service\Client_Service;
use Synerise\Integration\Mapper\Client_Action;
use Synerise\IntegrationCore\Uuid;

if (! defined('ABSPATH')) {
exit;
Expand Down Expand Up @@ -37,14 +38,12 @@ public function prepare_event(string $username): string

$customer = new \WC_Customer($wp_user->ID);

$this->tracking_manager->manageClientUuid($wp_user->user_email);

return \GuzzleHttp\json_encode(
[
'time' => Client_Action::get_time(new \DateTime()),
'label' => Client_Action::get_label(self::EVENT_NAME),
'client' => [
'uuid' => $this->tracking_manager->getClientUuid(),
'uuid' => $this->tracking_manager->manageClientUuid($wp_user->user_email),
'email' => $wp_user->user_email,
],
'params' => Client_Service::prepare_client_params($customer)
Expand Down
28 changes: 19 additions & 9 deletions src/Event/class-event-client-logout.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,51 @@

use Synerise\Integration\Logger_Service;
use Synerise\Integration\Mapper\Client_Action;
use Synerise\IntegrationCore\Uuid;

if (! defined('ABSPATH')) {
exit;
}

class Event_Client_Logout extends Abstract_Event
{
const HOOK_NAME = 'clear_auth_cookie';
const HOOK_NAME = 'wp_logout';
const EVENT_NAME = 'logout';

public function execute()
public function execute(int $user_id)
{
if (!$this->is_event_enabled()) {
return;
}

if(!$this->tracking_manager->getClientUuid()){
return;
}

try {
$this->process_event($this->prepare_event());
$payload = $this->prepare_event($user_id);
if($payload){
$this->process_event($this->prepare_event($user_id));
}
} catch (\Exception $e) {
$this->logger->error(Logger_Service::addExceptionToMessage('Synerise Event processing failed', $e));
}
}

public function prepare_event()
public function prepare_event($user_id)
{
$uuid = $this->tracking_manager->getClientUuid();
if(!$uuid){
$wp_user = \WP_User::get_data_by('id', $user_id);
if(!$wp_user || $wp_user->ID === 0){
return null;
}

$uuid = Uuid::generateUuidByEmail($wp_user->user_email);
}

return \GuzzleHttp\json_encode(
[
'time' => Client_Action::get_time(new \DateTime()),
'label' => Client_Action::get_label(self::EVENT_NAME),
'client' => [
'uuid' => $this->tracking_manager->getClientUuid()
'uuid' => $uuid
]
]
);
Expand Down
10 changes: 7 additions & 3 deletions src/Event/class-event-order-placed.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Synerise\Integration\Mapper\Client_Action;
use Synerise\Integration\Service\Client_Service;
use Synerise\Integration\Service\Order_Service;
use Synerise\IntegrationCore\Uuid;

if (! defined('ABSPATH')) {
exit;
Expand All @@ -28,6 +29,11 @@ public function execute(int $order_id)

try {
$order = wc_get_order($order_id);

if (!$order->get_billing_email()) {
return;
}

$this->process_event($this->prepare_event($order));

if (!is_user_logged_in()) {
Expand All @@ -42,8 +48,6 @@ public function execute(int $order_id)

public function prepare_event(\WC_Order $order)
{
$this->tracking_manager->manageClientUuid($order->get_billing_email());

$order_data = Order_Service::prepare_order_params($order);
if(empty($order_data['products'])){
return null;
Expand All @@ -53,7 +57,7 @@ public function prepare_event(\WC_Order $order)
'time' => Client_Action::get_time(new \DateTime()),
'label' => Client_Action::get_label(self::EVENT_NAME),
'client' => [
'uuid' => $this->tracking_manager->getClientUuid(),
'uuid' => $this->tracking_manager->manageClientUuid($order->get_billing_email()),
'email' => $order->get_billing_email()
],
'source' => Client_Action::get_source()
Expand Down
24 changes: 19 additions & 5 deletions src/Event/class-event-product-review.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ public function execute($comment_id) {
return;
}

$comment = get_comment($comment_id);
if (!$this->tracking_manager->getClientUuid()) {
return;
}

$comment = get_comment($comment_id);
$post = get_post($comment->comment_post_ID);

if(is_null($post) || strpos($post->post_type, 'product') === false) {
Expand All @@ -32,6 +36,10 @@ public function execute($comment_id) {
return;
}

if(!$this->tracking_manager->getClientUuid() && empty($comment->comment_author_email)) {
return;
}

try {
$this->process_event($this->prepare_event($comment));
} catch (\Exception $e) {
Expand All @@ -41,28 +49,34 @@ public function execute($comment_id) {

public function prepare_event($comment)
{
$uuid = $this->tracking_manager->getClientUuid();

$client = [];
if($comment->user_id == 0){
if(!empty($comment->comment_author_email) || !empty($comment->comment_author)) {
$client_data = [];

if ( ! empty( $comment->comment_author_email ) ) {
$this->tracking_manager->manageClientUuid( $comment->comment_author_email );
$uuid = $this->tracking_manager->manageClientUuid( $comment->comment_author_email );
$client_data['email'] = $comment->comment_author_email;
}

if ( ! empty( $comment->comment_author ) ) {
$client_data['displayName'] = $comment->comment_author;
}

$client_data['uuid'] = $this->tracking_manager->getClientUuid();
$this->process_client_update(\GuzzleHttp\json_encode([$client_data]));
if ($uuid) {
$client_data['uuid'] = $uuid;
$this->process_client_update(\GuzzleHttp\json_encode([$client_data]));
}
}
} else {
$client['custom_id'] = $comment->user_id;
}

$client['uuid'] = $this->tracking_manager->getClientUuid();
if ($uuid) {
$client['uuid'] = $uuid;
}

$params = Review_Service::get_review_data($comment);

Expand Down
Loading

0 comments on commit af2461e

Please sign in to comment.