Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Events support #115

Merged
30 commits merged into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 60 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The plugin provides an SDK client to easily integrate with Omnisend API.
> To use in your plugin you must check if wp-omnisend plugin is installed.
> Provided client will send data to Omnisend if it is connected.

You can find function references in the [client folder](https://github.com/omnisend/wp-omnisend/tree/main/omnisend/includes/Public/V1).
You can find function references in the [client folder](https://github.com/omnisend/wp-omnisend/tree/main/omnisend/includes/SDK/V1).

### Examples

Expand Down Expand Up @@ -46,33 +46,54 @@ This is done by getting an actual client
Here is how you can create a basic client & submit contact.

```php
$contact = new Contact();

$contact->set_email( $email );
if ( $phone_number != '' ) {
$contact->set_phone( $phone_number );
}
$contact->set_first_name( $first_name );
$contact->set_last_name( $last_name );
$contact->set_birthday( $birthday );
$contact->set_postal_code( $postal_code );
$contact->set_address( $address );
$contact->set_state( $state );
$contact->set_country( $country );
$contact->set_city( $city );
if ( $email_consent ) {
$contact->set_email_consent( 'actual_email_consent_for_gdrp' );
$contact->set_email_opt_in( 'where user opted to become subscriber' );
}

$client = \Omnisend\SDK\V1\Omnisend::get_client( 'integration name', 'integration version' );

$response = $client->create_contact( $contact );
$contact = new Contact();

$contact->set_email( $email );
if ( $phone_number != '' ) {
$contact->set_phone( $phone_number );
}
$contact->set_first_name( $first_name );
$contact->set_last_name( $last_name );
$contact->set_birthday( $birthday );
$contact->set_postal_code( $postal_code );
$contact->set_address( $address );
$contact->set_state( $state );
$contact->set_country( $country );
$contact->set_city( $city );
if ( $email_consent ) {
$contact->set_email_consent( 'actual_email_consent_for_gdrp' );
$contact->set_email_opt_in( 'where user opted to become subscriber' );
}
$client = \Omnisend\SDK\V1\Omnisend::get_client( 'integration name', 'integration version' );

$response = $client->create_contact( $contact );
```

#### Customer events

Here is how you can send customer events.

```php
$contact = new Contact();
$contact->set_email( $email );

$event = new Event();
$event->set_contact( $contact );
$event->set_origin( 'api' );
$event->set_event_name( 'site viewed' );
$event->add_properties( 'pageUrl', $pageUrl );
$event->add_properties( 'pageTitle', $pageTitle );

$client = \Omnisend\SDK\V1\Omnisend::get_client( 'integration name', 'integration version' );

$response = $client->send_customer_event($event);
```

You can send contact identifiers and if contact exists, then event will be attributed for this contact, if not - new contact will be created and event will be attributed to this new contact

#### Error handling

If data provided is invalid or creation fails, then
If data provided is invalid or contact creation fails, then

```php
$response = $client->create_contact($contact)
Expand All @@ -87,6 +108,21 @@ Will return `CreateContactResponse`. Depending on your integration logic you sho
}
```

If data provided is invalid or sending customer event fails, then

```php
$response = $client->send_customer_event($event);
```

Will return `SendCustomerEventResponse`. Depending on your integration logic you should handle the error i.e

```php
if ( $response->get_wp_error()->has_errors() ) {
error_log( 'Error in after_submission: ' . $response->get_wp_error()->get_error_message());
return;
}
```

## PHP Linting

WordPress.org team mandates our plugin to be linted
Expand Down
1 change: 1 addition & 0 deletions omnisend/class-omnisend-core-bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

// Change for different environment.
const OMNISEND_CORE_API_V3 = 'https://api.omnisend.com/v3';
greta-mik marked this conversation as resolved.
Show resolved Hide resolved
const OMNISEND_CORE_API_V5 = 'https://api.omnisend.com/v5';
const OMNISEND_CORE_SNIPPET_URL = 'https://omnisnippet1.com/inshop/launcher-v2.js';

// Omnisend for Woo plugin.
Expand Down
52 changes: 51 additions & 1 deletion omnisend/includes/Internal/V1/class-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@

use Omnisend\SDK\V1\Contact;
use Omnisend\SDK\V1\CreateContactResponse;
use Omnisend\SDK\V1\Event;
use Omnisend\SDK\V1\SendCustomerEventResponse;
use WP_Error;

defined( 'ABSPATH' ) || die( 'no direct access' );

class Client implements \Omnisend\SDK\V1\Client {




private string $api_key;
private string $plugin_name;
private string $plugin_version;
Expand Down Expand Up @@ -47,7 +52,7 @@ public function create_contact( $contact ): CreateContactResponse {
}

$response = wp_remote_post(
OMNISEND_CORE_API_V3 . '/contacts',
OMNISEND_CORE_API_V5 . '/contacts',
array(
'body' => wp_json_encode( $contact->to_array() ),
'headers' => array(
Expand Down Expand Up @@ -89,6 +94,51 @@ public function create_contact( $contact ): CreateContactResponse {
return new CreateContactResponse( (string) $arr['contactID'], $error );
}

public function send_customer_event( $event ): SendCustomerEventResponse {
$error = new WP_Error();

if ( $event instanceof Event ) {
$error->merge_from( $event->validate() );
} else {
$error->add( 'event', 'Event is not instance of Omnisend\SDK\V1\Event.' );
}

$error->merge_from( $this->check_setup() );

if ( $error->has_errors() ) {
return new SendCustomerEventResponse( $error );
}

$response = wp_remote_post(
OMNISEND_CORE_API_V5 . '/events',
array(
'body' => wp_json_encode( $event->to_array() ),
'headers' => array(
'Content-Type' => 'application/json',
'X-API-Key' => $this->api_key,
'X-INTEGRATION-NAME' => $this->plugin_name,
'X-INTEGRATION-VERSION' => $this->plugin_version,
),
'timeout' => 10,
)
);

if ( is_wp_error( $response ) ) {
error_log('wp_remote_post error: ' . $response->get_error_message()); // phpcs:ignore
return new SendCustomerEventResponse( $response );
}

$http_code = wp_remote_retrieve_response_code( $response );
if ( $http_code >= 400 ) {
$body = wp_remote_retrieve_body( $response );
$err_msg = "HTTP error: {$http_code} - " . wp_remote_retrieve_response_message( $response ) . " - {$body}";
$error->add( 'omnisend_api', $err_msg );
return new SendCustomerEventResponse( $error );
greta-mik marked this conversation as resolved.
Show resolved Hide resolved
}

return new SendCustomerEventResponse( $error );
}

/**
* @return WP_Error
*/
Expand Down
11 changes: 9 additions & 2 deletions omnisend/includes/SDK/V1/class-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

namespace Omnisend\SDK\V1;

use WP_Error;

defined( 'ABSPATH' ) || die( 'no direct access' );

/**
Expand All @@ -25,4 +23,13 @@ interface Client {
* @return CreateContactResponse
*/
public function create_contact( $contact ): CreateContactResponse;

/**
* Send customer event to Omnisend. Customer events are used to track customer behavior and trigger automations based on that behavior.
*
* @param Event $event
*
* @return SendCustomerEventResponse
*/
public function send_customer_event( $event ): SendCustomerEventResponse;
}
Loading
Loading