Skip to content

Commit 9aa21bf

Browse files
committed
Add context to payload
1 parent 4287812 commit 9aa21bf

File tree

8 files changed

+30
-20
lines changed

8 files changed

+30
-20
lines changed

src/Client/Client.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function sendEvent(Event $event): void
4646
$httpClient = $this->getHttpClient();
4747
$requestFactory = $this->getRequestFactory();
4848

49-
$data = json_encode([$event], \JSON_THROW_ON_ERROR);
49+
$data = json_encode([$event->getPayload()], \JSON_THROW_ON_ERROR);
5050

5151
foreach ($event->pixels as $pixel) {
5252
$body = [

src/Event/Content.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function __construct(
2626
$this->deliveryCategory = $deliveryCategory;
2727
}
2828

29-
protected function getMapping(): array
29+
protected function getMapping(string $context): array
3030
{
3131
return [
3232
'id' => $this->id,

src/Event/Custom.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ final class Custom extends Parameters
4444
*/
4545
public array $customProperties = [];
4646

47-
protected function getMapping(): array
47+
protected function getMapping(string $context): array
4848
{
4949
return array_merge($this->customProperties, [
5050
'content_category' => $this->contentCategory,

src/Event/Event.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public static function getEvents(): array
163163
];
164164
}
165165

166-
protected function getMapping(): array
166+
protected function getMapping(string $context): array
167167
{
168168
return [
169169
'event_name' => $this->eventName,

src/Event/Parameters.php

+8-10
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,32 @@
66

77
use FacebookAds\Object\ServerSide\Normalizer;
88
use FacebookAds\Object\ServerSide\Util;
9-
use JsonSerializable;
109
use Webmozart\Assert\Assert;
1110

12-
abstract class Parameters implements JsonSerializable
11+
abstract class Parameters
1312
{
13+
public const PAYLOAD_CONTEXT_BROWSER = 'browser';
14+
15+
public const PAYLOAD_CONTEXT_SERVER = 'server';
16+
1417
/**
1518
* This method returns an array representation of the object ready
1619
* to be sent to Meta/Facebook, i.e. it's both normalized and hashed
1720
*/
18-
public function getPayload(): array
21+
public function getPayload(string $context = self::PAYLOAD_CONTEXT_SERVER): array
1922
{
20-
$payload = self::normalize($this->getMapping());
23+
$payload = self::normalize($this->getMapping($context));
2124
Assert::isArray($payload);
2225

2326
return $payload;
2427
}
2528

26-
public function jsonSerialize(): array
27-
{
28-
return $this->getPayload();
29-
}
30-
3129
/**
3230
* Returns the Meta/Facebook fields mapped to their respective values
3331
*
3432
* @return array<string, mixed>
3533
*/
36-
abstract protected function getMapping(): array;
34+
abstract protected function getMapping(string $context): array;
3735

3836
/**
3937
* Returns a list of Meta/Facebook field names that must be normalized by \FacebookAds\Object\ServerSide\Normalizer::normalize

src/Event/User.php

+13-2
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ final class User extends Parameters
5858

5959
public ?int $leadId = null;
6060

61-
protected function getMapping(): array
61+
protected function getMapping(string $context): array
6262
{
63-
return [
63+
$mapping = [
6464
'em' => $this->email,
6565
'ph' => $this->phoneNumber,
6666
'fn' => $this->firstName,
@@ -80,6 +80,17 @@ protected function getMapping(): array
8080
'fb_login_id' => $this->fbLoginId,
8181
'lead_id' => $this->leadId,
8282
];
83+
84+
if (self::PAYLOAD_CONTEXT_BROWSER === $context) {
85+
unset(
86+
$mapping['client_ip_address'],
87+
$mapping['client_user_agent'],
88+
$mapping['fbc'],
89+
$mapping['fbp'],
90+
);
91+
}
92+
93+
return $mapping;
8394
}
8495

8596
/**

src/Generator/FbqGenerator.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
namespace Setono\MetaConversionsApi\Generator;
66

77
use Setono\MetaConversionsApi\Event\Event;
8+
use Setono\MetaConversionsApi\Event\Parameters;
89

910
final class FbqGenerator implements FbqGeneratorInterface
1011
{
1112
public function generateInit(Event $event, bool $includeScriptTag = false): string
1213
{
13-
$json = json_encode($event->userData, \JSON_THROW_ON_ERROR);
14+
$json = json_encode($event->userData->getPayload(Parameters::PAYLOAD_CONTEXT_BROWSER), \JSON_THROW_ON_ERROR);
1415

1516
$str = '';
1617

@@ -31,7 +32,7 @@ public function generateTrack(Event $event, bool $includeScriptTag = false): str
3132
"fbq('%s', '%s', %s, {eventID: '%s'});",
3233
$event->isCustom() ? 'trackCustom' : 'track',
3334
$event->eventName,
34-
json_encode($event->customData, \JSON_THROW_ON_ERROR),
35+
json_encode($event->customData->getPayload(Parameters::PAYLOAD_CONTEXT_BROWSER), \JSON_THROW_ON_ERROR),
3536
$event->eventId
3637
);
3738

tests/Generator/FbqGeneratorTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ public function it_generates_init(): void
2525

2626
$generator = new FbqGenerator();
2727
self::assertSame(<<<EXPECTED
28-
fbq('init', '111', {"db":["cccd631dbe89ae6c982a960f248fabab8a4ae7f899853a3ea5bceef8ca1d6585"],"client_ip_address":"192.168.0.1","client_user_agent":"Chrome"});fbq('init', '222', {"db":["cccd631dbe89ae6c982a960f248fabab8a4ae7f899853a3ea5bceef8ca1d6585"],"client_ip_address":"192.168.0.1","client_user_agent":"Chrome"});
28+
fbq('init', '111', {"db":["cccd631dbe89ae6c982a960f248fabab8a4ae7f899853a3ea5bceef8ca1d6585"]});fbq('init', '222', {"db":["cccd631dbe89ae6c982a960f248fabab8a4ae7f899853a3ea5bceef8ca1d6585"]});
2929
EXPECTED
3030
, $generator->generateInit($event));
3131

3232
self::assertSame(<<<EXPECTED
33-
<script>fbq('init', '111', {"db":["cccd631dbe89ae6c982a960f248fabab8a4ae7f899853a3ea5bceef8ca1d6585"],"client_ip_address":"192.168.0.1","client_user_agent":"Chrome"});fbq('init', '222', {"db":["cccd631dbe89ae6c982a960f248fabab8a4ae7f899853a3ea5bceef8ca1d6585"],"client_ip_address":"192.168.0.1","client_user_agent":"Chrome"});</script>
33+
<script>fbq('init', '111', {"db":["cccd631dbe89ae6c982a960f248fabab8a4ae7f899853a3ea5bceef8ca1d6585"]});fbq('init', '222', {"db":["cccd631dbe89ae6c982a960f248fabab8a4ae7f899853a3ea5bceef8ca1d6585"]});</script>
3434
EXPECTED
3535
, $generator->generateInit($event, true));
3636
}

0 commit comments

Comments
 (0)