Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
23 changes: 23 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,29 @@

All notable changes to `fopost/sdk` are documented here.

## Unreleased

### Changed

- **Breaking:** `$client->ads()->uploadConversions()` returns a `ConversionUploadResult`
(`id`, `accepted`, `eventCount`, `replayed`) instead of the accepted count, and takes an
optional `$idempotencyKey` sent as the `Idempotency-Key` header. Each event also accepts
`eventId`, `actionSource`, `sourceUrl`, `externalId`, `clientIpAddress`, `clientUserAgent`,
`clickId`, `browserId` and `contentIds`.
- `HttpClient::request()` takes a fifth `$headers` argument merged over the defaults.

### Added

- `$client->ads()->sendConversions()` posts to `/ads/conversions/events` with a required
`Idempotency-Key`; a replay answers `replayed: true`. `listConversionEvents()` reads the
stored batches as `ConversionBatch` rows whose events carry identifiers as hashes only.
- Pixels: `pixels()`, `createPixel()`, `pixel()`, `updatePixel()`, `sharePixel()`,
`pixelStats()` and `eventMatchQuality()`, with `AdPixel`, `AdPixelDetail`, `AdPixelStats`,
`AdPixelEventCount`, `EventMatchQuality` and `EventMatchKey`.
- Custom conversions: `customConversions()`, `createCustomConversion()`, `customConversion()`,
`updateCustomConversion()` and `deleteCustomConversion()`, with `CustomConversion` and
`CustomConversionRule`.

## 0.4.0 - 2026-10-18

