From f9cbf75aa945b2997c5e11adf14b4ed6fa0bc3fd Mon Sep 17 00:00:00 2001 From: Marcelo Vani Date: Tue, 12 Sep 2023 11:11:10 +0100 Subject: [PATCH 01/11] Added step definition to wait for Datalayer values. --- src/Context/GtmContext.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/Context/GtmContext.php b/src/Context/GtmContext.php index 7ccff97..de52df6 100644 --- a/src/Context/GtmContext.php +++ b/src/Context/GtmContext.php @@ -23,6 +23,27 @@ public function tagManagerIdIs($id) { } } + /** + * Waits until the Datalayer object is updated and then checks if property is available. + * + * @Given I wait for the data layer setting :arg1 + */ + public function waitDataLayerSetting($key, $loops = 10) { + $loop = 0; + do { + try { + $loop++; + $this->getDataLayerValue($key); + return true; + } catch ($e) { + // Ommit the exception until we finish the loop. + } + sleep(1); + } while ($loop < $loops); + + throw new \Exception("$key not found after waiting for $loops seconds."); + } + /** * Check google tag manager data layer contain key value pair * From fcb8fa75492cac71b76208916a996ce4e62cdc59 Mon Sep 17 00:00:00 2001 From: Marcelo Vani Date: Tue, 12 Sep 2023 11:31:38 +0100 Subject: [PATCH 02/11] Update GtmContext.php --- 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 de52df6..0e44ac2 100644 --- a/src/Context/GtmContext.php +++ b/src/Context/GtmContext.php @@ -35,7 +35,7 @@ public function waitDataLayerSetting($key, $loops = 10) { $loop++; $this->getDataLayerValue($key); return true; - } catch ($e) { + } catch (\Exception $e) { // Ommit the exception until we finish the loop. } sleep(1); From c82e3dec090e686a5543a0923846f41f5e413a4f Mon Sep 17 00:00:00 2001 From: yashmitkharvi Date: Tue, 19 Sep 2023 12:18:12 +0530 Subject: [PATCH 03/11] Nested Loops --- composer.json | 3 ++- src/Context/GtmContext.php | 49 ++++++++++++++++++++++++++++++-------- 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/composer.json b/composer.json index 912b699..4b5ce6f 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", + "adbario/php-dot-notation": "^3.3" }, "autoload": { "psr-4": {"DennisDigital\\Behat\\Gtm\\": "src"} diff --git a/src/Context/GtmContext.php b/src/Context/GtmContext.php index 0e44ac2..4543370 100644 --- a/src/Context/GtmContext.php +++ b/src/Context/GtmContext.php @@ -75,17 +75,46 @@ public function getDataLayerSettingShouldMatch($key, $regex) { * @return mixed * @throws \Exception */ - protected function getDataLayerValue($key) { - $json_arr = $this->getDataLayerJson(); - - // 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]; - } + protected function getDataLayerValue($key) { + $json_arr = $this->getDataLayerJson(); + // Loop through the array and return the data layer value + foreach ($json_arr as $json_item) { + // Check if the key contains dot. + if (strpos($key, '.', 0) !== false) { + if (strpos($key, '[', 0) !== false) { + $dot = dot($json_item); + // Trim before [ + $pos = strpos($key, '['); + if ($pos !== false) { + $trimmedString = substr($key, 0, $pos); + } + $value = $dot->get($trimmedString); + if ($value !== NULL) { + // Filter the Numbers from String + $int_var = (int)filter_var($key, FILTER_SANITIZE_NUMBER_INT); + //Filter last variable from key + $lastDotPosition = strrpos($key, "."); + if ($lastDotPosition !== false) { + $last_variable = substr($key, $lastDotPosition + 1); + print_r(__LINE__, $last_variable); + } + $dot_array = dot($value[$int_var]); + $value_array = $dot_array->get($last_variable); + return $value_array; + } + } else { + $dot = dot($json_item); + $value = $dot->get($key); + if ($value !== NULL) { + return $value; + } + } + } elseif (isset($json_item[$key])) { + return $json_item[$key]; + } + } + throw new \Exception($key . ' not found.'); } - throw new \Exception($key . ' not found.'); - } /** * Get dataLayer variable JSON. From 7135805743c26fcc73ecff9e43d88ce8b37c680a Mon Sep 17 00:00:00 2001 From: yashmitkharvi Date: Thu, 21 Sep 2023 15:58:47 +0530 Subject: [PATCH 04/11] Nested Loops. --- src/Context/GtmContext.php | 69 ++++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 28 deletions(-) diff --git a/src/Context/GtmContext.php b/src/Context/GtmContext.php index 4543370..d001ce9 100644 --- a/src/Context/GtmContext.php +++ b/src/Context/GtmContext.php @@ -3,6 +3,7 @@ use Behat\Mink\Driver\Selenium2Driver; use Behat\MinkExtension\Context\RawMinkContext; +use Adbar\Dot; /** * Class GtmContext @@ -75,39 +76,16 @@ public function getDataLayerSettingShouldMatch($key, $regex) { * @return mixed * @throws \Exception */ - protected function getDataLayerValue($key) { + protected function getDataLayerValue($key) + { $json_arr = $this->getDataLayerJson(); // Loop through the array and return the data layer value foreach ($json_arr as $json_item) { // Check if the key contains dot. if (strpos($key, '.', 0) !== false) { - if (strpos($key, '[', 0) !== false) { - $dot = dot($json_item); - // Trim before [ - $pos = strpos($key, '['); - if ($pos !== false) { - $trimmedString = substr($key, 0, $pos); - } - $value = $dot->get($trimmedString); - if ($value !== NULL) { - // Filter the Numbers from String - $int_var = (int)filter_var($key, FILTER_SANITIZE_NUMBER_INT); - //Filter last variable from key - $lastDotPosition = strrpos($key, "."); - if ($lastDotPosition !== false) { - $last_variable = substr($key, $lastDotPosition + 1); - print_r(__LINE__, $last_variable); - } - $dot_array = dot($value[$int_var]); - $value_array = $dot_array->get($last_variable); - return $value_array; - } - } else { - $dot = dot($json_item); - $value = $dot->get($key); - if ($value !== NULL) { - return $value; - } + $value = $this->getDotValue($json_item, $key); + if (!is_null($value)) { + return $value; } } elseif (isset($json_item[$key])) { return $json_item[$key]; @@ -116,6 +94,41 @@ protected function getDataLayerValue($key) { throw new \Exception($key . ' not found.'); } + /** + * Convert dataLayer values to dot notation + */ + private function getDotValue($arr, $key) + { + $dot = dot($arr); + // Check if the array contains dot. + if (strpos($key, '[', 0) !== false) { + // Trim before [ + $pos = strpos($key, '['); + if ($pos !== false) { + $trimmedString = substr($key, 0, $pos); + } + $value = $dot->get($trimmedString); + if ($value !== NULL) { + // Filter the number from string. + $int_var = (int)filter_var($key, FILTER_SANITIZE_NUMBER_INT); + // Filter last variable from key. + $lastDotPosition = strrpos($key, "."); + if ($lastDotPosition !== false) { + $last_variable = substr($key, $lastDotPosition + 1); + } + $dot_array = dot($value[$int_var]); + $value_array = $dot_array->get($last_variable); + return $value_array; + } + } else { + $dot = dot($arr); + $value = $dot->get($key); + if ($value !== NULL) { + return $value; + } + } + } + /** * Get dataLayer variable JSON. */ From 54c1b9f5ebd29c946afb1e1c8722ed7aae90ec33 Mon Sep 17 00:00:00 2001 From: yashmitkharvi Date: Mon, 25 Sep 2023 14:25:58 +0530 Subject: [PATCH 05/11] Nested Loops. --- src/Context/GtmContext.php | 57 +++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/src/Context/GtmContext.php b/src/Context/GtmContext.php index d001ce9..953c7d1 100644 --- a/src/Context/GtmContext.php +++ b/src/Context/GtmContext.php @@ -79,10 +79,11 @@ public function getDataLayerSettingShouldMatch($key, $regex) { 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) { // Check if the key contains dot. if (strpos($key, '.', 0) !== false) { + // Get value using dot notation. $value = $this->getDotValue($json_item, $key); if (!is_null($value)) { return $value; @@ -95,40 +96,50 @@ protected function getDataLayerValue($key) } /** - * Convert dataLayer values to dot notation + * Convert arrays :arg1 to dot notation :arg2 */ - private function getDotValue($arr, $key) + private function getDotValue($value, $key) { - $dot = dot($arr); - // Check if the array contains dot. - if (strpos($key, '[', 0) !== false) { + $dot = dot($value); + // Check if the key contains [. + // When the key contains [0] it means that we are trying to get the value from specific index in an array. + // i.e. foo.bar[0].foo. + $pos = strpos($key, '[',0); + if ($pos) { // Trim before [ - $pos = strpos($key, '['); - if ($pos !== false) { - $trimmedString = substr($key, 0, $pos); - } - $value = $dot->get($trimmedString); - if ($value !== NULL) { + $keyBeforeArray = substr($key, 0, $pos); + $values = $dot->get($keyBeforeArray); + if (!empty($values)) { // Filter the number from string. - $int_var = (int)filter_var($key, FILTER_SANITIZE_NUMBER_INT); + $int_var = $this->getNumberInsideKey($key); // Filter last variable from key. - $lastDotPosition = strrpos($key, "."); - if ($lastDotPosition !== false) { - $last_variable = substr($key, $lastDotPosition + 1); + $arrayIndexPosition = strrpos($key, "]."); + if ($arrayIndexPosition) { + $keyAfterArray = substr($key, $arrayIndexPosition + 2); } - $dot_array = dot($value[$int_var]); - $value_array = $dot_array->get($last_variable); - return $value_array; + $dot_array = dot($values[$int_var]); + return $dot_array->get($keyAfterArray); } } else { - $dot = dot($arr); - $value = $dot->get($key); - if ($value !== NULL) { - return $value; + $dot = dot($value); + $valueNonArray = $dot->get($key); + if ($valueNonArray !== NULL) { + return $valueNonArray; } } } + /** + * Get number inside array index :arg2 + */ + protected function getNumberInsideKey($key) { + $pattern = '/\[(\d+)]/'; + if (preg_match_all($pattern, $key, $matches)) { + return $matches[1][0]; + } + throw new \Exception('Number not found between' . $key ); + } + /** * Get dataLayer variable JSON. */ From 9f6f0d135ccff0c3f87a20176aba303cd13a6dec Mon Sep 17 00:00:00 2001 From: yashmitkharvi Date: Tue, 26 Sep 2023 10:48:08 +0530 Subject: [PATCH 06/11] Nested Loops. --- src/Context/GtmContext.php | 108 +++++++++++++++++++------------------ 1 file changed, 55 insertions(+), 53 deletions(-) diff --git a/src/Context/GtmContext.php b/src/Context/GtmContext.php index 953c7d1..2898a03 100644 --- a/src/Context/GtmContext.php +++ b/src/Context/GtmContext.php @@ -76,63 +76,65 @@ public function getDataLayerSettingShouldMatch($key, $regex) { * @return mixed * @throws \Exception */ - protected function getDataLayerValue($key) - { - $json_arr = $this->getDataLayerJson(); - // Loop through the array and return the data layer value. - foreach ($json_arr as $json_item) { - // Check if the key contains dot. - if (strpos($key, '.', 0) !== false) { - // Get value using dot notation. - $value = $this->getDotValue($json_item, $key); - if (!is_null($value)) { - return $value; - } - } elseif (isset($json_item[$key])) { - return $json_item[$key]; - } - } - throw new \Exception($key . ' not found.'); - } + protected function getDataLayerValue($key) { + $json_arr = $this->getDataLayerJson(); + // Loop through the array and return the data layer value. + foreach ($json_arr as $json_item) { + // Check if the key contains dot. + if (strpos($key, '.', 0) !== false) { + // Get value using dot notation. + $value = $this->getDotValue($json_item, $key); + if (!is_null($value)) { + return $value; + } + } elseif (isset($json_item[$key])) { + return $json_item[$key]; + } + } + throw new \Exception($key . ' not found.'); + } - /** - * Convert arrays :arg1 to dot notation :arg2 - */ - private function getDotValue($value, $key) - { - $dot = dot($value); - // Check if the key contains [. - // When the key contains [0] it means that we are trying to get the value from specific index in an array. - // i.e. foo.bar[0].foo. - $pos = strpos($key, '[',0); - if ($pos) { - // Trim before [ - $keyBeforeArray = substr($key, 0, $pos); - $values = $dot->get($keyBeforeArray); - if (!empty($values)) { - // Filter the number from string. - $int_var = $this->getNumberInsideKey($key); - // Filter last variable from key. - $arrayIndexPosition = strrpos($key, "]."); - if ($arrayIndexPosition) { - $keyAfterArray = substr($key, $arrayIndexPosition + 2); - } - $dot_array = dot($values[$int_var]); - return $dot_array->get($keyAfterArray); - } - } else { - $dot = dot($value); - $valueNonArray = $dot->get($key); - if ($valueNonArray !== NULL) { - return $valueNonArray; - } - } - } + /** + * Convert arrays to dot notation + * @param $value + * @param $key + * @return mixed|void + */ + private function getDotValue($value, $key) { + $dot = dot($value); + // Check if the key contains [. + // When the key contains [0] it means that we are trying to get the value from specific index in an array. + // i.e. foo.bar[0].foo. + $pos = strpos($key, '[',0); + if ($pos) { + // Trim before [. + $keyBeforeArray = substr($key, 0, $pos); + $values = $dot->get($keyBeforeArray); + if (!empty($values)) { + // Filter the number from string. + $index = $this->getNumberFromString($key); + // Filter last variable from key. + $arrayIndexPosition = strrpos($key, "]."); + if ($arrayIndexPosition) { + $key = substr($key, $arrayIndexPosition + 2); + } + $value = dot($values[$index]); + } + } + $dot = dot($value); + $value = $dot->get($key); + if (!is_null($value)) { + return $value; + } + } /** - * Get number inside array index :arg2 + * Get number inside array index + * @param $key + * @return string + * @throws \Exception */ - protected function getNumberInsideKey($key) { + protected function getNumberFromString($key) { $pattern = '/\[(\d+)]/'; if (preg_match_all($pattern, $key, $matches)) { return $matches[1][0]; From 9e204eddc3c59550488d28d29707b644e8a69441 Mon Sep 17 00:00:00 2001 From: yashmitkharvi Date: Tue, 26 Sep 2023 11:04:07 +0530 Subject: [PATCH 07/11] Nested Loops. --- src/Context/GtmContext.php | 86 +++++++++++++++++++------------------- 1 file changed, 43 insertions(+), 43 deletions(-) diff --git a/src/Context/GtmContext.php b/src/Context/GtmContext.php index 2898a03..6b4af0a 100644 --- a/src/Context/GtmContext.php +++ b/src/Context/GtmContext.php @@ -77,21 +77,21 @@ public function getDataLayerSettingShouldMatch($key, $regex) { * @throws \Exception */ protected function getDataLayerValue($key) { - $json_arr = $this->getDataLayerJson(); - // Loop through the array and return the data layer value. - foreach ($json_arr as $json_item) { - // Check if the key contains dot. - if (strpos($key, '.', 0) !== false) { - // Get value using dot notation. - $value = $this->getDotValue($json_item, $key); - if (!is_null($value)) { - return $value; - } - } elseif (isset($json_item[$key])) { - return $json_item[$key]; - } + $json_arr = $this->getDataLayerJson(); + // Loop through the array and return the data layer value. + foreach ($json_arr as $json_item) { + // Check if the key contains dot. + if (strpos($key, '.', 0) !== false) { + // Get value using dot notation. + $value = $this->getDotValue($json_item, $key); + if (!is_null($value)) { + return $value; + } + } elseif (isset($json_item[$key])) { + return $json_item[$key]; } - throw new \Exception($key . ' not found.'); + } + throw new \Exception($key . ' not found.'); } /** @@ -101,31 +101,31 @@ protected function getDataLayerValue($key) { * @return mixed|void */ private function getDotValue($value, $key) { - $dot = dot($value); - // Check if the key contains [. - // When the key contains [0] it means that we are trying to get the value from specific index in an array. - // i.e. foo.bar[0].foo. - $pos = strpos($key, '[',0); - if ($pos) { - // Trim before [. - $keyBeforeArray = substr($key, 0, $pos); - $values = $dot->get($keyBeforeArray); - if (!empty($values)) { - // Filter the number from string. - $index = $this->getNumberFromString($key); - // Filter last variable from key. - $arrayIndexPosition = strrpos($key, "]."); - if ($arrayIndexPosition) { - $key = substr($key, $arrayIndexPosition + 2); - } - $value = dot($values[$index]); - } - } - $dot = dot($value); - $value = $dot->get($key); - if (!is_null($value)) { - return $value; + $dot = dot($value); + // Check if the key contains [. + // When the key contains [0] it means that we are trying to get the value from specific index in an array. + // i.e. foo.bar[0].foo. + $pos = strpos($key, '[',0); + if ($pos) { + // Trim before [. + $keyBeforeArray = substr($key, 0, $pos); + $values = $dot->get($keyBeforeArray); + if (!empty($values)) { + // Filter the number from string. + $index = $this->getNumberFromString($key); + // Filter last variable from key. + $arrayIndexPosition = strrpos($key, "]."); + if ($arrayIndexPosition) { + $key = substr($key, $arrayIndexPosition + 2); + } + $value = dot($values[$index]); } + } + $dot = dot($value); + $value = $dot->get($key); + if (!is_null($value)) { + return $value; + } } /** @@ -135,11 +135,11 @@ private function getDotValue($value, $key) { * @throws \Exception */ protected function getNumberFromString($key) { - $pattern = '/\[(\d+)]/'; - if (preg_match_all($pattern, $key, $matches)) { - return $matches[1][0]; - } - throw new \Exception('Number not found between' . $key ); + $pattern = '/\[(\d+)]/'; + if (preg_match_all($pattern, $key, $matches)) { + return $matches[1][0]; + } + throw new \Exception('Number not found between' . $key ); } /** From 75e1218431e893912137e2e9cfb9c089b7855a85 Mon Sep 17 00:00:00 2001 From: yashmitkharvi Date: Tue, 26 Sep 2023 14:10:28 +0530 Subject: [PATCH 08/11] Nested Loops. --- README.md | 34 ++++++++++++++++++++++++++++++++++ src/Context/GtmContext.php | 15 +++++++-------- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index a5d10b5..a248bfa 100644 --- a/README.md +++ b/README.md @@ -21,3 +21,37 @@ Given google tag manager data layer setting :key should match :value ``` > Note: use `@javascript` to check using JS + +## Using dot notation to get nested GTM elements +For the nested GTM elements like +``` +{ +   "event": "productImpression", +   "timestamp": 1694581407435, +   "eventLabel2": "97 items", +   "ecommerce": { +      "currencyCode": "KWD", +      "impressions": [ +         { +            "name": "Knockout Front-Close Sports Bra", +            "id": "1119313554A2", +            "price": 23, +            "category": "Victorias Secret/Bras/Styles/Lightly Lined Bras", +            "variant": "", +            "product_style_code": "11193135", +            "dimension2": "configurable", +            "dimension3": "Regular Product", +            "dimension4": 4, +            "brand": "Victorias Secret", +            "list": "PLP|Victorias Secret|Apparel|All Apparel", +            "position": "1" +         }, +      ] +   }, +   "gtm.uniqueEventId": 127 +} +``` +To extract the GTM value nested in arrays i.e name under impressions. We can use dot notation +``` +ecommerce.impressions[0].name +``` \ No newline at end of file diff --git a/src/Context/GtmContext.php b/src/Context/GtmContext.php index 6b4af0a..1ab21a1 100644 --- a/src/Context/GtmContext.php +++ b/src/Context/GtmContext.php @@ -95,10 +95,11 @@ protected function getDataLayerValue($key) { } /** - * Convert arrays to dot notation + * Convert arrays to dot notation. + * * @param $value * @param $key - * @return mixed|void + * @return mixed|null */ private function getDotValue($value, $key) { $dot = dot($value); @@ -122,14 +123,12 @@ private function getDotValue($value, $key) { } } $dot = dot($value); - $value = $dot->get($key); - if (!is_null($value)) { - return $value; - } + return $dot->get($key); } /** - * Get number inside array index + * Get number inside array index. + * * @param $key * @return string * @throws \Exception @@ -139,7 +138,7 @@ protected function getNumberFromString($key) { if (preg_match_all($pattern, $key, $matches)) { return $matches[1][0]; } - throw new \Exception('Number not found between' . $key ); + throw new \Exception('Number not found in key ' . $key ); } /** From e92af918222ee34ef9ea60ab9099a4ee25009a1e Mon Sep 17 00:00:00 2001 From: yashmitkharvi Date: Tue, 26 Sep 2023 15:19:44 +0530 Subject: [PATCH 09/11] Nested Loops. --- README.md | 48 +++++++++++++++++++------------------- src/Context/GtmContext.php | 7 +++--- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index a248bfa..284c7dd 100644 --- a/README.md +++ b/README.md @@ -26,32 +26,32 @@ Given google tag manager data layer setting :key should match :value For the nested GTM elements like ``` { -   "event": "productImpression", -   "timestamp": 1694581407435, -   "eventLabel2": "97 items", -   "ecommerce": { -      "currencyCode": "KWD", -      "impressions": [ -         { -            "name": "Knockout Front-Close Sports Bra", -            "id": "1119313554A2", -            "price": 23, -            "category": "Victorias Secret/Bras/Styles/Lightly Lined Bras", -            "variant": "", -            "product_style_code": "11193135", -            "dimension2": "configurable", -            "dimension3": "Regular Product", -            "dimension4": 4, -            "brand": "Victorias Secret", -            "list": "PLP|Victorias Secret|Apparel|All Apparel", -            "position": "1" -         }, -      ] -   }, -   "gtm.uniqueEventId": 127 + "event": "productImpression", + "timestamp": 1694581407435, + "eventLabel2": "97 items", + "ecommerce": { + "currencyCode": "KWD", + "impressions": [ + { + "name": "Knockout Front-Close Sports Bra", + "id": "1119313554A2", + "price": 23, + "category": "Victorias Secret/Bras/Styles/Lightly Lined Bras", + "variant": "", + "product_style_code": "11193135", + "dimension2": "configurable", + "dimension3": "Regular Product", + "dimension4": 4, + "brand": "Victorias Secret", + "list": "PLP|Victorias Secret|Apparel|All Apparel", + "position": "1" + }, + ] + }, + "gtm.uniqueEventId": 127 } ``` To extract the GTM value nested in arrays i.e name under impressions. We can use dot notation ``` -ecommerce.impressions[0].name +Given google tag manager data layer setting "ecommerce.impressions[0].name" should match "~Knockout~" ``` \ No newline at end of file diff --git a/src/Context/GtmContext.php b/src/Context/GtmContext.php index 1ab21a1..83d815a 100644 --- a/src/Context/GtmContext.php +++ b/src/Context/GtmContext.php @@ -78,7 +78,8 @@ public function getDataLayerSettingShouldMatch($key, $regex) { */ 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) { // Check if the key contains dot. if (strpos($key, '.', 0) !== false) { @@ -119,10 +120,10 @@ private function getDotValue($value, $key) { if ($arrayIndexPosition) { $key = substr($key, $arrayIndexPosition + 2); } - $value = dot($values[$index]); + $dot = dot($values[$index]); } } - $dot = dot($value); +// $dot = dot($value); return $dot->get($key); } From c9fc5d78de7ed0b53251ac86d5526be7e18ce8b7 Mon Sep 17 00:00:00 2001 From: yashmitkharvi Date: Tue, 26 Sep 2023 15:20:33 +0530 Subject: [PATCH 10/11] Nested Loops. --- src/Context/GtmContext.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Context/GtmContext.php b/src/Context/GtmContext.php index 83d815a..916ae16 100644 --- a/src/Context/GtmContext.php +++ b/src/Context/GtmContext.php @@ -123,7 +123,6 @@ private function getDotValue($value, $key) { $dot = dot($values[$index]); } } -// $dot = dot($value); return $dot->get($key); } From 209d977824a5cf91a56e753c54993f5de21cee4a Mon Sep 17 00:00:00 2001 From: Marcelo Vani Date: Thu, 12 Oct 2023 12:43:40 +0100 Subject: [PATCH 11/11] Added new configuration to allow ignoring GTM items. --- README.md | 16 ++++++++++++---- src/Context/GtmContext.php | 25 +++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 284c7dd..7400975 100644 --- a/README.md +++ b/README.md @@ -2,16 +2,24 @@ Provides step definitions to check GTM implementation on web pages -``` -DennisDigital\Behat\Gtm\Context\GtmContext -``` - ## Installation ``` composer require dennisdigital/behat-gtm ``` +### Configuration +Setup extension by specifying your behat.yml: + +Example on how to add extension and configure items to be ignored. +```yaml +default: + extensions: + DennisDigital\Behat\Gtm\Context\GtmContext: + ignoreGtmItems: + - gtm.element +``` + ## Step Definitions ```gherkin diff --git a/src/Context/GtmContext.php b/src/Context/GtmContext.php index 916ae16..39933c9 100644 --- a/src/Context/GtmContext.php +++ b/src/Context/GtmContext.php @@ -3,13 +3,20 @@ use Behat\Mink\Driver\Selenium2Driver; use Behat\MinkExtension\Context\RawMinkContext; -use Adbar\Dot; /** * Class GtmContext * @package DennisDigital\Behat\Gtm\Context */ class GtmContext extends RawMinkContext { + + private $parameters; + + public function __construct(array $parameters = []) + { + $this->parameters = $parameters; + } + /** * Check the google tag manager present in the page * @@ -146,7 +153,21 @@ protected function getNumberFromString($key) { */ protected function getDataLayerJson() { if ($this->getSession()->getDriver() instanceof Selenium2Driver) { - $json_arr = $this->getSession()->getDriver()->evaluateScript('return dataLayer;'); + $ignore_items = isset($this->parameters['ignoreGtmItems']) ? $this->parameters['ignoreGtmItems'] : []; + if (empty($ignore_items)) { + // Return dataLayer array as is. + $script = 'return dataLayer'; + } + else { + // Remove items to be ignored before returning the array. + $script = 'return dataLayer.map(i => { '; + foreach ($ignore_items as $i => $key) { + $script .= "delete(i['$key']);"; + } + $script .= ' return i; });'; + } + var_dump($script); + $json_arr = $this->getSession()->getDriver()->evaluateScript($script); } else { $json_arr = json_decode($this->getDataLayerJsonFromSource(), TRUE);