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
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ setono_meta_conversions_api:
- id: '%env(META_PIXEL_ID)%'
access_token: '%env(META_ACCESS_TOKEN)%'

# The PSR-18 http client used to send events. Defaults to Symfony's default http client, which means requests
# to Meta show up in the profiler and honour the options you configured. Point it at a scoped client to give
# Meta its own timeout
http_client: psr18.http_client

# Send events as test events, so they show up under 'Test events' in Meta's event manager instead of counting
# as real conversions
test_event_code:
Expand Down Expand Up @@ -246,6 +251,35 @@ If such an event is raised while handling an HTTP request, for instance a webhoo
request properties still describe *that* request, not the customer. Overwrite them in a listener above
`PRIORITY_POPULATE` when they matter.

### Giving Meta its own timeout

Because the client is a normal service, a scoped client works out of the box:

```yaml
framework:
http_client:
scoped_clients:
meta.client:
base_uri: 'https://graph.facebook.com'
timeout: 2
max_duration: 5

setono_meta_conversions_api:
http_client: meta.client
```

Note that a scoped client is a Symfony `HttpClientInterface`, so wrap it for PSR-18:

```yaml
services:
meta.psr18_client:
class: Symfony\Component\HttpClient\Psr18Client
arguments: ['@meta.client']

setono_meta_conversions_api:
http_client: meta.psr18_client
```

## Graph API version

