From fa38f0effc31a9aa1c4748e669e16c6166a8cc72 Mon Sep 17 00:00:00 2001 From: Eduardo Morales Date: Fri, 11 Sep 2020 13:08:12 +0200 Subject: [PATCH 1/3] Added new step to check events gtm. --- composer.json | 3 +- src/Context/GtmContext.php | 96 ++++++++++++++++++++++++++++++++++---- 2 files changed, 89 insertions(+), 10 deletions(-) diff --git a/composer.json b/composer.json index 912b699..ec9ee18 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,8 @@ "minimum-stability": "dev", "prefer-stable": true, "require": { - "behat/mink": "~1.5" + "behat/mink": "~1.5", + "behat/gherkin": "^4.5.1" }, "autoload": { "psr-4": {"DennisDigital\\Behat\\Gtm\\": "src"} diff --git a/src/Context/GtmContext.php b/src/Context/GtmContext.php index 7ccff97..c1a08e3 100644 --- a/src/Context/GtmContext.php +++ b/src/Context/GtmContext.php @@ -1,16 +1,20 @@ getHash(); + $values_array = $hash[0]; + + $event_array = $this->getDatalayerEvent($event); + foreach ($values_array as $property => $value) { + $this->checkDatalayerProperty($event_array, $property, $value); + } + } + + /** + * Check datalayer property value. + * + * @param array $event_array + * Event array. + * @param string $property + * Property datalayer. + * @param string $value + * Value datalayer. + * + * @throws \Exception + */ + public function checkDatalayerProperty(array $event_array, $property, $value) { + if (!isset($event_array['event'])) { + throw new \Exception('Event not found.'); + } + $event_name = $event_array['event']; + + if (!isset($event_array[$property])) { + throw new \Exception('Property ' . $property . ' not found on event ' . $event_name); + } + if ($event_array[$property] != $value) { + throw new \Exception('Value ' . $value . ' not found on event ' . $event_name . ', value of property ' . $property . ' is' . $event_array[$property]); + } + } + + /** + * Obtain datalater event array from event name. + * + * @param string $event + * Event. * - * @param $key * @return mixed + * Event. + * + * @throws \Exception + */ + protected function getDatalayerEvent($event) { + $json_arr = $this->getDataLayerJson(); + + // Loop through the array and return the data layer event. + foreach ($json_arr as $json_item) { + if (isset($json_item['event']) && $json_item['event'] == $event) { + return $json_item; + } + } + throw new \Exception('Event' . $event . ' not found.'); + } + + /** + * Get Google Tag Manager Data Layer value. + * + * @param string $key + * Datalayer key. + * + * @return mixed + * Datalayer value from key. + * * @throws \Exception */ protected function getDataLayerValue($key) { $json_arr = $this->getDataLayerJson(); - // Loop through the array and return the data layer value + // Loop through the array and return the data layer value. foreach ($json_arr as $json_item) { if (isset($json_item[$key])) { return $json_item[$key]; @@ -92,8 +169,8 @@ protected function getDataLayerJsonFromSource() { // Get the html. $html = $this->getSession()->getPage()->getContent(); - // Get the dataLayer json and json_decode it - preg_match('~dataLayer\s*=\s*(.*?);~' , $html, $match); + // Get the dataLayer json and json_decode it. + preg_match('~dataLayer\s*=\s*(.*?);~', $html, $match); if (!isset($match[0])) { throw new \Exception('dataLayer variable not found in source.'); } @@ -113,4 +190,5 @@ protected function getDataLayerJsonFromJS() { return $json_arr; } + } From 0b562b7b6513b304393eb3f655945ac72e7a37bd Mon Sep 17 00:00:00 2001 From: Eduardo Morales Date: Fri, 11 Sep 2020 18:18:30 +0200 Subject: [PATCH 2/3] Allow to use wildcards and multilvel arrays. --- src/Context/GtmContext.php | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/Context/GtmContext.php b/src/Context/GtmContext.php index c1a08e3..9f34a60 100644 --- a/src/Context/GtmContext.php +++ b/src/Context/GtmContext.php @@ -57,14 +57,14 @@ public function getDataLayerSettingShouldMatch($key, $regex) { * @Given google tag manager data layer event :event should have the following values: * * Example: + * Wildcards * and ? allowed, and also multilevel arrays. * Given google tag manager data layer event "sign_event" should have the following values: - * | eventAction | eventCategory | eventLabel | - * | behat_gtm_campaign | firma | F_web | + * | eventAction | eventCategory | eventLabel | ecommerce:purchase:actionField:revenue | ecommerce:purchase:actionField:id | + * | behat_gtm_campaign | firma | F_web | 120 | ?_web_* | */ public function dataLayerEventShouldHavePropertyValue($event, TableNode $values) { $hash = $values->getHash(); $values_array = $hash[0]; - $event_array = $this->getDatalayerEvent($event); foreach ($values_array as $property => $value) { $this->checkDatalayerProperty($event_array, $property, $value); @@ -78,25 +78,39 @@ public function dataLayerEventShouldHavePropertyValue($event, TableNode $values) * Event array. * @param string $property * Property datalayer. - * @param string $value + * @param string $expected_value * Value datalayer. * * @throws \Exception */ - public function checkDatalayerProperty(array $event_array, $property, $value) { + public function checkDatalayerProperty(array $event_array, $property, $expected_value) { if (!isset($event_array['event'])) { throw new \Exception('Event not found.'); } $event_name = $event_array['event']; - if (!isset($event_array[$property])) { - throw new \Exception('Property ' . $property . ' not found on event ' . $event_name); + $value = $event_array; + $property_keys = explode(':', $property); + foreach ($property_keys as $key) { + if(!isset($value[$key])) { + throw new \Exception('Property ' . $property . ' not found on event ' . $event_name); + } + $value = $value[$key]; } - if ($event_array[$property] != $value) { - throw new \Exception('Value ' . $value . ' not found on event ' . $event_name . ', value of property ' . $property . ' is' . $event_array[$property]); + + if (!$this->wildcardMatch($expected_value, $value)) { + throw new \Exception('Value ' . $expected_value . ' not found on event ' . $event_name . ', value of property ' . $property . ' is' . $value); } } + public function wildcardMatch($pattern, $subject) { + $pattern = strtr($pattern, [ + '*' => '.*?', // 0 or more (lazy) - asterisk (*) + '?' => '.', // 1 character - question mark (?) + ]); + return preg_match("/$pattern/", $subject); + } + /** * Obtain datalater event array from event name. * @@ -117,7 +131,7 @@ protected function getDatalayerEvent($event) { return $json_item; } } - throw new \Exception('Event' . $event . ' not found.'); + throw new \Exception('Event ' . $event . ' not found.'); } /** From 39c8be73d2cfc795e9db9b382df3019840b81b8d Mon Sep 17 00:00:00 2001 From: Eduardo Morales Date: Fri, 11 Sep 2020 19:35:21 +0200 Subject: [PATCH 3/3] Fix error message. --- src/Context/GtmContext.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Context/GtmContext.php b/src/Context/GtmContext.php index 9f34a60..22722f4 100644 --- a/src/Context/GtmContext.php +++ b/src/Context/GtmContext.php @@ -99,7 +99,7 @@ public function checkDatalayerProperty(array $event_array, $property, $expected_ } if (!$this->wildcardMatch($expected_value, $value)) { - throw new \Exception('Value ' . $expected_value . ' not found on event ' . $event_name . ', value of property ' . $property . ' is' . $value); + throw new \Exception('Value ' . $expected_value . ' not found on event ' . $event_name . ', value of property ' . $property . ' is ' . $value); } }