PHP code example of delighted / delighted

1. Go to this page and download the library: Download delighted/delighted 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/ */

    

delighted / delighted example snippets


// Add a new person, and schedule a survey immediately
$person1 = \Delighted\Person::create(['email' => '[email protected]']);

// Add a new person, and schedule a survey after 1 minute (60 seconds)
$person2 = \Delighted\Person::create(['email' => '[email protected]', 'delay' => 60]);

// Add a new person, but do not schedule a survey
$person3 = \Delighted\Person::create(['email' => '[email protected]', 'send' => false]);

// Add a new person with full set of attributes, including a custom question
// product name, and schedule a survey with a 30 second delay
$props = ['customer_id' => 123, 'country' => 'USA', 'question_product_name' => 'The London Trench'];
$person4 = \Delighted\Person::create([
                                        'email' => '[email protected]',
                                        'name' => 'Alexis Burke',
                                        'properties' => $props,
                                        'delay' => 30
                                    ]);

// Update an existing person (identified by email), adding a name, without
// scheduling a survey
$updated_person1 = \Delighted\Person::create([
                                                'email' => '[email protected]',
                                                'name' => 'Ellie Newman',
                                                'send' => false
                                            ]);

// Unsubscribe an existing person
\Delighted\Unsubscribe::create(['person_email' => '[email protected]'])

// List all people, auto pagination
// Note: Make sure to handle the possible rate limits error
$people = \Delighted\Person::list();
while (true) {
  try {
    foreach ($people->autoPagingIterator() as $person) {
      // Do something with $person
    }
    break;
  } catch (\Delighted\RateLimitedException $e) {
    // Indicates how long (in seconds) to wait before making this request again
    $e->getRetryAfter();
    continue;
  }
}

// For convenience, this method can use a sleep to automatically handle rate limits
$people = \Delighted\Person::list();
foreach ($people->autoPagingIterator(['auto_handle_rate_limits' => true]) as $person) {
  // Do something with $person
}


// List all unsubscribed people, 20 per page, first 2 pages
$unsubscribes = \Delighted\Unsubscribe::all()
$unsubscribes_p2 = \Delighted\Unsubscribe::all(['page' => 2]);

// List all bounced people, 20 per page, first 2 pages
$bounces = \Delighted\Bounce::all()
$bounces_p2 = \Delighted\Bounce::all(['page' => 2]);

// Delete by person id
\Delighted\Person::delete(array('id' => 42));
// Delete by email address
\Delighted\Person::delete(array('email' => '[email protected]'));
// Delete by phone number (must be E.164 format)
\Delighted\Person::delete(array('phone_number' => '+14155551212'));

// Delete all pending (scheduled but unsent) survey requests for a person,
// by email.
\Delighted\SurveyRequest::deletePending(['person_email' => '[email protected]']);

// Add a survey response, score only
$survey_response1 = \Delighted\SurveyResponse::create(['person' => $person1->id, 'score' => 10]);

// Add *another* survey response (for the same person), score and comment
$survey_response2 = \Delighted\SurveyResponse::create([
                                                         'person' => $person1->id,
                                                         'score' => 5,
                                                         'comment' => 'Really nice.'
                                                      ]);

// Retrieve an existing survey response
$survey_response3 = \Delighted\SurveyResponse::retrieve('123');

// Update a survey response score
$survey_response4 = \Delighted\SurveyResponse::retrieve('234');
$survey_response4->score = 10;
$survey_response4->save();

// Update (or add) survey response properties
$survey_response4->person_properties = ['segment' => 'Online'];
$survey_response4->save();

// Update person who recorded the survey response
$survey_response4->person = '321';
$survey_response4->save();

// List all survey responses, 20 per page, first 2 pages
$responses_p1 = \Delighted\SurveyResponse::all()
$responses_p2 = \Delighted\SurveyResponse::all(['page' => 2]);

// List all survey responses, 20 per page, expanding person object
$responses_p1_expand = \Delighted\SurveyResponse::all(['expand' => ['person']]);
// The person property is a \Delighted\Person object now
print $responses_p1_expand[0]->person->name;

// List all survey responses, 20 per page, for a specific trend (ID: 123)
$responses_p1_trend = \Delighted\SurveyResponse::all(['trend' => '123']);

// List all survey responses, 20 per page, in reverse chronological order
// (newest first)
$responses_p1_desc = \Delighted\SurveyResponse::all(['order' => 'desc']);

// List all survey responses, 100 per page, page 5, with a time range
$filtered_survey_responses = \Delighted\SurveyResponse::all([
                                                               'page' => 5,
                                                               'per_page' => 100, 
                                                               'since' => gmmktime(0, 0, 0, 10, 1, 2013),
                                                               'until' => gmmktime(0, 0, 0, 11, 1, 2013)
                                                            ]);

// Get current metrics, 30-day simple moving average, from most recent response
$metrics = \Delighted\Metrics::retrieve()

// Get current metrics, 30-day simple moving average, from most recent response
// for a specific trend (ID: 123)
$metrics = \Delighted\Metrics::retrieve(['trend' => '123']);

// Get metrics, for given range
$metrics = \Delighted\Metrics::retrieve([
                                           'since' => gmmktime(0, 0, 0, 10, 1, 2013), 
                                           'until' => gmmktime(0, 0, 0, 11, 1, 2013)
                                        ]);

// Get Autopilot configuration for the `email` platform
$autopilot = \Delighted\AutopilotConfiguration::retrieve('email');

// List people in AutopilotMembership for the `email` platform
$people_autopilot = \Delighted\AutopilotMembership\Email::list();
foreach ($people_autopilot->autoPagingIterator(['auto_handle_rate_limits' => true]) as $person_autopilot) {
  // Do something with $person_autopilot
}

// Add people to AutopilotMembership
$autopilot = \Delighted\AutopilotMembership\Email::create(['person_email' => '[email protected]']);

// Add people to AutopilotMembership, with a full set of attributes
$props = ['customer_id' => 123, 'country' => 'USA', 'question_product_name' => 'The London Trench'];
$autopilot = \Delighted\AutopilotMembership\Sms::create(['person_phone_number' => '+14155551212', 'properties' => $props]);

// Delete by person id
\Delighted\AutopilotMembership\Email::delete(['person_id' => 42]);

// Delete by email address
\Delighted\AutopilotMembership\Email::delete(['person_email' => '[email protected]']);

// Delete by phone number (must be E.164 format)
\Delighted\AutopilotMembership\Sms::delete(['person_phone_number' => '+14155551212']);

try {
    $metrics = \Delighted\Metrics::retrieve();
} catch (Delighted\RateLimitedException $e) {
    $errorCode = $e->getCode();

    if ($errorCode == 429) { // rate limited
        $retryAfterSeconds = e->getRetryAfter();
        // wait for $retryAfterSeconds before retrying
        // add your retry strategy here ...
    } else {
        // some other error
    }
}

$myUrl = 'http://localhost/delighted-mock/';
\Delighted\Client::getInstance(['baseUrl' => $myUrl]);

$mock_response = new \GuzzleHttp\Psr7\Response(200, [], ['nps' => 10]);
$mock_handler = new \GuzzleHttp\Handler\MockHandler([$mock_response]);
$handler_stack = \GuzzleHttp\HandlerStack::create($mock_handler);        
$client = \Delighted\TestClient::getInstance(['apiKey' => 'xyzzy', 'handler' => $handler_stack]);
$metrics = Delighted\Metrics::retrieve([], $client);

// This prints 10 -- the value comes from the mock response
print $metrics->nps;