### Added
Expand Down
7 changes: 6 additions & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ src/
LeadForm.php LeadFormSource.php Lead.php LeadsPage.php LeadFormDetail.php FeedLead.php LeadsFeedPage.php LeadPage.php
AdAccountTree.php AdCampaign.php AdSet.php NetworkAd.php AdCreative.php BulkAdStatusResult.php
AdInsightsReport.php InsightsMetrics.php InsightsRow.php ReachEstimate.php
AdPixel.php AdPixelDetail.php AdPixelStats.php AdPixelEventCount.php EventMatchQuality.php EventMatchKey.php
ConversionUploadResult.php ConversionBatch.php ConversionBatchEvent.php CustomConversion.php CustomConversionRule.php
PostValidation.php LengthValidation.php MediaValidation.php (+ per-platform rows, ValidationSignal)
Exception/
FopostException.php ErrorFactory.php + one subclass per status
Expand Down Expand Up @@ -119,7 +121,10 @@ resource is added. `scripts/sdk-gaps.mjs` in `fopost-api-collections` lists what
connections (+ Meta authorize), sources, boost, create, refresh, status, delete, audiences,
targeting search, lead forms and leads, plus the campaign tree (campaigns, ad sets, network ads,
duplicate, bulk status), creatives, audience detail and users, reach, insights, and the stored leads
feed with Page subscriptions. `boost()`, `create()`, `setStatus()`, `delete()`, `bulkSetStatus()` and
feed with Page subscriptions, plus pixels (`pixels`, `createPixel`, `pixel`, `updatePixel`, `sharePixel`,
`pixelStats`, `eventMatchQuality`), the Conversions API (`uploadConversions`, `sendConversions` with a required
`Idempotency-Key`, `listConversionEvents`) and custom conversions. `sendConversionEvents()` is the older
LinkedIn conversion-rule send, hence the different name. `boost()`, `create()`, `setStatus()`, `delete()`, `bulkSetStatus()` and
every campaign, ad set and network ad write also need the `publish` scope, and anything created
starts paused unless `paused` is `false`. Campaign-tree objects take Meta ids plus `connection_id`
and are read live. An empty update or duplicate body goes out as `{}` because the API rejects none. Request
Expand Down
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,44 @@ $client->ads()->sendConversionEvents($ruleId, $workspaceId, $connectionId, $even
$page = $client->ads()->adLibrary($workspaceId, $connectionId, keyword: 'crm', countries: ['US']);
```

### Pixels, the Conversions API and custom conversions

Every call needs the `ads` scope. Amounts are minor units; email, phone and externalId are
hashed before they leave FoPost and stored only as hashes.

```php
$pixels = $client->ads()->pixels($workspaceId, $connectionId, $adAccountId); // AdPixel[]
$pixel = $client->ads()->createPixel($workspaceId, $connectionId, $adAccountId, 'Site'); // ->code is the install script
$client->ads()->updatePixel($pixel->id, $workspaceId, $connectionId, $adAccountId, dataUseSetting: 'analytics_only');
$client->ads()->sharePixel($pixel->id, $workspaceId, $connectionId, $adAccountId, $otherAdAccountId);
$stats = $client->ads()->pixelStats($pixel->id, $workspaceId, $connectionId, $adAccountId); // last 28 days
$quality = $client->ads()->eventMatchQuality($pixel->id, $workspaceId, $connectionId, $adAccountId); // score out of 10

$events = [[
'eventName' => 'Purchase',
'occurredAt' => '2026-10-18T10:04:00Z',
'eventId' => 'order_77', // dedupes a browser and server pair
'actionSource' => 'website',
'email' => '[email protected]',
'valueMinor' => 4999,
'currency' => 'USD',
]];
// Idempotency-Key required: a repeat with the same key sends nothing and answers replayed = true.
$result = $client->ads()->sendConversions($workspaceId, $connectionId, $adAccountId, $pixel->id, $events, 'order_77');
$result->accepted; $result->replayed;
// The older upload keeps working; the key is optional there.
$client->ads()->uploadConversions($workspaceId, $connectionId, $adAccountId, $pixel->id, $events);
$batches = $client->ads()->listConversionEvents($workspaceId, $connectionId, limit: 20); // ConversionBatch[]

$conversion = $client->ads()->createCustomConversion(
$workspaceId, $connectionId, $adAccountId, $pixel->id, 'Checkout', 'PURCHASE',
['urlContains' => '/thank-you'], defaultValueMinor: 2500,
);
$client->ads()->customConversions($workspaceId, $connectionId, $adAccountId);
$client->ads()->updateCustomConversion($conversion->id, $workspaceId, $connectionId, $adAccountId, name: 'Checkout 2');
$client->ads()->deleteCustomConversion($conversion->id, $workspaceId, $connectionId, $adAccountId);
```

### Audiences, reach and insights

```php
Expand Down
12 changes: 9 additions & 3 deletions src/Http/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,19 @@ public function url(string $path, ?array $params = null): string
* Send a request, retrying on 429, and return the decoded body.
*
* @param array<string, mixed>|null $params
* @param array<string, string> $headers extra request headers, such as Idempotency-Key
*/
public function request(string $method, string $path, mixed $json = null, ?array $params = null): mixed
{
public function request(
string $method,
string $path,
mixed $json = null,
?array $params = null,
array $headers = [],
): mixed {
$url = $this->url($path, $params);
$payload = $json === null ? null : self::encode($json);

return $this->sendWithRetry($method, $url, $this->headers(), $payload);
return $this->sendWithRetry($method, $url, $headers + $this->headers(), $payload);
}

/**
Expand Down
24 changes: 24 additions & 0 deletions src/Model/AdPixel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Fopost\Sdk\Model;

/** A pixel as the list answers it. */
final class AdPixel extends Model
{
private function __construct(
array $raw,
public readonly string $id,
public readonly string $name,
) {
parent::__construct($raw);
}

public static function fromArray(mixed $data): static
{
$data = is_array($data) ? $data : [];

return new self($data, self::requiredStr($data, 'id'), self::requiredStr($data, 'name'));
}
}
38 changes: 38 additions & 0 deletions src/Model/AdPixelDetail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Fopost\Sdk\Model;

/** A pixel in full; `code` is the script to place on the site. */
final class AdPixelDetail extends Model
{
private function __construct(
array $raw,
public readonly string $id,
public readonly string $name,
public readonly ?string $code,
public readonly ?string $createdAt,
public readonly ?string $lastFiredAt,
public readonly bool $isUnavailable,
public readonly ?string $dataUseSetting,
) {
parent::__construct($raw);
}

public static function fromArray(mixed $data): static
{
$data = is_array($data) ? $data : [];

return new self(
$data,
self::requiredStr($data, 'id'),
self::requiredStr($data, 'name'),
self::str($data, 'code'),
self::str($data, 'created_at'),
self::str($data, 'last_fired_at'),
self::bool($data, 'is_unavailable') ?? false,
self::str($data, 'data_use_setting'),
);
}
}
24 changes: 24 additions & 0 deletions src/Model/AdPixelEventCount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Fopost\Sdk\Model;

/** How often one event fired on a pixel. */
final class AdPixelEventCount extends Model
{
private function __construct(
array $raw,
public readonly string $eventName,
public readonly int $count,
) {
parent::__construct($raw);
}

public static function fromArray(mixed $data): static
{
$data = is_array($data) ? $data : [];

return new self($data, self::requiredStr($data, 'event_name'), self::int($data, 'count') ?? 0);
}
}
31 changes: 31 additions & 0 deletions src/Model/AdPixelStats.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Fopost\Sdk\Model;

/** Event counts on a pixel over the last 28 days. */
final class AdPixelStats extends Model
{
/** @param array<int, AdPixelEventCount> $events */
private function __construct(
array $raw,
public readonly string $since,
public readonly string $until,
public readonly array $events,
) {
parent::__construct($raw);
}

public static function fromArray(mixed $data): static
{
$data = is_array($data) ? $data : [];

return new self(
$data,
self::requiredStr($data, 'since'),
self::requiredStr($data, 'until'),
AdPixelEventCount::listFrom(self::seq($data, 'events')),
);
}
}
45 changes: 45 additions & 0 deletions src/Model/ConversionBatch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Fopost\Sdk\Model;

/** A stored conversions batch as listConversionEvents() returns it. */
final class ConversionBatch extends Model
{
/** @param array<int, ConversionBatchEvent> $events */
private function __construct(
array $raw,
public readonly string $id,
public readonly string $workspaceId,
public readonly string $connectionId,
public readonly string $adAccountId,
public readonly string $pixelId,
public readonly ?string $idempotencyKey,
public readonly int $eventCount,
public readonly int $accepted,
public readonly array $events,
public readonly string $createdAt,
) {
parent::__construct($raw);
}

public static function fromArray(mixed $data): static
{
$data = is_array($data) ? $data : [];

return new self(
$data,
self::requiredStr($data, 'id'),
self::requiredStr($data, 'workspace_id'),
self::requiredStr($data, 'connection_id'),
self::requiredStr($data, 'ad_account_id'),
self::requiredStr($data, 'pixel_id'),
self::str($data, 'idempotency_key'),
self::int($data, 'event_count') ?? 0,
self::int($data, 'accepted') ?? 0,
ConversionBatchEvent::listFrom(self::seq($data, 'events')),
self::requiredStr($data, 'created_at'),
);
}
}
44 changes: 44 additions & 0 deletions src/Model/ConversionBatchEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Fopost\Sdk\Model;

/** One stored event of a batch; identifiers are kept only as hashes. */
final class ConversionBatchEvent extends Model
{
private function __construct(
array $raw,
public readonly string $eventName,
public readonly string $occurredAt,
public readonly ?string $eventId,
public readonly string $actionSource,
public readonly ?string $emailHash,
public readonly ?string $phoneHash,
public readonly ?string $externalIdHash,
public readonly ?int $valueMinor,
public readonly ?string $currency,
public readonly ?string $orderId,
) {
parent::__construct($raw);
}

public static function fromArray(mixed $data): static
{
$data = is_array($data) ? $data : [];

return new self(
$data,
self::requiredStr($data, 'event_name'),
self::requiredStr($data, 'occurred_at'),
self::str($data, 'event_id'),
self::str($data, 'action_source') ?? 'website',
self::str($data, 'email_hash'),
self::str($data, 'phone_hash'),
self::str($data, 'external_id_hash'),
self::int($data, 'value_minor'),
self::str($data, 'currency'),
self::str($data, 'order_id'),
);
}
}
32 changes: 32 additions & 0 deletions src/Model/ConversionUploadResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Fopost\Sdk\Model;

/** What came of a conversions batch; `replayed` means the Idempotency-Key had been used before. */
final class ConversionUploadResult extends Model
{
private function __construct(
array $raw,
public readonly string $id,
public readonly int $accepted,
public readonly int $eventCount,
public readonly bool $replayed,
) {
parent::__construct($raw);
}

public static function fromArray(mixed $data): static
{
$data = is_array($data) ? $data : [];

return new self(
$data,
self::requiredStr($data, 'id'),
self::int($data, 'accepted') ?? 0,
self::int($data, 'event_count') ?? 0,
self::bool($data, 'replayed') ?? false,
);
}
}
Loading
Loading