From 13105c0de57d2be222448effffee50a7aa1f6799 Mon Sep 17 00:00:00 2001 From: Francis Hilaire Date: Fri, 4 Jan 2019 20:12:51 +0100 Subject: [PATCH 1/8] Add steps to checkout feature --- src/DependencyInjection/Configuration.php | 44 ++++++++++++++++++- .../SyliusGtmEnhancedEcommerceExtension.php | 2 +- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 398b982..f6ce02b 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -26,7 +26,49 @@ public function getConfigTreeBuilder() ->booleanNode('product_detail_impressions')->defaultTrue()->end() ->booleanNode('product_clicks')->defaultTrue()->end() ->booleanNode('cart')->defaultTrue()->end() - ->booleanNode('checkout')->defaultTrue()->end() + ->arrayNode('checkout') + ->canBeDisabled() + ->addDefaultsIfNotSet() + ->children() + ->arrayNode('steps') + ->defaultValue([ + 1 => [ + [ + 'event' => 'click', + 'selector' => 'a[href$=/checkout/]' + ] + ], + 2 => [ + [ + 'event' => 'submit', + 'selector' => 'form[name=sylius_checkout_address]' + ] + ], + 3 => [ + [ + 'event' => 'submit', + 'selector' => 'form[name=sylius_checkout_select_shipping]' + ] + ], + 4 => [ + [ + 'event' => 'submit', + 'selector' => 'form[name=sylius_checkout_select_payment]' + ] + ], + ]) + ->arrayPrototype() + ->arrayPrototype() + ->addDefaultsIfNotSet() + ->children() + ->scalarNode('event')->defaultValue('submit')->end() + ->scalarNode('selector')->isRequired()->end() + ->end() + ->end() + ->end() + ->end() + ->end() + ->end() ->end() ->end() diff --git a/src/DependencyInjection/SyliusGtmEnhancedEcommerceExtension.php b/src/DependencyInjection/SyliusGtmEnhancedEcommerceExtension.php index 5a1224a..5f5b6c3 100644 --- a/src/DependencyInjection/SyliusGtmEnhancedEcommerceExtension.php +++ b/src/DependencyInjection/SyliusGtmEnhancedEcommerceExtension.php @@ -25,7 +25,7 @@ public function load(array $configs, ContainerBuilder $container) $container->setParameter($parameter, $setting); - if ($setting === true) { + if ($setting === true || (is_array($setting) && $setting['enabled'] === true)) { $loader->load(sprintf('features/%s.yml', $feature)); } } From be5dc6be60521304ccf8b7792857c4d20517ac8b Mon Sep 17 00:00:00 2001 From: Francis Hilaire Date: Sat, 5 Jan 2019 18:29:02 +0100 Subject: [PATCH 2/8] Allow to use the new configuration --- src/DependencyInjection/Configuration.php | 11 ++- src/Resources/config/features/checkout.yml | 7 ++ src/Resources/config/services.yml | 12 +++ .../public/gtm.enhancedEcommerce.checkout.js | 78 +++++++++++-------- src/Resources/views/Checkout/head.html.twig | 3 + src/Twig/ParameterExtension.php | 67 ++++++++++++++++ 6 files changed, 141 insertions(+), 37 deletions(-) create mode 100644 src/Resources/views/Checkout/head.html.twig create mode 100644 src/Twig/ParameterExtension.php diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index f6ce02b..0908125 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -35,25 +35,27 @@ public function getConfigTreeBuilder() 1 => [ [ 'event' => 'click', - 'selector' => 'a[href$=/checkout/]' + 'selector' => 'a[href$=\'/checkout/\']', ] ], 2 => [ [ 'event' => 'submit', - 'selector' => 'form[name=sylius_checkout_address]' + 'selector' => 'form[name=sylius_checkout_address]', ] ], 3 => [ [ 'event' => 'submit', - 'selector' => 'form[name=sylius_checkout_select_shipping]' + 'selector' => 'form[name=sylius_checkout_select_shipping]', + 'option' => 'getCheckoutChoiceVal', ] ], 4 => [ [ 'event' => 'submit', - 'selector' => 'form[name=sylius_checkout_select_payment]' + 'selector' => 'form[name=sylius_checkout_select_payment]', + 'option' => 'getCheckoutChoiceVal', ] ], ]) @@ -63,6 +65,7 @@ public function getConfigTreeBuilder() ->children() ->scalarNode('event')->defaultValue('submit')->end() ->scalarNode('selector')->isRequired()->end() + ->scalarNode('option')->end() ->end() ->end() ->end() diff --git a/src/Resources/config/features/checkout.yml b/src/Resources/config/features/checkout.yml index f71e0e1..aea06bb 100644 --- a/src/Resources/config/features/checkout.yml +++ b/src/Resources/config/features/checkout.yml @@ -12,6 +12,13 @@ services: - "@sylius.google_tag_manager.enhanced_ecommerce_tracking.resolver.checkout_step" tags: - { name: kernel.event_listener, event: kernel.controller, method: onKernelController } + + sylius.google_tag_manager.enhanced_ecommerce_tracking.checkout.listener.sylius.shop.layout.head: + class: Sylius\Bundle\UiBundle\Block\BlockEventListener + arguments: + - '@@SyliusGtmEnhancedEcommercePlugin/Checkout/head.html.twig' + tags: + - { name: kernel.event_listener, event: sonata.block.event.sylius.shop.layout.head, method: onBlockEvent } sylius.google_tag_manager_enhanced_ecommerce.checkout.block_event_listener.layout.javascripts: class: Sylius\Bundle\UiBundle\Block\BlockEventListener diff --git a/src/Resources/config/services.yml b/src/Resources/config/services.yml index 74d6b21..e266526 100644 --- a/src/Resources/config/services.yml +++ b/src/Resources/config/services.yml @@ -45,3 +45,15 @@ services: sylius.google_tag_manager.enhanced_ecommerce_tracking.object.factory.product_detail_impression: class: StefanDoorn\SyliusGtmEnhancedEcommercePlugin\Object\Factory\ProductDetailImpressionFactory + + sylius.google_tag_manager.enhanced_ecommerce_tracking.twig.extension.parameters: + class: StefanDoorn\SyliusGtmEnhancedEcommercePlugin\Twig\ParameterExtension + arguments: + - "%sylius_gtm_enhanced_ecommerce.features.purchases%" + - "%sylius_gtm_enhanced_ecommerce.features.product_impressions%" + - "%sylius_gtm_enhanced_ecommerce.features.product_detail_impressions%" + - "%sylius_gtm_enhanced_ecommerce.features.product_clicks%" + - "%sylius_gtm_enhanced_ecommerce.features.cart%" + - "%sylius_gtm_enhanced_ecommerce.features.checkout%" + tags: + - { name: "twig.extension" } diff --git a/src/Resources/public/gtm.enhancedEcommerce.checkout.js b/src/Resources/public/gtm.enhancedEcommerce.checkout.js index c92523a..2a73835 100644 --- a/src/Resources/public/gtm.enhancedEcommerce.checkout.js +++ b/src/Resources/public/gtm.enhancedEcommerce.checkout.js @@ -1,44 +1,56 @@ -(function ( $ ) { - 'use strict'; +(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()); - }); - } - }); + if (typeof checkoutStepsConfiguration !== "undefined" && checkoutStepsConfiguration.enabled === true) { + for (var stepId in checkoutStepsConfiguration.steps) { + if (checkoutStepsConfiguration.steps.hasOwnProperty(stepId)) { + checkoutStepsConfiguration.steps[stepId].forEach(function (conf) { + $(conf.selector).on(conf.event, function (e) { + 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 getCheckoutChoiceVal() { + return $('input[type=radio]:checked', this).val(); +} /** * @param {integer} step * @param {string} checkoutOption */ function enhancedEcommerceTrackCheckoutOption(step, checkoutOption) { - var obj = { - 'event': 'checkoutOption', - 'ecommerce': { - 'checkout_option': { - 'actionField': { - 'step': step, - 'option': checkoutOption - } - } + var obj = { + 'event': 'checkoutOption', + 'ecommerce': { + 'checkout_option': { + 'actionField': { + 'step': step, + 'option': checkoutOption } - }; + } + } + }; - /** global: dataLayer */ - dataLayer.push(obj); + /** global: dataLayer */ + dataLayer.push(obj); } diff --git a/src/Resources/views/Checkout/head.html.twig b/src/Resources/views/Checkout/head.html.twig new file mode 100644 index 0000000..a207b51 --- /dev/null +++ b/src/Resources/views/Checkout/head.html.twig @@ -0,0 +1,3 @@ + diff --git a/src/Twig/ParameterExtension.php b/src/Twig/ParameterExtension.php new file mode 100644 index 0000000..a127845 --- /dev/null +++ b/src/Twig/ParameterExtension.php @@ -0,0 +1,67 @@ +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 + */ + 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]); + } +} From de4190f065871e73643ffc110d66ea7e68dd6226 Mon Sep 17 00:00:00 2001 From: Francis Hilaire Date: Sat, 5 Jan 2019 18:49:14 +0100 Subject: [PATCH 3/8] Add information about the configuration of the checkoutOption events --- docs/checkout.md | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/docs/checkout.md b/docs/checkout.md index 99d7cc2..b21ae4d 100644 --- a/docs/checkout.md +++ b/docs/checkout.md @@ -1,5 +1,37 @@ # Checkout +## Configuration + +You can customize the steps of checkout by creating your own. This is the default one : + +```$yaml +# config/packages/sylius_gtm_enhanced_ecommerce.yaml + +sylius_gtm_enhanced_ecommerce: + features: + checkout: + steps: + 1: + - + event: "click" + selector: "a[href$='/checkout/']" + 2: + - + selector: "form[name=sylius_checkout_address]" + 3: + - + selector: "form[name=sylius_checkout_select_shipping]" + option: "getCheckoutChoiceVal" + 4: + - + selector: "form[name=sylius_checkout_select_payment]" + option: "getCheckoutChoiceVal" +``` + +The configuration allow you to add or remove steps, choice a specific js event to listen, +a specific selector to put this event and finally allow you to give a global function which +give you the possibility to add additional information to GA. + ## Google Documentation https://developers.google.com/tag-manager/enhanced-ecommerce#checkout @@ -59,4 +91,4 @@ Add a tag: * Action: `Checkout Option` * Enable Enhanced Ecommerce * Use dataLayer -* Trigger on: see trigger above. \ No newline at end of file +* Trigger on: see trigger above. From ca2dd0993cc2ee924df51ecfb165768cc2bb1726 Mon Sep 17 00:00:00 2001 From: Francis Hilaire Date: Fri, 25 Jan 2019 20:54:50 +0100 Subject: [PATCH 4/8] Make final --- src/Twig/ParameterExtension.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Twig/ParameterExtension.php b/src/Twig/ParameterExtension.php index a127845..0808e66 100644 --- a/src/Twig/ParameterExtension.php +++ b/src/Twig/ParameterExtension.php @@ -5,10 +5,10 @@ use Twig\Extension\AbstractExtension; use Twig\TwigFunction; -class ParameterExtension extends AbstractExtension +final class ParameterExtension extends AbstractExtension { /** @var array */ - protected $parameters; + private $parameters; /** * @param bool $purchases @@ -49,7 +49,7 @@ public function getFunctions() /** * @param string $name - * @return bool|array + * @return bool|array|null */ public function getParameter(string $name) { From e790170090e0e806c2dc0a30c11035f75c32c208 Mon Sep 17 00:00:00 2001 From: Francis Hilaire Date: Fri, 25 Jan 2019 20:57:08 +0100 Subject: [PATCH 5/8] add prefix to global fct, and make the code more readable --- docs/checkout.md | 6 +-- src/DependencyInjection/Configuration.php | 4 +- .../public/gtm.enhancedEcommerce.checkout.js | 43 +++++++++++-------- 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/docs/checkout.md b/docs/checkout.md index b21ae4d..b21cd35 100644 --- a/docs/checkout.md +++ b/docs/checkout.md @@ -2,7 +2,7 @@ ## Configuration -You can customize the steps of checkout by creating your own. This is the default one : +You can customize the steps of checkout by creating your own. This is the default one: ```$yaml # config/packages/sylius_gtm_enhanced_ecommerce.yaml @@ -20,12 +20,12 @@ sylius_gtm_enhanced_ecommerce: selector: "form[name=sylius_checkout_address]" 3: - + option: "enhancedEcommerceCheckoutGetChoiceVal" selector: "form[name=sylius_checkout_select_shipping]" - option: "getCheckoutChoiceVal" 4: - + option: "enhancedEcommerceCheckoutGetChoiceVal" selector: "form[name=sylius_checkout_select_payment]" - option: "getCheckoutChoiceVal" ``` The configuration allow you to add or remove steps, choice a specific js event to listen, diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 0908125..549025a 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -48,14 +48,14 @@ public function getConfigTreeBuilder() [ 'event' => 'submit', 'selector' => 'form[name=sylius_checkout_select_shipping]', - 'option' => 'getCheckoutChoiceVal', + 'option' => 'enhancedEcommerceCheckoutGetChoiceVal', ] ], 4 => [ [ 'event' => 'submit', 'selector' => 'form[name=sylius_checkout_select_payment]', - 'option' => 'getCheckoutChoiceVal', + 'option' => 'enhancedEcommerceCheckoutGetChoiceVal', ] ], ]) diff --git a/src/Resources/public/gtm.enhancedEcommerce.checkout.js b/src/Resources/public/gtm.enhancedEcommerce.checkout.js index 2a73835..7e0116e 100644 --- a/src/Resources/public/gtm.enhancedEcommerce.checkout.js +++ b/src/Resources/public/gtm.enhancedEcommerce.checkout.js @@ -1,19 +1,28 @@ +'use strict'; (function ($) { - 'use strict'; - if (typeof checkoutStepsConfiguration !== "undefined" && checkoutStepsConfiguration.enabled === true) { - for (var stepId in checkoutStepsConfiguration.steps) { - if (checkoutStepsConfiguration.steps.hasOwnProperty(stepId)) { - checkoutStepsConfiguration.steps[stepId].forEach(function (conf) { - $(conf.selector).on(conf.event, function (e) { - var option = null; - if (typeof conf.option !== "undefined" && typeof window[conf.option] === 'function') { - option = window[conf.option].call(this); - } - enhancedEcommerceTrackCheckoutOption(stepId, option); - }); + 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)) { + checkoutStepsConfiguration.steps[stepId].forEach(function BindStep(conf) { + $(conf.selector).on(conf.event, function (e) { + var option = null; + + if ( + typeof conf.option !== "undefined" + && typeof window[conf.option] === 'function' + ) { + option = window[conf.option].call(this); + } + + enhancedEcommerceTrackCheckoutOption(stepId, option); }); - } + }); } } })(jQuery); @@ -30,21 +39,21 @@ * * @returns {string} */ -function getCheckoutChoiceVal() { +function enhancedEcommerceCheckoutGetChoiceVal() { return $('input[type=radio]:checked', this).val(); } /** - * @param {integer} step + * @param {integer} stepId * @param {string} checkoutOption */ -function enhancedEcommerceTrackCheckoutOption(step, checkoutOption) { +function enhancedEcommerceTrackCheckoutOption(stepId, checkoutOption) { var obj = { 'event': 'checkoutOption', 'ecommerce': { 'checkout_option': { 'actionField': { - 'step': step, + 'step': stepId, 'option': checkoutOption } } From 46e7d6d18770c6cc290f68f4ae8f8ae3f1fec1b7 Mon Sep 17 00:00:00 2001 From: Francis Hilaire Date: Fri, 25 Jan 2019 21:37:37 +0100 Subject: [PATCH 6/8] Add tests for ParameterExtension --- tests/Twig/ParameterExtensionTest.php | 61 +++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 tests/Twig/ParameterExtensionTest.php diff --git a/tests/Twig/ParameterExtensionTest.php b/tests/Twig/ParameterExtensionTest.php new file mode 100644 index 0000000..baf1329 --- /dev/null +++ b/tests/Twig/ParameterExtensionTest.php @@ -0,0 +1,61 @@ + $template + ]), + [ + 'debug' => true, + 'cache' => false, + 'autoescape' => 'html', + 'optimizations' => 0 + ] + ); + $twig->addExtension(new ParameterExtension( + true, + true, + true, + true, + true, + [] + )); + + return $twig; + } + + public function testParameterCreationForArrayResult() + { + $template = "{{ sylius_gtm_enhanced_ecommerce_parameter('checkout')|json_encode }}"; + $output = $this->initTwigForTemplateParameter($template)->render('template'); + $this->assertEquals('[]', $output); + } + + public function testParameterCreationForBoolResult() + { + $template = "{{ sylius_gtm_enhanced_ecommerce_parameter('purchases') }}"; + $output = $this->initTwigForTemplateParameter($template)->render('template'); + $this->assertEquals('1', $output); + } + + public function testParameterCreationForNotExistingParameter() + { + $template = "{{ sylius_gtm_enhanced_ecommerce_parameter('not_existing_parameter') }}"; + $output = $this->initTwigForTemplateParameter($template)->render('template'); + $this->assertEmpty($output); + } +} From ae4eae5f2e95be8f6fb309006e830c42a626d2e1 Mon Sep 17 00:00:00 2001 From: Francis Hilaire Date: Fri, 25 Jan 2019 22:17:19 +0100 Subject: [PATCH 7/8] Add Configuration tests --- ...yliusGtmEnhancedEcommerceExtensionTest.php | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 tests/DependencyInjection/SyliusGtmEnhancedEcommerceExtensionTest.php diff --git a/tests/DependencyInjection/SyliusGtmEnhancedEcommerceExtensionTest.php b/tests/DependencyInjection/SyliusGtmEnhancedEcommerceExtensionTest.php new file mode 100644 index 0000000..7c51035 --- /dev/null +++ b/tests/DependencyInjection/SyliusGtmEnhancedEcommerceExtensionTest.php @@ -0,0 +1,75 @@ +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() + { + $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() + { + 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/', + ])); + } +} From a2d047918540affe268b55c7b5108498b5cd4380 Mon Sep 17 00:00:00 2001 From: Francis Hilaire Date: Tue, 29 Jan 2019 12:51:59 +0100 Subject: [PATCH 8/8] Extend abbrev, make final, strict and add return types --- docs/checkout.md | 4 +-- src/DependencyInjection/Configuration.php | 4 +-- .../public/gtm.enhancedEcommerce.checkout.js | 28 +++++++++---------- ...yliusGtmEnhancedEcommerceExtensionTest.php | 10 +++---- tests/EventListener/ThankYouListenerTest.php | 2 +- tests/TagManager/AddTransactionTest.php | 2 +- tests/Twig/ParameterExtensionTest.php | 13 ++++----- 7 files changed, 30 insertions(+), 33 deletions(-) diff --git a/docs/checkout.md b/docs/checkout.md index b21cd35..8fa89d6 100644 --- a/docs/checkout.md +++ b/docs/checkout.md @@ -20,11 +20,11 @@ sylius_gtm_enhanced_ecommerce: selector: "form[name=sylius_checkout_address]" 3: - - option: "enhancedEcommerceCheckoutGetChoiceVal" + option: "enhancedEcommerceCheckoutGetChoiceValue" selector: "form[name=sylius_checkout_select_shipping]" 4: - - option: "enhancedEcommerceCheckoutGetChoiceVal" + option: "enhancedEcommerceCheckoutGetChoiceValue" selector: "form[name=sylius_checkout_select_payment]" ``` diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 549025a..0ffd7d7 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -48,14 +48,14 @@ public function getConfigTreeBuilder() [ 'event' => 'submit', 'selector' => 'form[name=sylius_checkout_select_shipping]', - 'option' => 'enhancedEcommerceCheckoutGetChoiceVal', + 'option' => 'enhancedEcommerceCheckoutGetChoiceValue', ] ], 4 => [ [ 'event' => 'submit', 'selector' => 'form[name=sylius_checkout_select_payment]', - 'option' => 'enhancedEcommerceCheckoutGetChoiceVal', + 'option' => 'enhancedEcommerceCheckoutGetChoiceValue', ] ], ]) diff --git a/src/Resources/public/gtm.enhancedEcommerce.checkout.js b/src/Resources/public/gtm.enhancedEcommerce.checkout.js index 7e0116e..a0d4dd0 100644 --- a/src/Resources/public/gtm.enhancedEcommerce.checkout.js +++ b/src/Resources/public/gtm.enhancedEcommerce.checkout.js @@ -8,22 +8,22 @@ if (!checkoutStepsConfiguration.hasOwnProperty('steps')) return; for (var stepId in checkoutStepsConfiguration.steps) { - if (checkoutStepsConfiguration.steps.hasOwnProperty(stepId)) { - checkoutStepsConfiguration.steps[stepId].forEach(function BindStep(conf) { - $(conf.selector).on(conf.event, function (e) { - var option = null; + if (!checkoutStepsConfiguration.steps.hasOwnProperty(stepId)) continue; - if ( - typeof conf.option !== "undefined" - && typeof window[conf.option] === 'function' - ) { - option = window[conf.option].call(this); - } + checkoutStepsConfiguration.steps[stepId].forEach(function BindStep(conf) { + $(conf.selector).on(conf.event, function () { + var option = null; - enhancedEcommerceTrackCheckoutOption(stepId, option); - }); + if ( + typeof conf.option !== "undefined" + && typeof window[conf.option] === 'function' + ) { + option = window[conf.option].call(this); + } + + enhancedEcommerceTrackCheckoutOption(stepId, option); }); - } + }); } })(jQuery); @@ -39,7 +39,7 @@ * * @returns {string} */ -function enhancedEcommerceCheckoutGetChoiceVal() { +function enhancedEcommerceCheckoutGetChoiceValue() { return $('input[type=radio]:checked', this).val(); } diff --git a/tests/DependencyInjection/SyliusGtmEnhancedEcommerceExtensionTest.php b/tests/DependencyInjection/SyliusGtmEnhancedEcommerceExtensionTest.php index 7c51035..aec477c 100644 --- a/tests/DependencyInjection/SyliusGtmEnhancedEcommerceExtensionTest.php +++ b/tests/DependencyInjection/SyliusGtmEnhancedEcommerceExtensionTest.php @@ -1,4 +1,4 @@ -getContainer(); $extension = new SyliusGtmEnhancedEcommerceExtension(); @@ -46,7 +46,7 @@ public function testMinimalConfig() ); } - public function testWithCacheResolver() + public function testWithCacheResolver(): void { $container = $this->getContainer(); $extension = new SyliusGtmEnhancedEcommerceExtension(); @@ -62,7 +62,7 @@ public function testWithCacheResolver() ); } - private function getContainer() + private function getContainer(): ContainerBuilder { return new ContainerBuilder(new ParameterBag([ 'kernel.debug' => false, diff --git a/tests/EventListener/ThankYouListenerTest.php b/tests/EventListener/ThankYouListenerTest.php index edbc68e..b0e3bdf 100644 --- a/tests/EventListener/ThankYouListenerTest.php +++ b/tests/EventListener/ThankYouListenerTest.php @@ -1,4 +1,4 @@ - 0 ] ); + $twig->addExtension(new ParameterExtension( true, true, @@ -38,21 +35,21 @@ private function initTwigForTemplateParameter(string $template): Environment return $twig; } - public function testParameterCreationForArrayResult() + public function testParameterCreationForArrayResult(): void { $template = "{{ sylius_gtm_enhanced_ecommerce_parameter('checkout')|json_encode }}"; $output = $this->initTwigForTemplateParameter($template)->render('template'); $this->assertEquals('[]', $output); } - public function testParameterCreationForBoolResult() + public function testParameterCreationForBoolResult(): void { $template = "{{ sylius_gtm_enhanced_ecommerce_parameter('purchases') }}"; $output = $this->initTwigForTemplateParameter($template)->render('template'); $this->assertEquals('1', $output); } - public function testParameterCreationForNotExistingParameter() + public function testParameterCreationForNotExistingParameter(): void { $template = "{{ sylius_gtm_enhanced_ecommerce_parameter('not_existing_parameter') }}"; $output = $this->initTwigForTemplateParameter($template)->render('template');