Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
110 changes: 101 additions & 9 deletions src/Context/GtmContext.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
<?php

namespace DennisDigital\Behat\Gtm\Context;

use Behat\Gherkin\Node\TableNode;
use Behat\Mink\Driver\Selenium2Driver;
use Behat\MinkExtension\Context\RawMinkContext;

/**
* Class GtmContext
* Class GtmContext.
*
* @package DennisDigital\Behat\Gtm\Context
*/
class GtmContext extends RawMinkContext {

/**
* Check the google tag manager present in the page
* Check the google tag manager present in the page.
*
* @Given google tag manager id is :arg1
*/
Expand All @@ -24,7 +28,7 @@ public function tagManagerIdIs($id) {
}

/**
* Check google tag manager data layer contain key value pair
* Check google tag manager data layer contain key value pair.
*
* @Given google tag manager data layer setting :arg1 should be :arg2
*/
Expand All @@ -36,7 +40,7 @@ public function dataLayerSettingShouldBe($key, $value) {
}

/**
* Check google tag manager data layer contain key value pair
* Check google tag manager data layer contain key value pair.
*
* @Given google tag manager data layer setting :arg1 should match :arg2
*/
Expand All @@ -48,16 +52,103 @@ public function getDataLayerSettingShouldMatch($key, $regex) {
}

/**
* Get Google Tag Manager Data Layer value
* Check google tag manager data layer contain event key value.
*
* @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 | 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);
}
}

/**
* Check datalayer property value.
*
* @param array $event_array
* Event array.
* @param string $property
* Property datalayer.
* @param string $expected_value
* Value datalayer.
*
* @throws \Exception
*/
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'];

$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 (!$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.
*
* @param string $event
* Event.
*
* @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.
*
* @param $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];
Expand Down Expand Up @@ -92,8 +183,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*(.*?);</script>~' , $html, $match);
// Get the dataLayer json and json_decode it.
preg_match('~dataLayer\s*=\s*(.*?);</script>~', $html, $match);
if (!isset($match[0])) {
throw new \Exception('dataLayer variable not found in source.');
}
Expand All @@ -113,4 +204,5 @@ protected function getDataLayerJsonFromJS() {

return $json_arr;
}

}