generated from Setono/SyliusPluginSkeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds offline conversion tracking (#4)
* WIP * WIP * WIP * Added readme and fixed phpunit * Made the algorithm changeable * Fixed static analysis issues * Only returned the last three days of conversions * Removed placeholder on category * Add event * Added plugin specific salt configuration value * Added index on createdAt and channel * Added upgrade file and updated changelog * Added benefit of no JS * Added csv header to conversion csv file
- Loading branch information
Showing
56 changed files
with
1,172 additions
and
567 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Changelog | ||
All notable changes to this project will be documented in this file. | ||
|
||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), | ||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). | ||
|
||
## 1.0.0 | ||
### Changed | ||
- Changed from client side to server side tracking. This change renders v0.1 completely different to v1.0. Do not | ||
upgrade to v1.0 if you don't want server side tracking. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Upgrade from v0.1 to v1.0 | ||
1. If you are not using the [tag bag bundle](https://github.com/Setono/TagBagBundle) anywhere else in your application | ||
you can safely remove it. | ||
|
||
2. The entity that was named `Conversion` in v0.1 has been renamed to `ConversionAction` in v1.0 while a new `Conversion` | ||
entity has been created. This means your Doctrine migration file should first rename old | ||
`setono_sylius_google_ads__conversion` table to `setono_sylius_google_ads__conversion_action`. | ||
|
||
3. Generate a random string and use it as a salt in the config: | ||
```yaml | ||
# config/packages/setono_sylius_google_ads.yaml | ||
setono_sylius_google_ads: | ||
salt: 'insert your random string here' | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusGoogleAdsPlugin\ConsentChecker; | ||
|
||
final class AlwaysGivenConsentChecker implements ConsentCheckerInterface | ||
{ | ||
public function hasConsent(): bool | ||
{ | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusGoogleAdsPlugin\ConsentChecker; | ||
|
||
interface ConsentCheckerInterface | ||
{ | ||
/** | ||
* Returns true if the user has consented to sending data to Google | ||
*/ | ||
public function hasConsent(): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusGoogleAdsPlugin\Controller\Action; | ||
|
||
use function Safe\fopen; | ||
use function Safe\fputcsv; | ||
use function Safe\sprintf; | ||
use Setono\SyliusGoogleAdsPlugin\KeyGenerator\KeyGeneratorInterface; | ||
use Setono\SyliusGoogleAdsPlugin\Model\ConversionInterface; | ||
use Setono\SyliusGoogleAdsPlugin\Repository\ConversionRepositoryInterface; | ||
use Sylius\Component\Channel\Context\ChannelContextInterface; | ||
use Sylius\Component\Channel\Model\ChannelInterface; | ||
use Symfony\Component\HttpFoundation\HeaderUtils; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpFoundation\StreamedResponse; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
|
||
final class DownloadConversionsAction | ||
{ | ||
private ChannelContextInterface $channelContext; | ||
|
||
private ConversionRepositoryInterface $conversionRepository; | ||
|
||
private KeyGeneratorInterface $keyGenerator; | ||
|
||
public function __construct( | ||
ChannelContextInterface $channelContext, | ||
ConversionRepositoryInterface $conversionRepository, | ||
KeyGeneratorInterface $keyGenerator | ||
) { | ||
$this->channelContext = $channelContext; | ||
$this->conversionRepository = $conversionRepository; | ||
$this->keyGenerator = $keyGenerator; | ||
} | ||
|
||
public function __invoke(Request $request, string $key): Response | ||
{ | ||
$channel = $this->channelContext->getChannel(); | ||
if (!$this->keyGenerator->check($channel, $key)) { | ||
throw new NotFoundHttpException('The page you are looking for does not exist'); // todo throw an unauthorized exception? | ||
} | ||
|
||
$qb = $this->conversionRepository->findByChannelQueryBuilder($channel); | ||
$manager = $qb->getEntityManager(); | ||
$iterableResult = $qb->getQuery()->iterate(); | ||
|
||
$response = new StreamedResponse(function () use ($manager, $iterableResult): void { | ||
$output = fopen('php://output', 'wb'); | ||
|
||
fputcsv($output, [sprintf('Parameters:TimeZone=%s', date_default_timezone_get())]); | ||
fputcsv($output, ['Google Click ID', 'Conversion Name', 'Conversion Time', 'Conversion Value', 'Conversion Currency']); | ||
|
||
foreach ($iterableResult as $row) { | ||
/** @var ConversionInterface $conversion */ | ||
$conversion = $row[0]; | ||
|
||
$createdAt = $conversion->getCreatedAt(); | ||
if (null === $createdAt) { | ||
throw new \LogicException(sprintf( | ||
'The created at timestamp on the conversion with id %s is null. This should not be possible.', | ||
$conversion->getId() | ||
)); | ||
} | ||
|
||
fputcsv($output, [ | ||
$conversion->getGoogleClickId(), $conversion->getName(), $createdAt->format('Y-m-d\TH:i:s'), | ||
self::formatValue((int) $conversion->getValue()), $conversion->getCurrencyCode(), | ||
]); | ||
|
||
$manager->detach($row[0]); | ||
|
||
flush(); | ||
} | ||
|
||
flush(); | ||
}, 200, [ | ||
'Content-Type' => 'text/csv', | ||
]); | ||
|
||
$disposition = HeaderUtils::makeDisposition( | ||
HeaderUtils::DISPOSITION_INLINE, | ||
self::generateFilename($channel) | ||
); | ||
|
||
$response->headers->set('Content-Disposition', $disposition); | ||
|
||
return $response; | ||
} | ||
|
||
private static function formatValue(int $value): float | ||
{ | ||
return round($value / 100, 2); | ||
} | ||
|
||
private static function generateFilename(ChannelInterface $channel): string | ||
{ | ||
return 'conversions---' . mb_strtolower((string) $channel->getCode()) . '.csv'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusGoogleAdsPlugin\Controller\Action; | ||
|
||
use Setono\SyliusGoogleAdsPlugin\Repository\ConversionActionRepositoryInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Twig\Environment; | ||
|
||
final class ShowHelpAction | ||
{ | ||
private Environment $twig; | ||
|
||
private ConversionActionRepositoryInterface $conversionRepository; | ||
|
||
public function __construct( | ||
Environment $twig, | ||
ConversionActionRepositoryInterface $conversionRepository | ||
) { | ||
$this->twig = $twig; | ||
$this->conversionRepository = $conversionRepository; | ||
} | ||
|
||
public function __invoke(Request $request): Response | ||
{ | ||
return new Response($this->twig->render('@SetonoSyliusGoogleAdsPlugin/admin/help.html.twig', [ | ||
'channels' => $this->conversionRepository->findChannels(), | ||
])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.