-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from Prometee/master
[Feature] add checkout configuration
- Loading branch information
Showing
12 changed files
with
359 additions
and
39 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
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
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
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 |
---|---|---|
@@ -1,44 +1,65 @@ | ||
(function ( $ ) { | ||
'use strict'; | ||
|
||
var CHECKOUT_STEP_SHIPPING = 3; | ||
var CHECKOUT_STEP_PAYMENT = 4; | ||
|
||
$.fn.extend({ | ||
enhancedEcommerceCheckoutShipping: function () { | ||
$(this).on('submit', function () { | ||
enhancedEcommerceTrackCheckoutOption(CHECKOUT_STEP_SHIPPING, $('input[type=radio]:checked', $(this)).val()); | ||
}); | ||
}, | ||
|
||
enhancedEcommerceCheckoutPayment: function () { | ||
$(this).on('submit', function () { | ||
enhancedEcommerceTrackCheckoutOption(CHECKOUT_STEP_PAYMENT, $('input[type=radio]:checked', $(this)).val()); | ||
}); | ||
'use strict'; | ||
(function ($) { | ||
|
||
if (typeof checkoutStepsConfiguration === "undefined") return; | ||
if (typeof checkoutStepsConfiguration !== "object") return; | ||
if (!checkoutStepsConfiguration.hasOwnProperty('enabled')) return; | ||
if (checkoutStepsConfiguration.enabled === false) return; | ||
if (!checkoutStepsConfiguration.hasOwnProperty('steps')) return; | ||
|
||
for (var stepId in checkoutStepsConfiguration.steps) { | ||
if (!checkoutStepsConfiguration.steps.hasOwnProperty(stepId)) continue; | ||
|
||
checkoutStepsConfiguration.steps[stepId].forEach(function BindStep(conf) { | ||
$(conf.selector).on(conf.event, function () { | ||
var option = null; | ||
|
||
if ( | ||
typeof conf.option !== "undefined" | ||
&& typeof window[conf.option] === 'function' | ||
) { | ||
option = window[conf.option].call(this); | ||
} | ||
|
||
enhancedEcommerceTrackCheckoutOption(stepId, option); | ||
}); | ||
}); | ||
} | ||
})(jQuery); | ||
|
||
$('form[name=sylius_checkout_select_shipping]').enhancedEcommerceCheckoutShipping(); | ||
$('form[name=sylius_checkout_select_payment]').enhancedEcommerceCheckoutPayment(); | ||
})( jQuery ); | ||
/** | ||
* This function will be called above if the configuration of | ||
* %sylius_gtm_enhanced_ecommerce.features.checkout.steps.*.*.option% | ||
* contains a function accessible into the window var. | ||
* | ||
* The 'this' context is the context of | ||
* %sylius_gtm_enhanced_ecommerce.features.checkout.steps.*.*.selector% | ||
* So if 'this' represent the current checkout form you can use it | ||
* to restrict what you want to be stored into GA | ||
* | ||
* @returns {string} | ||
*/ | ||
function enhancedEcommerceCheckoutGetChoiceValue() { | ||
return $('input[type=radio]:checked', this).val(); | ||
} | ||
|
||
/** | ||
* @param {integer} step | ||
* @param {integer} stepId | ||
* @param {string} checkoutOption | ||
*/ | ||
function enhancedEcommerceTrackCheckoutOption(step, checkoutOption) { | ||
var obj = { | ||
'event': 'checkoutOption', | ||
'ecommerce': { | ||
'checkout_option': { | ||
'actionField': { | ||
'step': step, | ||
'option': checkoutOption | ||
} | ||
} | ||
function enhancedEcommerceTrackCheckoutOption(stepId, checkoutOption) { | ||
var obj = { | ||
'event': 'checkoutOption', | ||
'ecommerce': { | ||
'checkout_option': { | ||
'actionField': { | ||
'step': stepId, | ||
'option': checkoutOption | ||
} | ||
}; | ||
} | ||
} | ||
}; | ||
|
||
/** global: dataLayer */ | ||
dataLayer.push(obj); | ||
/** global: dataLayer */ | ||
dataLayer.push(obj); | ||
} |
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,3 @@ | ||
<script type="text/javascript"> | ||
var checkoutStepsConfiguration = window.checkoutStepsConfiguration || (window.checkoutStepsConfiguration = {{ sylius_gtm_enhanced_ecommerce_parameter('checkout')|json_encode|raw }}); | ||
</script> |
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,67 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace StefanDoorn\SyliusGtmEnhancedEcommercePlugin\Twig; | ||
|
||
use Twig\Extension\AbstractExtension; | ||
use Twig\TwigFunction; | ||
|
||
final class ParameterExtension extends AbstractExtension | ||
{ | ||
/** @var array */ | ||
private $parameters; | ||
|
||
/** | ||
* @param bool $purchases | ||
* @param bool $product_impressions | ||
* @param bool $product_detail_impressions | ||
* @param bool $product_clicks | ||
* @param bool $cart | ||
* @param array $checkout | ||
*/ | ||
public function __construct( | ||
bool $purchases, | ||
bool $product_impressions, | ||
bool $product_detail_impressions, | ||
bool $product_clicks, | ||
bool $cart, | ||
array $checkout | ||
) | ||
{ | ||
$this->parameters = [ | ||
'purchases' => $purchases, | ||
'product_impressions' => $product_impressions, | ||
'product_detail_impressions' => $product_detail_impressions, | ||
'product_clicks' => $product_clicks, | ||
'cart' => $cart, | ||
'checkout' => $checkout, | ||
]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getFunctions() | ||
{ | ||
return array( | ||
new TwigFunction('sylius_gtm_enhanced_ecommerce_parameter', array($this, 'getParameter')), | ||
); | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* @return bool|array|null | ||
*/ | ||
public function getParameter(string $name) | ||
{ | ||
return $this->hasParameter($name) ? $this->parameters[$name] : null; | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* @return bool | ||
*/ | ||
public function hasParameter(string $name): bool | ||
{ | ||
return isset($this->parameters[$name]); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
tests/DependencyInjection/SyliusGtmEnhancedEcommerceExtensionTest.php
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,75 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Tests\StefanDoorn\SyliusGtmEnhancedEcommercePlugin\DependencyInjection; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use StefanDoorn\SyliusGtmEnhancedEcommercePlugin\DependencyInjection\SyliusGtmEnhancedEcommerceExtension; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; | ||
|
||
final class SyliusGtmEnhancedEcommerceExtensionTest extends TestCase | ||
{ | ||
public function testMinimalConfig(): void | ||
{ | ||
$container = $this->getContainer(); | ||
$extension = new SyliusGtmEnhancedEcommerceExtension(); | ||
|
||
$config = []; | ||
|
||
$extension->load(['sylius_gtm_enhanced_ecommerce' => $config], $container); | ||
|
||
$this->assertTrue( | ||
$container->getParameter('sylius_gtm_enhanced_ecommerce.features.purchases') | ||
); | ||
|
||
$this->assertTrue( | ||
$container->getParameter('sylius_gtm_enhanced_ecommerce.features.product_impressions') | ||
); | ||
|
||
$this->assertTrue( | ||
$container->getParameter('sylius_gtm_enhanced_ecommerce.features.product_detail_impressions') | ||
); | ||
|
||
$this->assertTrue( | ||
$container->getParameter('sylius_gtm_enhanced_ecommerce.features.product_clicks') | ||
); | ||
|
||
$this->assertTrue( | ||
$container->getParameter('sylius_gtm_enhanced_ecommerce.features.cart') | ||
); | ||
|
||
$conf = $container->getParameter('sylius_gtm_enhanced_ecommerce.features.checkout'); | ||
$this->assertTrue($conf['enabled']); | ||
|
||
$this->assertFalse( | ||
$container->hasParameter('sylius_gtm_enhanced_ecommerce.cache_resolver.product_detail_impressions') | ||
); | ||
} | ||
|
||
public function testWithCacheResolver(): void | ||
{ | ||
$container = $this->getContainer(); | ||
$extension = new SyliusGtmEnhancedEcommerceExtension(); | ||
|
||
$config = [ | ||
'cache_resolvers'=>true, | ||
]; | ||
$extension->load(['sylius_gtm_enhanced_ecommerce' => $config], $container); | ||
|
||
$this->assertEquals( | ||
3600, | ||
$container->getParameter('sylius_gtm_enhanced_ecommerce.cache_resolver.product_detail_impressions') | ||
); | ||
} | ||
|
||
private function getContainer(): ContainerBuilder | ||
{ | ||
return new ContainerBuilder(new ParameterBag([ | ||
'kernel.debug' => false, | ||
'kernel.bundles' => [], | ||
'kernel.cache_dir' => sys_get_temp_dir(), | ||
'kernel.environment' => 'test', | ||
'kernel.root_dir' => __DIR__.'/../../src/', | ||
])); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
<?php | ||
<?php declare(strict_types=1); | ||
|
||
namespace Tests\StefanDoorn\SyliusGtmEnhancedEcommercePlugin\TagManager; | ||
|
||
|
Oops, something went wrong.