Events are posted to the Graph API version of the installed `facebook/php-business-sdk` package (the SDK reads
Expand Down
5 changes: 5 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ public function getConfigTreeBuilder(): TreeBuilder
->end()
->end()
->end()
->scalarNode('http_client')
->info('The PSR-18 http client used to send events. Defaults to the application\'s psr18.http_client, i.e. the default Symfony http client. Point it at a scoped client to give Meta its own timeout')
->defaultValue('psr18.http_client')
->cannotBeEmpty()
->end()
->arrayNode('test_event_code')
->info('Send events as test events, see https://developers.facebook.com/docs/marketing-api/conversions-api/using-the-api#testEvents')
->addDefaultsIfNotSet()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function getConfiguration(array $config, ContainerBuilder $container): Co
public function load(array $configs, ContainerBuilder $container): void
{
/**
* @var array{consent: array{enabled: bool, category: string}, client_side: array{enabled: bool}, server_side: array{enabled: bool, message_bus: string}, pixels: array<array-key, array{id: string, access_token: string}>, test_event_code: array{query_parameter: bool, value: string|null}, filters: array{user_agent: list<string>}} $config
* @var array{consent: array{enabled: bool, category: string}, client_side: array{enabled: bool}, server_side: array{enabled: bool, message_bus: string}, pixels: array<array-key, array{id: string, access_token: string}>, http_client: string, test_event_code: array{query_parameter: bool, value: string|null}, filters: array{user_agent: list<string>}} $config
*/
$config = $this->processConfiguration($this->getConfiguration([], $container), $configs);
// The XML format is deprecated since Symfony 7.4 and removed in 8.0. Migrate to PHP config before adding Symfony 8 support
Expand All @@ -43,6 +43,10 @@ public function load(array $configs, ContainerBuilder $container): void
$container->setParameter('setono_meta_conversions_api.test_event_code.value', '' === $testEventCode ? null : $testEventCode);
$container->setParameter('setono_meta_conversions_api.test_event_code.query_parameter', $config['test_event_code']['query_parameter']);

// The reference to this alias is optional, so an application without symfony/http-client simply lets the
// SDK fall back to php-http/discovery
$container->setAlias('setono_meta_conversions_api.http_client', $config['http_client']);

$loader->load('services.xml');

if ($config['test_event_code']['query_parameter']) {
Expand Down
18 changes: 18 additions & 0 deletions src/Resources/config/services/client.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,28 @@
<service id="Setono\MetaConversionsApi\Client\ClientInterface"
alias="Setono\MetaConversionsApi\Client\Client"/>

<!--
Without these calls the SDK discovers a PSR-18 client and PSR-17 factories at runtime through
php-http/discovery. Wiring the application's own services instead means requests to Meta show up in the
profiler, honour the timeouts and options configured for the client, and can be replaced by a
MockHttpClient in tests. Each call is dropped when the service it needs is not available, in which case
the SDK falls back to discovery
-->
<service id="Setono\MetaConversionsApi\Client\Client">
<call method="setHttpClient">
<argument type="service" id="setono_meta_conversions_api.http_client" on-invalid="ignore"/>
</call>
<call method="setRequestFactory">
<argument type="service" id="Psr\Http\Message\RequestFactoryInterface" on-invalid="ignore"/>
</call>
<call method="setStreamFactory">
<argument type="service" id="Psr\Http\Message\StreamFactoryInterface" on-invalid="ignore"/>
</call>
<call method="setLogger">
<argument type="service" id="logger" on-invalid="ignore"/>
</call>

<tag name="monolog.logger" channel="setono_meta_conversions_api"/>
</service>
</services>
</container>
31 changes: 31 additions & 0 deletions tests/Double/RecordingHttpClientFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Setono\MetaConversionsApiBundle\Tests\Double;

use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;

/**
* Builds a MockHttpClient inside the container, since a container definition cannot hold a live object
*/
final class RecordingHttpClientFactory
{
/** @var list<array{string, string}> */
public static array $requests = [];

public static function reset(): void
{
self::$requests = [];
}

public static function create(): MockHttpClient
{
return new MockHttpClient(static function (string $method, string $url): MockResponse {
self::$requests[] = [$method, $url];

return new MockResponse('{"events_received":1}');
});
}
}
67 changes: 67 additions & 0 deletions tests/Integration/SetonoMetaConversionsApiBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,28 @@

namespace Setono\MetaConversionsApiBundle\Tests\Integration;

use FacebookAds\ApiConfig;
use Nyholm\BundleTest\TestKernel;
use Nyholm\Psr7\Factory\Psr17Factory;
use PHPUnit\Framework\Attributes\Test;
use Setono\BotDetectionBundle\SetonoBotDetectionBundle;
use Setono\ConsentBundle\SetonoConsentBundle;
use Setono\MetaConversionsApi\Client\ClientInterface;
use Setono\MetaConversionsApi\Event\Event;
use Setono\MetaConversionsApi\Pixel\Pixel;
use Setono\MetaConversionsApiBundle\ConsentChecker\ConsentCheckerInterface;
use Setono\MetaConversionsApiBundle\EventSubscriber\AddEventToTagBagSubscriber;
use Setono\MetaConversionsApiBundle\EventSubscriber\AddLibraryToTagBagSubscriber;
use Setono\MetaConversionsApiBundle\EventSubscriber\DispatchOnCommandBusSubscriber;
use Setono\MetaConversionsApiBundle\Message\Handler\SendEventHandler;
use Setono\MetaConversionsApiBundle\SetonoMetaConversionsApiBundle;
use Setono\MetaConversionsApiBundle\Tests\Double\RecordingHttpClientFactory;
use Setono\TagBagBundle\SetonoTagBagBundle;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Psr18Client;
use Symfony\Component\HttpKernel\KernelInterface;

final class SetonoMetaConversionsApiBundleTest extends KernelTestCase
Expand Down Expand Up @@ -218,6 +227,64 @@ public function it_dispatches_on_the_configured_message_bus(): void
);
}

#[Test]
public function it_sends_events_through_the_applications_http_client(): void
{
RecordingHttpClientFactory::reset();

self::bootKernel(['config' => function (TestKernel $kernel) {
$kernel->addTestConfig(static function (ContainerBuilder $container) {
$container->loadFromExtension('setono_meta_conversions_api', [
'client_side' => false,
]);

$container->register('test.psr17_factory', Psr17Factory::class);
$container->register('test.mock_http_client', MockHttpClient::class)
->setFactory([RecordingHttpClientFactory::class, 'create']);

// Replacing the application's PSR-18 client must be enough to intercept everything the bundle
// sends. That only holds because the client is wired instead of discovered at runtime
$container->register('psr18.http_client', Psr18Client::class)
->setArguments([
new Reference('test.mock_http_client'),
new Reference('test.psr17_factory'),
new Reference('test.psr17_factory'),
]);

$container->setAlias('test.conversions_api_client', ClientInterface::class)->setPublic(true);
});
}]);

$event = new Event(Event::EVENT_VIEW_CONTENT);
$event->pixels = [new Pixel('1234', 's3cr3t')];

$client = self::getContainer()->get('test.conversions_api_client');
self::assertInstanceOf(ClientInterface::class, $client);
$client->sendEvent($event);

// The Graph API version follows whichever facebook/php-business-sdk is installed
self::assertSame(
[['POST', sprintf('https://graph.facebook.com/v%s/1234/events', ApiConfig::APIVersion)]],
RecordingHttpClientFactory::$requests,
);
}

#[Test]
public function it_boots_when_the_configured_http_client_does_not_exist(): void
{
// Without symfony/http-client there is no psr18.http_client, and the SDK falls back to discovery
self::bootKernel(['config' => function (TestKernel $kernel) {
$kernel->addTestConfig(static function (ContainerBuilder $container) {
$container->loadFromExtension('setono_meta_conversions_api', [
'client_side' => false,
'http_client' => 'a.http.client.that.does.not.exist',
]);
});
}]);

self::assertTrue(self::getContainer()->has(SendEventHandler::class));
}

#[Test]
public function it_works_with_consent_bundle(): void
{
Expand Down
Loading