Quick-start

Installation

Refer to the installation page for installation steps.

Flagship Usage

Start SDK

Step 1: Start the SDK by calling the start function of the \Flagship\Flagship class, in the most appropriate location for your application.

require __DIR__ . '/vendor/autoload.php';

use Flagship\Flagship;

Flagship::start("<ENV_ID>", "<API_KEY>");

This starts the SDK in DECISION-API. More options are available in the SDK configuration.

👍

Good to know

Your apiKey and environmentId can be found in your Flagship account under Parameters > Environment & Security.

Create a visitor

Step 2: Create a visitor using the newVisitor method from the Flagship instance. The visitor instance allows you to set relevant data for Flagship to make a decision, including: Visitor ID, Visitor Context, GDPR Consent, and Authentication status.

For example, if you want to enable a specific feature for all your VIP visitors, you'll need to add this data as an attribute into the visitor context (key-value pair): isVIP: true. Based on your targeting criteria defined in your use-case (isVIP === true), Flagship will make the decision and show your feature to visitors that have isVIP in their context and for which isVIP is equal to true.

require __DIR__ . '/vendor/autoload.php';

use Flagship\Flagship;

Flagship::start("<ENV_ID>", "<API_KEY>");

$visitor = Flagship::newVisitor("<VISITOR_ID>", true)
        ->setContext(["isVip" => true])
        ->build();

Getting Flags

Steps 3, 4, 5, and 6 involve fetching flags from Flagship, retrieving your flag, reading your flag's value., and send hits.

First, fetch flags from the Flagship platform using the fetchFlags method. You can ensure that your flags are fetched and ready to be used by using an await statement, a then function, or an event listener.

Then, use the getFlag method of the visitor instance to retrieve a specific flag object based on the key provided. This object includes methods to retrieve the flag value, access flag metadata, expose the flag, verify the flag's existence, and get the flag status.

require __DIR__ . '/vendor/autoload.php';

use Flagship\Flagship;

// Step 1: start the SDK
Flagship::start("<ENV_ID>", "<API_KEY>");

//Step 2: Create a visitor
$visitor = Flagship::newVisitor("<VISITOR_ID>", true)
        ->setContext(["isVip" => true])
        ->build();

//Step 3: Fetch flag from the Flagship platform 
$visitor->fetchFlags();

//Step 4: Retrieves a flag named "displayVipFeature"
$flag = $visitor->getFlag("displayVipFeature");

//Step 5: Returns the flag value ,or if the flag does not exist, it returns the default value "false"
echo "flag value:". $flag->getValue(false);

//Step 6: Batch all the collected hits and send them
Flagship::close();

👍

Good to know

  • By default, the SDK assumes that the visitor has been exposed to the flag when flag.getValue() is called, so it sends the flag exposure hit to flagship server. However, this behavior can be changed. Click here for more details.
  • Whenever your app (script) is about to terminate or crash, you should call Flagship::close() to batch all the collected hits and send them.

Tracking hits

Step 7: Send hits to Flagship using the sendHit method of the visitor instance. These hits help validate your objectives (KPIs) set up in your campaign.

// ... other code

$visitor->sendHit(new Event(EventCategory::ACTION_TRACKING, "add-to-cart"));

//Step 6: Batch all the collected hits and send them
Flagship::close();