PHP code example of printu / customerio
1. Go to this page and download the library: Download printu/customerio library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
printu / customerio example snippets
pplication code...
use Customerio\Client;
$client = new Client('YOUR_API_KEY', 'YOUR_SITE_ID');
/*
* To authenticate, provide your key as a Bearer token in a HTTP Authorization header.
* You can create and manage your API keys by visiting your App API Keys page directly or by clicking the Integrations
* link in the left-hand menu of your Customer.io account and choosing Customer.io API > Manage API Credentials > App API Keys.
*/
$client->setAppAPIKey('APP_KEY');
use Customerio\Client;
$client = new Client('YOUR_API_KEY', 'YOUR_SITE_ID', ['region' => 'eu']);
// Create customer
try {
$client->customers->add(
[
'id' => 1,
'email' => '[email protected] ',
'plan' => 'free',
'created_at' => time()
]
);
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
// Handle the error
}
// Get customer
try {
$client->customers->get(
[
'email' => '[email protected] ',
]
);
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
// Handle the error
}
// Update customer
try {
$client->customers->update(
[
'id' => 1,
'email' => '[email protected] ',
'plan' => 'premium'
]
);
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
// Handle the error
}
// Delete customer
try {
$client->customers->delete(
[
'id' => 1,
]
);
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
// Handle the error
}
// Add customer event
try {
$client->customers->event(
[
'id' => 1,
'name' => 'test-event',
'data' => [
'event-metadata-1' => 'test',
'event-metadata-2' => 'test-2'
]
]
);
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
// Handle the error
}
// Add anonymous event
try {
$client->events->anonymous(
[
'name' => 'invite-friend',
'data' => [
'recipient' => '[email protected] '
]
]
);
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
// Handle the error
}
// Get segment data
try {
$client->segments->get(
[
'id' => 1
]
);
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
// Handle the error
}
// Add page view
try {
$result = $client->page->view(
[
'id' => 1,
'url' => 'http://example.com/login',
'data' => [
'referrer' => 'http://example.com'
]
]
);
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
// Handle the error
}
// Get campaigns data
try {
$client->campaigns->get(
[
'id' => 1
]
);
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
// Handle the error
}
// Trigger broadcast campaign
try {
$result = $client->campaigns->trigger(
[
'id' => 1,
'data' => [
'headline' => 'Roadrunner spotted in Albuquerque!',
'date' => 'January 24, 2018',
'text' => 'We\'ve received reports of a roadrunner in your immediate area! Head to your dashboard to view more information!'
],
'recipients' => [
'segments' => [
'id' => 1
]
]
]
);
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
// Handle the error
}
// Single entity operation
try {
$client->track->entity([
'type' => 'person',
'action' => 'identify',
'identifiers' => [
'id' => '123' // or 'email' => '[email protected] ' or 'cio_id' => 'cio_123'
],
'attributes' => [
'name' => 'John Doe',
'plan' => 'premium',
'test_attribute' => null, // pass null to remove the attribute from the entity
'last_activity_at' => time()
]
]);
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
// Handle the error
}
// Create or update an object
try {
$client->track->entity([
'type' => 'object',
'action' => 'identify',
'identifiers' => [
'object_type_id' => 'product',
'object_id' => 'SKU-123'
],
'attributes' => [
'name' => 'Awesome Product',
'price' => 99.99,
'category' => 'Electronics'
]
]);
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
// Handle the error
}
// Delete an entity
try {
$client->track->entity([
'type' => 'person',
'action' => 'delete',
'identifiers' => [
'id' => '123'
]
]);
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
// Handle the error
}
// Add relationships to a person entity
try {
$client->track->entity([
'type' => 'person',
'action' => 'add_relationships',
'identifiers' => [
'id' => '123'
],
'cio_relationships' => [
'identifiers' => [
'object_type_id' => 'product',
'object_id' => 'SKU-123'
],
'relationship_attributes' => [
'role' => 'client',
'created_at' => '2024-01-01T10:12:00Z'
]
]
]);
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
// Handle the error
}
// Batch multiple operations
try {
$client->track->batch([
'batch' => [
[
'type' => 'person',
'action' => 'identify',
'identifiers' => ['id' => '123'],
'attributes' => ['name' => 'John Doe']
],
[
'type' => 'person',
'action' => 'event',
'identifiers' => ['id' => '123'],
'name' => 'purchased',
'timestamp' => time(),
'attributes' => ['product_id' => 'SKU-123']
],
[
'type' => 'object',
'action' => 'identify',
'identifiers' => [
'object_type_id' => 'product',
'object_id' => 'SKU-123'
],
'attributes' => ['in_stock' => true]
]
]
]);
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
// Handle the error
}