Skip to content

Commit

Permalink
Fix doc errors. Add Conversions API request logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
message-dimke committed Aug 7, 2023
1 parent 47a9b4d commit cada25b
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 144 deletions.
118 changes: 0 additions & 118 deletions src/API/Conversions.php

This file was deleted.

13 changes: 10 additions & 3 deletions src/Tracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ public function handle_view_category() {
*
* @since x.x.x
*
* @param string $cart_item_key - WooCommerce cart item key.
* @param string $product_id - WooCommerce product id.
* @param string $quantity - Number of products.
* @param string $variation_id - Product variation id if any.
*
* @return void
*/
public function handle_add_to_cart( $cart_item_key, $product_id, $quantity, $variation_id ) {
Expand All @@ -136,6 +141,8 @@ public function handle_add_to_cart( $cart_item_key, $product_id, $quantity, $var
*
* @since x.x.x
*
* @param string $order_id - WooCommerce order id.
*
* @return void
*/
public function handle_checkout( $order_id ) {
Expand Down Expand Up @@ -210,7 +217,7 @@ public function handle_search() {
*
* @return void
*/
public function track_event(string $event_name, Data $data ) {
public function track_event( string $event_name, Data $data ) {
foreach ( $this->get_trackers() as $tracker ) {
// Skip Pinterest tag tracking if tag is not active.
if ( $tracker instanceof Tag && ! Tag::get_active_tag() ) {
Expand All @@ -236,7 +243,7 @@ public function get_trackers() {
*
* @since x.x.x
*
* @param Tracker $tracker
* @param Tracker $tracker - One of objects implementing Tracker interface.
*
* @return void
*/
Expand All @@ -249,7 +256,7 @@ public function add_tracker( Tracker $tracker ) {
*
* @since x.x.x
*
* @param string $tracker Tracker class name to be removed. e.g. Tag::class, Conversions::class
* @param string $tracker Tracker class name to be removed. e.g. Tag::class, Conversions::class.
*
* @return void
*/
Expand Down
21 changes: 19 additions & 2 deletions src/Tracking/Conversions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Automattic\WooCommerce\Pinterest\Tracking;

use Automattic\WooCommerce\Pinterest\API\APIV5;
use Automattic\WooCommerce\Pinterest\Logger as Logger;
use Automattic\WooCommerce\Pinterest\Tracking;
use Automattic\WooCommerce\Pinterest\Tracking\Data\Category;
use Automattic\WooCommerce\Pinterest\Tracking\Data\Checkout;
Expand Down Expand Up @@ -39,7 +40,7 @@ class Conversions implements Tracker {
/**
* Pinterest Conversions API class constructor.
*
* @param User $user
* @param User $user - User data object to hold ip address and user agent string.
*/
public function __construct( User $user ) {
$this->user = $user;
Expand Down Expand Up @@ -171,13 +172,29 @@ function ( Product $product ) {
);

try {
/* Translators: 1: Conversions API event name, 2: JSON encoded event data. */
$messages = sprintf(
'Sending Pinterest Conversions API event %1$s with a payload: %2$s',
$event_name,
json_encode( $data )
);
Logger::log( $messages, 'debug', 'conversions' );

APIV5::make_request(
"ad_accounts/{$ad_account_id}/events",
'POST',
array( 'data' => array( $data ) )
);
} catch ( Throwable $e ) {
// Do nothing.
/* Translators: 1: Conversions API event name, 2: JSON encoded event data, 3: Error code, 4: Error message. */
$messages = sprintf(
'Sending Pinterest Conversions API event %1$s with a payload %2$s has failed with the error %3$d code and %4$s mesasge',
$event_name,
json_encode( $data ),
$e->getCode(),
$e->getMessage()
);
Logger::log( $messages, 'error', 'conversions' );
}
}
}
22 changes: 22 additions & 0 deletions src/Tracking/Data.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,37 @@
<?php
/**
* Pinterest tracking base data class.
*
* @package Pinterest_For_WooCommerce/Classes/
* @version 1.0.0
*/

namespace Automattic\WooCommerce\Pinterest\Tracking;

/**
* Common data class to store event related data.
*/
class Data {

/**
* A unique event id.
*
* @var string
*/
private $event_id;

/**
* @param string $event_id - A unique event id.
*/
public function __construct( $event_id ) {
$this->event_id = $event_id;
}

/**
* Get unique event id.
*
* @return string
*/
public function get_event_id() {
return $this->event_id;
}
Expand Down
10 changes: 10 additions & 0 deletions src/Tracking/Data/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,18 @@
*/
class Category extends Data {

/**
* WooCommerce category id.
*
* @var string
*/
private $id;

/**
* Category name.
*
* @var string
*/
private $name;

/**
Expand Down
26 changes: 18 additions & 8 deletions src/Tracking/Data/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,45 @@
class Checkout extends Data {

/**
* @var string WooCommerce order id.
* WooCommerce order id.
*
* @var string
*/
private $order_id;

/**
* @var string WooCommerce order amount.
* WooCommerce order amount.
*
* @var string
*/
private $price;

/**
* @var int Number of items in the order.
* Number of items in the order.
*
* @var int
*/
private $quantity;

/**
* @var string order currency code.
* Order currency code.
*
* @var string
*/
private $currency;

/**
* @var Product[] an array of products involved into checkout.
* An array of products involved into checkout.
*
* @var Product[]
*/
private $items;

/**
* @param string $event_id - A unique event id as a requirement by Pinterest for deduplication purposes.
* @param $order_id - WooCommerce Order ID.
* @param $price - WooCommerce total order amount.
* @param $quantity - Number of items.
* @param string $order_id - WooCommerce Order ID.
* @param string $price - WooCommerce total order amount.
* @param string $quantity - Number of items.
* @param string $currency - Order currency.
* @param Product[] $items - An array of ordered items.
*/
Expand Down
3 changes: 3 additions & 0 deletions src/Tracking/Data/None.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
*/
class None extends Data {

/**
* @param string $event_id - A unique event id.
*/
public function __construct( $event_id ) {
parent::__construct( $event_id );
}
Expand Down
35 changes: 35 additions & 0 deletions src/Tracking/Data/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,53 @@
*/
class Product extends Data {

/**
* Product WooCommerce id.
*
* @var string
*/
private $id;

/**
* Product name.
*
* @var string
*/
private $name;

/**
* Product category.
*
* @var string
*/
private $category;

/**
* Product brand.
*
* @var string
*/
private $brand;

/**
* Product price.
*
* @var string
*/
private $price;

/**
* Currency code.
*
* @var string
*/
private $currency;

/**
* Number of products.
*
* @var string
*/
private $quantity;

/**
Expand Down
Loading

0 comments on commit cada25b

Please sign in to comment.