Skip to content

Commit

Permalink
Set the enhanced conversion upload delay as a parameter, so people ca…
Browse files Browse the repository at this point in the history
…n change it and it's only defined in one place
  • Loading branch information
loevgaard committed Sep 12, 2023
1 parent bb9a337 commit 1b58a76
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
16 changes: 13 additions & 3 deletions src/ConversionProcessor/ConversionProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,25 @@

use Google\Ads\GoogleAds\Util\V13\ResourceNames;
use Google\Ads\GoogleAds\V13\Services\ClickConversion;
use Setono\SyliusGoogleAdsPlugin\Factory\GoogleAdsClientFactoryInterface;
use Setono\SyliusGoogleAdsPlugin\Logger\ConversionLogger;
use Setono\SyliusGoogleAdsPlugin\Model\ConversionInterface;
use Setono\SyliusGoogleAdsPlugin\Repository\ConnectionMappingRepositoryInterface;
use Setono\SyliusGoogleAdsPlugin\Workflow\ConversionWorkflow;
use Symfony\Component\Workflow\WorkflowInterface;
use Webmozart\Assert\Assert;

final class ConversionProcessor extends AbstractConversionProcessor
{
public function __construct(
WorkflowInterface $workflow,
GoogleAdsClientFactoryInterface $googleAdsClientFactory,
ConnectionMappingRepositoryInterface $connectionMappingRepository,
private readonly string $enhancedConversionUploadDelay,
) {
parent::__construct($workflow, $googleAdsClientFactory, $connectionMappingRepository);
}

public function isEligible(ConversionInterface $conversion): bool
{
return $this->workflow->can($conversion, ConversionWorkflow::TRANSITION_UPLOAD_CONVERSION);
Expand Down Expand Up @@ -75,9 +87,7 @@ public function process(ConversionInterface $conversion): void
));
}

// According to Google we must upload the enhanced conversion within 24 hours
// See https://developers.google.com/google-ads/api/docs/conversions/enhance-conversions
$conversion->setNextProcessingAt((new \DateTimeImmutable())->add(new \DateInterval('PT23H30M')));
$conversion->setNextProcessingAt((new \DateTimeImmutable())->add(new \DateInterval($this->enhancedConversionUploadDelay)));

$this->workflow->apply($conversion, ConversionWorkflow::TRANSITION_UPLOAD_CONVERSION);
}
Expand Down
14 changes: 13 additions & 1 deletion src/ConversionProcessor/EnhancedConversionProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,33 @@
use Google\Ads\GoogleAds\V13\Enums\ConversionAdjustmentTypeEnum\ConversionAdjustmentType;
use Google\Ads\GoogleAds\V13\Enums\UserIdentifierSourceEnum\UserIdentifierSource;
use Google\Ads\GoogleAds\V13\Services\ConversionAdjustment;
use Setono\SyliusGoogleAdsPlugin\Factory\GoogleAdsClientFactoryInterface;
use Setono\SyliusGoogleAdsPlugin\Logger\ConversionLogger;
use Setono\SyliusGoogleAdsPlugin\Model\ConversionInterface;
use Setono\SyliusGoogleAdsPlugin\Repository\ConnectionMappingRepositoryInterface;
use Setono\SyliusGoogleAdsPlugin\Workflow\ConversionWorkflow;
use Symfony\Component\Workflow\WorkflowInterface;
use Webmozart\Assert\Assert;

final class EnhancedConversionProcessor extends AbstractConversionProcessor
{
public function __construct(
WorkflowInterface $workflow,
GoogleAdsClientFactoryInterface $googleAdsClientFactory,
ConnectionMappingRepositoryInterface $connectionMappingRepository,
private readonly string $enhancedConversionUploadDelay,
) {
parent::__construct($workflow, $googleAdsClientFactory, $connectionMappingRepository);
}

public function isEligible(ConversionInterface $conversion): bool
{
$stateUpdatedAt = $conversion->getStateUpdatedAt();
if (null === $stateUpdatedAt) {
return false;
}

$lastUpdatedThreshold = (new \DateTimeImmutable())->sub(new \DateInterval('PT23H30M'));
$lastUpdatedThreshold = (new \DateTimeImmutable())->sub(new \DateInterval($this->enhancedConversionUploadDelay));

return $stateUpdatedAt <= $lastUpdatedThreshold && $this->workflow->can($conversion, ConversionWorkflow::TRANSITION_UPLOAD_ENHANCED_CONVERSION);
}
Expand Down
4 changes: 4 additions & 0 deletions src/DependencyInjection/SetonoSyliusGoogleAdsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public function load(array $configs, ContainerBuilder $container): void
$config = $this->processConfiguration($this->getConfiguration([], $container), $configs);
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));

// According to Google we must upload the enhanced conversion within 24 hours
// See https://developers.google.com/google-ads/api/docs/conversions/enhance-conversions
$container->setParameter('setono_sylius_google_ads.enhanced_conversion_upload_delay', 'PT23H30M');

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

$this->registerResources(
Expand Down
2 changes: 2 additions & 0 deletions src/Resources/config/services/conversion_processor.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
<service id="setono_sylius_google_ads.conversion_processor.conversion"
class="Setono\SyliusGoogleAdsPlugin\ConversionProcessor\ConversionProcessor"
parent="setono_sylius_google_ads.conversion_processor.abstract">
<argument>%setono_sylius_google_ads.enhanced_conversion_upload_delay%</argument>

<tag name="setono_sylius_google_ads.conversion_processor" priority="90"/>
</service>

<service id="setono_sylius_google_ads.conversion_processor.enhanced_conversion"
class="Setono\SyliusGoogleAdsPlugin\ConversionProcessor\EnhancedConversionProcessor"
parent="setono_sylius_google_ads.conversion_processor.abstract">
<argument>%setono_sylius_google_ads.enhanced_conversion_upload_delay%</argument>

<tag name="setono_sylius_google_ads.conversion_processor" priority="80"/>
</service>
Expand Down

0 comments on commit 1b58a76

Please sign in to comment.