### get usage example Source: https://github.com/jeffersongoncalves/laravel-optimizely Example of fetching a project by its id. ```php $project = Optimizely::projects()->get(1234); ``` -------------------------------- ### Direct use of OptimizelyClient Source: https://github.com/jeffersongoncalves/laravel-optimizely Example of directly instantiating OptimizelyClient and calling get(). Note: unusual, normally go through Optimizely resources. ```php // Direct use (unusual — normally go through Optimizely resources) use JeffersonGoncalves\Optimizely\OptimizelyClient; $client = new OptimizelyClient('pat-abc123'); $projects = $client->get('/projects', ['page' => 1, 'per_page' => 25]); ``` -------------------------------- ### Install via Composer Source: https://github.com/jeffersongoncalves/laravel-optimizely/blob/main/README.md Install the package using Composer. ```bash composer require jeffersongoncalves/laravel-optimizely ``` -------------------------------- ### create usage example Source: https://github.com/jeffersongoncalves/laravel-optimizely Example of creating a project with a name and optional platform. ```php $created = Optimizely::projects()->create('Marketing Site', platform: 'web'); ``` -------------------------------- ### Example .env configuration Source: https://github.com/jeffersongoncalves/laravel-optimizely Example environment variables for the package. Set OPTIMIZELY_API_KEY to your personal access token and OPTIMIZELY_PROJECT_ID to your project id. ```env OPTIMIZELY_API_KEY=your-personal-access-token OPTIMIZELY_PROJECT_ID=1234 OPTIMIZELY_BASE_URL=https://api.optimizely.com/v2 OPTIMIZELY_DEFAULT_PER_PAGE=25 ``` -------------------------------- ### Quick start: list projects, get experiment results, handle exception Source: https://github.com/jeffersongoncalves/laravel-optimizely Use the Optimizely facade to list projects, fetch experiment results, and catch OptimizelyException with errorBody(). ```php use JeffersonGoncalves\Optimizely\Facades\Optimizely; $projects = Optimizely::projects()->list(page: 1, perPage: 50); $results = Optimizely::experiments()->results(555, '2026-01-01', '2026-01-31'); try { Optimizely::campaigns()->get(777); } catch (\JeffersonGoncalves\Optimizely\Exceptions\OptimizelyException $e) { logger()->error($e->getMessage(), $e->errorBody()); } ``` -------------------------------- ### list usage example Source: https://github.com/jeffersongoncalves/laravel-optimizely Example of calling the list method via the Optimizely facade with page and perPage parameters. ```php use JeffersonGoncalves\Optimizely\Facades\Optimizely; $projects = Optimizely::projects()->list(page: 1, perPage: 50); ``` -------------------------------- ### get usage example Source: https://github.com/jeffersongoncalves/laravel-optimizely Retrieves experiment with id 555. ```php $experiment = Optimizely::experiments()->get(555); ``` -------------------------------- ### Projects Usage Source: https://github.com/jeffersongoncalves/laravel-optimizely/blob/main/README.md List, get, create, and update projects using the Optimizely facade. ```php use JeffersonGoncalves\Optimizely\Facades\Optimizely; $projects = Optimizely::projects()->list(page: 1, perPage: 50); $project = Optimizely::projects()->get(1234); $created = Optimizely::projects()->create('Marketing Site', platform: 'web'); $updated = Optimizely::projects()->update(1234, ['name' => 'Marketing Site v2']); ``` -------------------------------- ### update usage example Source: https://github.com/jeffersongoncalves/laravel-optimizely Example of updating a project's name. ```php $updated = Optimizely::projects()->update(1234, ['name' => 'Marketing Site v2']); ``` -------------------------------- ### create error handling example Source: https://github.com/jeffersongoncalves/laravel-optimizely Shows how to handle InvalidArgumentException for blank names and OptimizelyException for HTTP errors. ```php try { Optimizely::projects()->create(' '); // blank name } catch (InvalidArgumentException $e) { // 'The "name" argument must not be empty.' } try { Optimizely::projects()->create('Site'); } catch (OptimizelyException $e) { logger()->error($e->getMessage(), $e->errorBody()); } ``` -------------------------------- ### Experiments Usage Source: https://github.com/jeffersongoncalves/laravel-optimizely/blob/main/README.md List, get, create, update, results, and archive experiments. Omits project id to fall back to config('optimizely.project_id'). ```php // Omits the project id — falls back to config('optimizely.project_id') $experiments = Optimizely::experiments()->list(status: 'running'); $experiment = Optimizely::experiments()->get(555); $created = Optimizely::experiments()->create( name: 'Homepage CTA', type: 'a/b', trafficAllocation: 5000, ); $updated = Optimizely::experiments()->update(555, ['status' => 'paused']); $results = Optimizely::experiments()->results(555, '2026-01-01', '2026-01-31'); $archived = Optimizely::experiments()->archive(555); ``` -------------------------------- ### list usage example Source: https://github.com/jeffersongoncalves/laravel-optimizely Uses the configured project id to list running experiments. ```php // Uses configured project id $running = Optimizely::experiments()->list(status: 'running'); ``` -------------------------------- ### get Source: https://github.com/jeffersongoncalves/laravel-optimizely Fetches a single project via GET /projects/{$id}. Throws OptimizelyException on non-2xx responses, including 404 when the project does not exist. ```APIDOC ## [GET] /projects/{id} ### Description Fetches a single project via `GET /projects/{$id}`. ### Method GET ### Endpoint /projects/{id} ### Parameters #### Path Parameters - **$id** (int) - Required - Optimizely project id ### Response #### Success Response (200) - **array** - The project object ### Request Example ```php $project = Optimizely::projects()->get(1234); ``` ``` -------------------------------- ### results usage example Source: https://github.com/jeffersongoncalves/laravel-optimizely Example call to fetch results for experiment id 555 with a date range. ```php $results = Optimizely::experiments()->results(555, '2026-01-01', '2026-01-31'); ``` -------------------------------- ### create usage example Source: https://github.com/jeffersongoncalves/laravel-optimizely Creates an experiment named 'Homepage CTA' with type 'a/b' and traffic allocation 5000. ```php $created = Optimizely::experiments()->create( name: 'Homepage CTA', type: 'a/b', trafficAllocation: 5000, ); ``` -------------------------------- ### create request body example Source: https://github.com/jeffersongoncalves/laravel-optimizely Shows the JSON body structure for the create request, with platform optional. ```json { "name": "...", "platform": "...?" } ``` -------------------------------- ### Get a single campaign with Optimizely::campaigns()->get() Source: https://github.com/jeffersongoncalves/laravel-optimizely Fetches a single campaign via GET /campaigns/{$id} (not project-scoped). Throws OptimizelyException on non-2xx responses, including 404. ```php /** @return array */ public function get(int $id): array ``` ```php $campaign = Optimizely::campaigns()->get(777); ``` -------------------------------- ### Usage examples for list() Source: https://github.com/jeffersongoncalves/laravel-optimizely Shows two ways to call list(): using the configured project ID or passing an explicit project ID as the first argument. The first call uses the default project ID from configuration. ```php // Uses the configured project id $events = Optimizely::events()->list(); // Or an explicit project id as first argument $events = Optimizely::events()->list(projectId: 4321); ``` -------------------------------- ### archive usage example Source: https://github.com/jeffersongoncalves/laravel-optimizely Example call to archive experiment id 555. ```php $archived = Optimizely::experiments()->archive(555); ``` -------------------------------- ### Using the Optimizely facade for projects Source: https://github.com/jeffersongoncalves/laravel-optimizely Shows how to use the facade to list and get projects. The list method accepts named arguments like page and perPage. ```php use JeffersonGoncalves\Optimizely\Facades\Optimizely; $projects = Optimizely::projects()->list(page: 1, perPage: 50); $project = Optimizely::projects()->get(1234); ``` -------------------------------- ### Install and publish config Source: https://github.com/jeffersongoncalves/laravel-optimizely Install the package via Composer and publish its configuration file using the optimizely-config tag. ```bash composer require jeffersongoncalves/laravel-optimizely php artisan vendor:publish --tag=optimizely-config ``` -------------------------------- ### get usage example Source: https://github.com/jeffersongoncalves/laravel-optimizely Fetches an audience with id 888 using the audiences resource. ```php $audience = Optimizely::audiences()->get(888); ``` -------------------------------- ### get Source: https://github.com/jeffersongoncalves/laravel-optimizely Fetches a single experiment via GET /experiments/{$id}. Returns the experiment object. ```APIDOC ## GET /experiments/{id} ### Description Fetches a single experiment via `GET /experiments/{$id}`. ### Method GET ### Endpoint /experiments/{id} ### Parameters #### Path Parameters - **id** (int) - Required - Experiment id ### Response #### Success Response (200) - **array** - The experiment object ### Request Example ```php $experiment = Optimizely::experiments()->get(555); ``` ``` -------------------------------- ### update usage example Source: https://github.com/jeffersongoncalves/laravel-optimizely Updates experiment 555 to set status to 'paused'. ```php $updated = Optimizely::experiments()->update(555, ['status' => 'paused']); ``` -------------------------------- ### list method signature Source: https://github.com/jeffersongoncalves/laravel-optimizely Lists projects via GET /projects using the base pagination() helper. $page defaults to 1, $perPage defaults to null (falls back to 25 unless configured). Throws OptimizelyException on non-2xx HTTP response. ```php /** @return array */ public function list(int $page = 1, ?int $perPage = null): array ``` -------------------------------- ### create request body JSON Source: https://github.com/jeffersongoncalves/laravel-optimizely Example request body for creating an experiment. Note the hard-coded status 'not_started'. ```json { "project_id": 1234, "name": "...", "type": "a/b", "status": "not_started", "traffic_allocation": 5000 } ``` -------------------------------- ### list usage examples Source: https://github.com/jeffersongoncalves/laravel-optimizely Shows how to call list() using the configured project id or an explicit one. The first call uses the default project id from config; the second passes projectId: 4321. ```php // Uses the configured project id $audiences = Optimizely::audiences()->list(); // Or an explicit project id $audiences = Optimizely::audiences()->list(projectId: 4321); ``` -------------------------------- ### Campaigns, Audiences, Events, and Pages Usage Source: https://github.com/jeffersongoncalves/laravel-optimizely/blob/main/README.md List and get campaigns, audiences, events, and pages. Scoped resources accept an explicit project id as the first argument. ```php $campaigns = Optimizely::campaigns()->list(); $campaign = Optimizely::campaigns()->get(777); $results = Optimizely::campaigns()->results(777); $audiences = Optimizely::audiences()->list(); $audience = Optimizely::audiences()->get(888); $events = Optimizely::events()->list(); $pages = Optimizely::pages()->list(); // Any scoped resource accepts an explicit project id as the first argument $events = Optimizely::events()->list(projectId: 4321); ``` -------------------------------- ### get method signature Source: https://github.com/jeffersongoncalves/laravel-optimizely Fetches a single audience via GET /audiences/{$id} (not project-scoped). Returns the audience object. Throws OptimizelyException on non-2xx, including 404. ```php /** @return array */ public function get(int $id): array ``` -------------------------------- ### get Source: https://github.com/jeffersongoncalves/laravel-optimizely Fetches a single campaign via GET /campaigns/{id} (not project-scoped). Returns the campaign object as a decoded JSON array. ```APIDOC ## [GET] /campaigns/{id} ### Description Fetches a single campaign via `GET /campaigns/{$id}` (not project-scoped). ### Method GET ### Endpoint /campaigns/{id} ### Parameters #### Path Parameters - **id** (int) - Required - Campaign id ### Response #### Success Response (200) - **array** - The campaign object ### Request Example ```php $campaign = Optimizely::campaigns()->get(777); ``` ``` -------------------------------- ### Error handling for get Source: https://github.com/jeffersongoncalves/laravel-optimizely Catches OptimizelyException and logs the error message and decoded error body. Demonstrates how to handle API failures when calling get(). ```php use JeffersonGoncalves\Optimizely\Exceptions\OptimizelyException; try { $audience = Optimizely::audiences()->get(888); } catch (OptimizelyException $e) { logger()->error($e->getMessage(), $e->errorBody()); } ``` -------------------------------- ### Fetch campaign results with Optimizely::campaigns()->results() Source: https://github.com/jeffersongoncalves/laravel-optimizely Fetches campaign results via GET /campaigns/{$id}/results (not project-scoped). Throws OptimizelyException on non-2xx responses. ```php /** @return array */ public function results(int $id): array ``` ```php $results = Optimizely::campaigns()->results(777); ``` -------------------------------- ### get Source: https://github.com/jeffersongoncalves/laravel-optimizely Fetches a single audience via GET /audiences/{$id} (not project-scoped). Returns the audience object as a decoded JSON response. ```APIDOC ## [GET] /audiences/{id} ### Description Fetches a single audience via `GET /audiences/{$id}` (not project-scoped). ### Method GET ### Endpoint /audiences/{id} ### Parameters #### Path Parameters - **id** (int) - Required - Audience id ### Response #### Success Response (200) - **array** - The audience object ### Request Example ```php $audience = Optimizely::audiences()->get(888); ``` ### Error Handling ```php use JeffersonGoncalves\Optimizely\Exceptions\OptimizelyException; try { $audience = Optimizely::audiences()->get(888); } catch (OptimizelyException $e) { logger()->error($e->getMessage(), $e->errorBody()); } ``` ``` -------------------------------- ### results method signature Source: https://github.com/jeffersongoncalves/laravel-optimizely Fetches experiment results via GET /experiments/{$id}/results with optional start_time / end_time query params (omitted when null). Returns array and throws OptimizelyException on non-2xx. ```php /** @return array */ public function results(int $id, ?string $startTime = null, ?string $endTime = null): array ``` -------------------------------- ### list Source: https://github.com/jeffersongoncalves/laravel-optimizely Lists pages via GET /pages with query params project_id, page and per_page. Returns a decoded JSON response containing a collection of page objects. ```APIDOC ## [GET] /pages ### Description Lists pages via `GET /pages` with query params `project_id`, `page` and `per_page`. Returns a decoded JSON response (a collection of page objects). ### Method GET ### Endpoint /pages ### Parameters #### Query Parameters - **project_id** (?int) - Optional - Project id; falls back to `optimizely.project_id` - **page** (int) - Optional - Page number (default: 1) - **per_page** (?int) - Optional - Items per page; falls back to default ### Response #### Success Response (200) - **array** - Decoded JSON response (a collection of page objects) ### Request Example ```php // Uses the configured project id $pages = Optimizely::pages()->list(); // Or an explicit project id as first argument $pages = Optimizely::pages()->list(projectId: 4321); ``` ### Error Handling Example ```php use JeffersonGoncalves\Optimizely\Exceptions\OptimizelyException; try { $pages = Optimizely::pages()->list(projectId: 4321, page: 1, perPage: 50); } catch (OptimizelyException $e) { logger()->error($e->getMessage(), $e->errorBody()); } ``` ``` -------------------------------- ### list method signature Source: https://github.com/jeffersongoncalves/laravel-optimizely Lists experiments via GET /experiments with optional status filter, pagination, and project id fallback. Throws InvalidArgumentException if no project id is available. ```php /** @return array */ public function list(?int $projectId = null, ?string $status = null, int $page = 1, ?int $perPage = null): array ``` -------------------------------- ### list Source: https://github.com/jeffersongoncalves/laravel-optimizely Lists projects via GET /projects using the base pagination() helper. This is a global operation that does not require a project id. ```APIDOC ## [GET] /projects ### Description Lists projects via `GET /projects` using the base `pagination()` helper. ### Method GET ### Endpoint /projects ### Parameters #### Query Parameters - **$page** (int) - Optional - Page number (default: `1`) - **$perPage** (?int) - Optional - Items per page; falls back to default per page (`25` unless configured) ### Response #### Success Response (200) - **array** - Decoded JSON response (a collection of project objects) ### Request Example ```php use JeffersonGoncalves\Optimizely\Facades\Optimizely; $projects = Optimizely::projects()->list(page: 1, perPage: 50); ``` ``` -------------------------------- ### list Source: https://github.com/jeffersongoncalves/laravel-optimizely Lists experiments via GET /experiments with optional status filter, pagination, and project id. Returns a collection of experiment objects. ```APIDOC ## GET /experiments ### Description Lists experiments via `GET /experiments` with query params `project_id`, optional `status`, `page` and `per_page`. ### Method GET ### Endpoint /experiments ### Parameters #### Query Parameters - **project_id** (int) - Optional - Project id; falls back to `optimizely.project_id` - **status** (string) - Optional - Experiment status filter (e.g. `'running'`); omitted when null - **page** (int) - Optional - Page number (default: 1) - **per_page** (int) - Optional - Items per page; falls back to default ### Response #### Success Response (200) - **array** - Decoded JSON response (a collection of experiment objects) ### Request Example ```php // Uses configured project id $running = Optimizely::experiments()->list(status: 'running'); ``` ``` -------------------------------- ### Catching InvalidArgumentException Source: https://github.com/jeffersongoncalves/laravel-optimizely Example of catching InvalidArgumentException for argument-validation failures that occur before any HTTP call. ```APIDOC ## [EXAMPLE] Catching InvalidArgumentException ### Description Example of catching InvalidArgumentException for argument-validation failures. ### Code ```php use JeffersonGoncalves\Optimizely\Facades\Optimizely; // Configured project id is missing AND no id passed: try { Optimizely::campaigns()->list(); } catch (InvalidArgumentException $e) { // 'A project id is required. Pass one or set "optimizely.project_id".' } ``` ``` -------------------------------- ### List campaigns with Optimizely::campaigns()->list() Source: https://github.com/jeffersongoncalves/laravel-optimizely Lists campaigns via GET /campaigns with optional project id, page, and per-page parameters. Throws InvalidArgumentException if no project id is available, and OptimizelyException on non-2xx responses. ```php /** @return array */ public function list(?int $projectId = null, int $page = 1, ?int $perPage = null): array ``` ```php $campaigns = Optimizely::campaigns()->list(); ``` -------------------------------- ### results Source: https://github.com/jeffersongoncalves/laravel-optimizely Fetches campaign results via GET /campaigns/{id}/results (not project-scoped). Returns the decoded results object as an array. ```APIDOC ## [GET] /campaigns/{id}/results ### Description Fetches campaign results via `GET /campaigns/{$id}/results` (not project-scoped). ### Method GET ### Endpoint /campaigns/{id}/results ### Parameters #### Path Parameters - **id** (int) - Required - Campaign id ### Response #### Success Response (200) - **array** - Decoded results object ### Request Example ```php $results = Optimizely::campaigns()->results(777); ``` ``` -------------------------------- ### list Source: https://github.com/jeffersongoncalves/laravel-optimizely Lists events via GET /events with query parameters project_id, page, and per_page. Returns a decoded JSON response containing a collection of event objects. ```APIDOC ## [GET] /events ### Description Lists events via `GET /events` with query params `project_id`, `page` and `per_page`. Returns a decoded JSON response (a collection of event objects). ### Method GET ### Endpoint /events ### Parameters #### Query Parameters - **project_id** (?int) - Optional - Project id; falls back to `optimizely.project_id` - **page** (int) - Optional - Page number (default: 1) - **per_page** (?int) - Optional - Items per page; falls back to default ### Response #### Success Response (200) - Returns `array` — decoded JSON response (a collection of event objects) ### Request Example ```php // Uses the configured project id $events = Optimizely::events()->list(); // Or an explicit project id as first argument $events = Optimizely::events()->list(projectId: 4321); ``` ### Error Handling ```php use JeffersonGoncalves\Optimizely\Exceptions\OptimizelyException; try { $events = Optimizely::events()->list(page: 2, perPage: 100); } catch (OptimizelyException $e) { logger()->error($e->getMessage(), $e->errorBody()); } ``` ``` -------------------------------- ### list Source: https://github.com/jeffersongoncalves/laravel-optimizely Lists campaigns via GET /campaigns with query parameters for project ID, pagination, and page size. Returns a collection of campaign objects as a decoded JSON array. ```APIDOC ## [GET] /campaigns ### Description Lists campaigns via `GET /campaigns` with query params `project_id`, `page` and `per_page`. ### Method GET ### Endpoint /campaigns ### Parameters #### Query Parameters - **project_id** (?int) - Optional - Project id; falls back to `optimizely.project_id` - **page** (int) - Optional - Page number (default: 1) - **per_page** (?int) - Optional - Items per page; falls back to default ### Response #### Success Response (200) - **array** - Decoded JSON response (a collection of campaign objects) ### Request Example ```php $campaigns = Optimizely::campaigns()->list(); ``` ``` -------------------------------- ### Catching OptimizelyException Source: https://github.com/jeffersongoncalves/laravel-optimizely Example of catching OptimizelyException in consumer code to handle API failures. The exception code provides the HTTP status and errorBody() provides the decoded JSON body. ```APIDOC ## [EXAMPLE] Catching OptimizelyException ### Description Example of catching OptimizelyException to handle API failures. ### Code ```php use JeffersonGoncalves\Optimizely\Facades\Optimizely; use JeffersonGoncalves\Optimizely\Exceptions\OptimizelyException; try { Optimizely::experiments()->get(1); } catch (OptimizelyException $e) { $status = $e->getCode(); // HTTP status, e.g. 404 $body = $e->errorBody(); // decoded JSON body as array logger()->error($e->getMessage(), ['status' => $status, 'body' => $body]); } ``` ``` -------------------------------- ### list Source: https://github.com/jeffersongoncalves/laravel-optimizely Lists audiences via GET /audiences with query parameters project_id, page, and per_page. Returns a collection of audience objects as a decoded JSON response. ```APIDOC ## [GET] /audiences ### Description Lists audiences via `GET /audiences` with query params `project_id`, `page` and `per_page`. ### Method GET ### Endpoint /audiences ### Parameters #### Query Parameters - **project_id** (?int) - Optional - Project id; falls back to `optimizely.project_id` - **page** (int) - Optional - Page number (default: 1) - **per_page** (?int) - Optional - Items per page; falls back to default ### Response #### Success Response (200) - **array** - Decoded JSON response (a collection of audience objects) ### Request Example ```php // Uses the configured project id $audiences = Optimizely::audiences()->list(); // Or an explicit project id $audiences = Optimizely::audiences()->list(projectId: 4321); ``` ``` -------------------------------- ### results Source: https://github.com/jeffersongoncalves/laravel-optimizely Fetches experiment results via GET /experiments/{id}/results with optional start_time and end_time query parameters. Returns the decoded results object, typically containing a metrics collection. ```APIDOC ## GET /experiments/{id}/results ### Description Fetches experiment results via `GET /experiments/{id}/results` with optional `start_time` / `end_time` query params (omitted when null). ### Method GET ### Endpoint `/experiments/{id}/results` ### Parameters #### Path Parameters - `id` (int) - Required - Experiment id #### Query Parameters - `start_time` (string) - Optional - Start of the results window - `end_time` (string) - Optional - End of the results window ### Response #### Success Response (200) - Returns an array of mixed values - decoded results object (e.g. a `metrics` collection) ### Request Example ```php $results = Optimizely::experiments()->results(555, '2026-01-01', '2026-01-31'); ``` ``` -------------------------------- ### Basic usage through the Optimizely facade Source: https://github.com/jeffersongoncalves/laravel-optimizely Shows how to call all six resource methods statically via the facade. Each method returns a resource object for further interaction. ```php use JeffersonGoncalves\Optimizely\Facades\Optimizely; $projects = Optimizely::projects(); $experiments = Optimizely::experiments(); $campaigns = Optimizely::campaigns(); $audiences = Optimizely::audiences(); $events = Optimizely::events(); $pages = Optimizely::pages(); ``` -------------------------------- ### configurePackage implementation Source: https://github.com/jeffersongoncalves/laravel-optimizely Configures the package with name 'optimizely' and calls hasConfigFile() to publish the config file. This is the actual implementation of the configurePackage method. ```php protected function configurePackage(Package $package): void { $package ->name('optimizely') ->hasConfigFile(); } ``` -------------------------------- ### pagination method implementation Source: https://github.com/jeffersongoncalves/laravel-optimizely Implementation of pagination: merges page, per_page (with default fallback), and extra params, then filters out null values. ```php // src/Resources/Resource.php lines 34-40 protected function pagination(int $page = 1, ?int $perPage = null, array $extra = []): array { return array_filter( array_merge(['page' => $page, 'per_page' => $perPage ?? $this->defaultPerPage], $extra), fn (mixed $value) => $value !== null, ); } ``` -------------------------------- ### Run tests with composer test Source: https://github.com/jeffersongoncalves/laravel-optimizely/blob/main/README.md Executes the package's test suite using Composer. Run this command from the project root to verify the codebase. ```bash composer test ``` -------------------------------- ### Publish config file with artisan vendor:publish Source: https://github.com/jeffersongoncalves/laravel-optimizely Run this command to publish the package configuration to config/optimizely.php. ```bash php artisan vendor:publish --tag=optimizely-config ``` -------------------------------- ### request method implementation Source: https://github.com/jeffersongoncalves/laravel-optimizely Builds a PendingRequest with token authentication, JSON accept/send, and base URL. ```php return Http::withToken($this->apiKey) ->acceptJson() ->asJson() ->baseUrl($this->baseUrl); ``` -------------------------------- ### Set Optimizely Credentials in .env Source: https://github.com/jeffersongoncalves/laravel-optimizely/blob/main/README.md Environment variables for API key and project ID. Generate the token under Profile > API Access in your Optimizely account. ```env OPTIMIZELY_API_KEY=your-personal-access-token OPTIMIZELY_PROJECT_ID=1234 ``` -------------------------------- ### create method signature Source: https://github.com/jeffersongoncalves/laravel-optimizely Creates an experiment via POST /experiments. Requires non-empty name and project id; throws InvalidArgumentException otherwise. ```php /** @return array */ public function create(string $name, ?int $projectId = null, string $type = 'a/b', ?int $trafficAllocation = null): array ``` -------------------------------- ### projects() Source: https://github.com/jeffersongoncalves/laravel-optimizely Provides static access to the Projects resource via the facade. Returns a Projects instance that can be used to list or retrieve projects. ```APIDOC ## projects() ### Description Returns a `\JeffersonGoncalves\Optimizely\Resources\Projects` instance, delegating to `Optimizely::projects()`. This allows static calls like `Optimizely::projects()->list(page: 1, perPage: 50)`. ### Method `public static function projects(): \JeffersonGoncalves\Optimizely\Resources\Projects` ### Return Type `\JeffersonGoncalves\Optimizely\Resources\Projects` ### Example ```php use JeffersonGoncalves\Optimizely\Facades\Optimizely; $projects = Optimizely::projects()->list(page: 1, perPage: 50); $project = Optimizely::projects()->get(1234); ``` ``` -------------------------------- ### Full published config/optimizely.php Source: https://github.com/jeffersongoncalves/laravel-optimizely The complete configuration file with environment variable mappings and defaults. The api_key is required for real API calls; project_id has no default and throws InvalidArgumentException when unset and needed. ```php env('OPTIMIZELY_API_KEY', ''), 'base_url' => env('OPTIMIZELY_BASE_URL', 'https://api.optimizely.com/v2'), 'project_id' => env('OPTIMIZELY_PROJECT_ID'), 'default_per_page' => env('OPTIMIZELY_DEFAULT_PER_PAGE', 25), ]; ``` -------------------------------- ### create method signature Source: https://github.com/jeffersongoncalves/laravel-optimizely Creates a project via POST /projects. $name must be non-empty after trim(), otherwise throws InvalidArgumentException with message 'The "name" argument must not be empty.'. $platform is omitted from the body when null. Throws OptimizelyException on non-2xx. ```php /** @return array */ public function create(string $name, ?string $platform = null): array ``` -------------------------------- ### Resource constructor signature Source: https://github.com/jeffersongoncalves/laravel-optimizely Defines the constructor for the abstract Resource base class, accepting an OptimizelyClient, optional project ID, and default per-page value. ```php public function __construct( protected OptimizelyClient $client, protected ?int $projectId = null, protected int $defaultPerPage = 25, ) ``` -------------------------------- ### create Source: https://github.com/jeffersongoncalves/laravel-optimizely Creates a project via POST /projects. Throws InvalidArgumentException if the name is blank after trimming, and OptimizelyException on non-2xx HTTP responses. ```APIDOC ## [POST] /projects ### Description Creates a project via `POST /projects`. ### Method POST ### Endpoint /projects ### Parameters #### Request Body - **$name** (string) - Required - Project name; must be non-empty after `trim()` - **$platform** (?string) - Optional - Platform identifier; omitted from the body when `null` ### Response #### Success Response (200) - **array** - The created project object ### Request Example ```php $created = Optimizely::projects()->create('Marketing Site', platform: 'web'); ``` ### Error Handling ```php try { Optimizely::projects()->create(' '); // blank name } catch (InvalidArgumentException $e) { // 'The "name" argument must not be empty.' } try { Optimizely::projects()->create('Site'); } catch (OptimizelyException $e) { logger()->error($e->getMessage(), $e->errorBody()); } ``` ``` -------------------------------- ### create Source: https://github.com/jeffersongoncalves/laravel-optimizely Creates an experiment via POST /experiments. Requires a project id and a non-empty name. ```APIDOC ## POST /experiments ### Description Creates an experiment via `POST /experiments`. ### Method POST ### Endpoint /experiments ### Parameters #### Request Body - **project_id** (int) - Required - Project id; falls back to `optimizely.project_id` - **name** (string) - Required - Experiment name; must be non-empty after `trim()` - **type** (string) - Optional - Experiment type (default: 'a/b') - **status** (string) - Optional - Hard-coded to 'not_started' on creation - **traffic_allocation** (int) - Optional - Traffic allocation; omitted when null ### Response #### Success Response (200) - **array** - The created experiment object ### Request Example ```json { "project_id": 1234, "name": "...", "type": "a/b", "status": "not_started", "traffic_allocation": 5000 } ``` ```php $created = Optimizely::experiments()->create( name: 'Homepage CTA', type: 'a/b', trafficAllocation: 5000, ); ``` ``` -------------------------------- ### Error handling with OptimizelyException Source: https://github.com/jeffersongoncalves/laravel-optimizely Shows how to catch OptimizelyException and access HTTP status code and decoded error body. ```php use JeffersonGoncalves\Optimizely\Exceptions\OptimizelyException; try { $client->get('/projects/99999'); } catch (OptimizelyException $e) { $httpStatus = $e->getCode(); // e.g. 404 $errorPayload = $e->errorBody(); // decoded JSON body } ``` -------------------------------- ### pagination method signature with docblock Source: https://github.com/jeffersongoncalves/laravel-optimizely Signature and docblock for the protected pagination method that builds query parameters for list endpoints. ```php /** * @param array $extra * @return array */ protected function pagination(int $page = 1, ?int $perPage = null, array $extra = []): array ``` -------------------------------- ### Basic usage of Optimizely::pages()->list() Source: https://github.com/jeffersongoncalves/laravel-optimizely Shows how to call the list method using the configured project ID or an explicit project ID as the first argument. The first call uses the default project ID from config; the second passes projectId 4321. ```php // Uses the configured project id $pages = Optimizely::pages()->list(); // Or an explicit project id as first argument $pages = Optimizely::pages()->list(projectId: 4321); ``` -------------------------------- ### configurePackage method signature Source: https://github.com/jeffersongoncalves/laravel-optimizely Declares the method signature for configurePackage. It configures the spatie package-tools package under the name 'optimizely' and declares a config file. ```php public function configurePackage(Package $package): void ``` -------------------------------- ### Error handling with OptimizelyException Source: https://github.com/jeffersongoncalves/laravel-optimizely Shows how to catch OptimizelyException when using the facade. The exception provides errorBody() for additional error details. ```php use JeffersonGoncalves\Optimizely\Exceptions\OptimizelyException; try { Optimizely::campaigns()->results(1); } catch (OptimizelyException $e) { logger()->error($e->getMessage(), $e->errorBody()); } ``` -------------------------------- ### Chaining a scoped call with default project id Source: https://github.com/jeffersongoncalves/laravel-optimizely Demonstrates chaining a scoped call on the experiments resource using the configured default project id. The `list` method accepts a `status` parameter to filter results. ```php $running = Optimizely::experiments()->list(status: 'running'); ``` -------------------------------- ### Error handling with OptimizelyException Source: https://github.com/jeffersongoncalves/laravel-optimizely Demonstrates catching OptimizelyException when listing pages with explicit parameters. Logs the error message and error body. Requires importing the exception class. ```php use JeffersonGoncalves\Optimizely\Exceptions\OptimizelyException; try { $pages = Optimizely::pages()->list(projectId: 4321, page: 1, perPage: 50); } catch (OptimizelyException $e) { logger()->error($e->getMessage(), $e->errorBody()); } ``` -------------------------------- ### pages() Source: https://github.com/jeffersongoncalves/laravel-optimizely Provides static access to the Pages resource via the facade. Returns a Pages instance that can be used to list or retrieve pages. ```APIDOC ## pages() ### Description Returns a `\JeffersonGoncalves\Optimizely\Resources\Pages` instance, delegating to `Optimizely::pages()`. This allows static calls to page-related methods. ### Method `public static function pages(): \JeffersonGoncalves\Optimizely\Resources\Pages` ### Return Type `\JeffersonGoncalves\Optimizely\Resources\Pages` ``` -------------------------------- ### experiments() Source: https://github.com/jeffersongoncalves/laravel-optimizely Provides static access to the Experiments resource via the facade. Returns an Experiments instance that can be used to list or retrieve experiments. ```APIDOC ## experiments() ### Description Returns a `\JeffersonGoncalves\Optimizely\Resources\Experiments` instance, delegating to `Optimizely::experiments()`. This allows static calls like `Optimizely::experiments()->list(status: 'running')`. ### Method `public static function experiments(): \JeffersonGoncalves\Optimizely\Resources\Experiments` ### Return Type `\JeffersonGoncalves\Optimizely\Resources\Experiments` ### Example ```php use JeffersonGoncalves\Optimizely\Facades\Optimizely; $running = Optimizely::experiments()->list(status: 'running'); ``` ``` -------------------------------- ### getFacadeAccessor method signature Source: https://github.com/jeffersongoncalves/laravel-optimizely Declares the protected static method that returns the container key the facade proxies. The return type is string. ```php protected static function getFacadeAccessor(): string ``` -------------------------------- ### Optimizely Constructor Source: https://github.com/jeffersongoncalves/laravel-optimizely Creates a new Optimizely client instance. This is the entry-point class for the Laravel Optimizely client, typically resolved by the container or facade. ```APIDOC ## [CONSTRUCTOR] Optimizely::__construct ### Description Creates a new Optimizely client instance. This is the entry-point class for the Laravel Optimizely client, typically resolved by the container or facade. ### Method Constructor ### Signature ```php public function __construct( string $apiKey, string $baseUrl = 'https://api.optimizely.com/v2', protected ?int $projectId = null, protected int $defaultPerPage = 25, ) ``` ### Parameters - **$apiKey** (string) - Required - Optimizely personal access token, sent as a Bearer token on every request - **$baseUrl** (string) - Optional - Root URL of the Optimizely REST API v2. Default: `'https://api.optimizely.com/v2'` - **$projectId** (?int) - Optional - Default project id applied to scoped resources when none is passed; if `null`, the caller must pass one. Default: `null` - **$defaultPerPage** (int) - Optional - Default `per_page` value used by list endpoints when none is passed. Default: `25` ### Request Example ```php // Manual construction outside the Laravel container use JeffersonGoncalves\Optimizely\Optimizely; $optimizely = new Optimizely( apiKey: 'pat-abc123', projectId: 4321, defaultPerPage: 50, ); ``` ``` -------------------------------- ### Singleton binding for Optimizely Source: https://github.com/jeffersongoncalves/laravel-optimizely Binds Optimizely as a singleton in the container. Resolves config values with coercion: api_key and base_url as strings, project_id as int or null, default_per_page as int with default 25. No validation is performed. ```php $this->app->singleton(Optimizely::class, function () { $projectId = config('optimizely.project_id'); return new Optimizely( (string) config('optimizely.api_key'), (string) config('optimizely.base_url', 'https://api.optimizely.com/v2'), $projectId !== null ? (int) $projectId : null, (int) config('optimizely.default_per_page', 25), ); }); ``` -------------------------------- ### Resolve Optimizely from container or facade Source: https://github.com/jeffersongoncalves/laravel-optimizely Shows two ways to use the Optimizely service: resolving the singleton from the container, or using the facade alias. The facade is registered in composer.json. ```php // Container resolves the singleton configured from config $optimizely = app(JeffersonGoncalves\Optimizely\Optimizely::class); // Or use the facade alias use JeffersonGoncalves\Optimizely\Facades\Optimizely; Optimizely::projects()->list(); ``` -------------------------------- ### projects() Source: https://github.com/jeffersongoncalves/laravel-optimizely Returns a Projects resource for managing Optimizely projects. This method provides access to project-related operations through the shared client. ```APIDOC ## projects() ### Description Returns a `Projects` resource for managing Optimizely projects. ### Method `projects()` ### Return Type `JeffersonGoncalves\Optimizely\Resources\Projects` ### Throws none ``` -------------------------------- ### campaigns() Source: https://github.com/jeffersongoncalves/laravel-optimizely Provides static access to the Campaigns resource via the facade. Returns a Campaigns instance that can be used to list or retrieve campaigns. ```APIDOC ## campaigns() ### Description Returns a `\JeffersonGoncalves\Optimizely\Resources\Campaigns` instance, delegating to `Optimizely::campaigns()`. This allows static calls like `Optimizely::campaigns()->results(1)`. ### Method `public static function campaigns(): \JeffersonGoncalves\Optimizely\Resources\Campaigns` ### Return Type `\JeffersonGoncalves\Optimizely\Resources\Campaigns` ### Example ```php use JeffersonGoncalves\Optimizely\Facades\Optimizely; use JeffersonGoncalves\Optimizely\Exceptions\OptimizelyException; try { Optimizely::campaigns()->results(1); } catch (OptimizelyException $e) { logger()->error($e->getMessage(), $e->errorBody()); } ``` ``` -------------------------------- ### Error handling with pagination Source: https://github.com/jeffersongoncalves/laravel-optimizely Combines pagination with the configured default project ID and catches OptimizelyException. Logs the error message and error body on API failure. ```php use JeffersonGoncalves\Optimizely\Exceptions\OptimizelyException; try { $events = Optimizely::events()->list(page: 2, perPage: 100); } catch (OptimizelyException $e) { logger()->error($e->getMessage(), $e->errorBody()); } ``` -------------------------------- ### Manual construction of Optimizely outside Laravel container Source: https://github.com/jeffersongoncalves/laravel-optimizely Shows how to instantiate the Optimizely class manually without the Laravel container, using named arguments for apiKey, projectId, and defaultPerPage. The baseUrl is not specified, so it uses the default. ```php // Manual construction outside the Laravel container use JeffersonGoncalves\Optimizely\Optimizely; $optimizely = new Optimizely( apiKey: 'pat-abc123', projectId: 4321, defaultPerPage: 50, ); ``` -------------------------------- ### events() Source: https://github.com/jeffersongoncalves/laravel-optimizely Provides static access to the Events resource via the facade. Returns an Events instance that can be used to list or retrieve events. ```APIDOC ## events() ### Description Returns a `\JeffersonGoncalves\Optimizely\Resources\Events` instance, delegating to `Optimizely::events()`. This allows static calls to event-related methods. ### Method `public static function events(): \JeffersonGoncalves\Optimizely\Resources\Events` ### Return Type `\JeffersonGoncalves\Optimizely\Resources\Events` ``` -------------------------------- ### send method signature Source: https://github.com/jeffersongoncalves/laravel-optimizely Protected method that dispatches any HTTP verb. Throws OptimizelyException on failed responses, otherwise returns the decoded JSON array. ```php /** @param array $data */ protected function send(string $method, string $path, array $data = []): array ``` -------------------------------- ### request method signature Source: https://github.com/jeffersongoncalves/laravel-optimizely Protected method that builds a configured PendingRequest. ```php protected function request(): PendingRequest ```