Skip to content

Commit

Permalink
Merge pull request #2 from dennisinteractive/1_selium_driver
Browse files Browse the repository at this point in the history
#1 support GTM implementations without noscript fallback
  • Loading branch information
davidgrayston authored Jul 16, 2018
2 parents dabc0d7 + b19e592 commit 63478c4
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 104 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# Behat GTM Context
Provides steps to check GTM implementation
# GTM Behat Context

Provides step definitions to check GTM implementation on web pages

```
DennisDigital\Behat\Gtm\Context\GtmContext
```

## Installation

```
Expand All @@ -12,8 +14,10 @@ composer require dennisdigital/behat-gtm

## Step Definitions

```
```gherkin
Given google tag manager id is :id
Given google tag manager data layer setting :key should be :value
Given google tag manager data layer setting :key should match :value
```

> Note: use `@javascript` to check using JS
170 changes: 69 additions & 101 deletions src/Context/GtmContext.php
Original file line number Diff line number Diff line change
@@ -1,148 +1,116 @@
<?php
namespace DennisDigital\Behat\Gtm\Context;

use Behat\Behat\Context\Context;

use Behat\Mink\Mink;
use Behat\Mink\WebAssert;
use Behat\Mink\Session;
use Behat\MinkExtension\Context\MinkAwareContext;

use Behat\Mink\Driver\Selenium2Driver;
use Behat\MinkExtension\Context\RawMinkContext;

/**
* Class GtmContext
* @package DennisDigital\Behat\Gtm\Context
*/
class GtmContext implements MinkAwareContext {

class GtmContext extends RawMinkContext {
/**
* @var Mink
*/
private $mink;
private $minkParameters;


/**
* Sets Mink instance.
*
* @param Mink $mink Mink session manager
*/
public function setMink(Mink $mink)
{
$this->mink = $mink;
}

/**
* Sets parameters provided for Mink.
* Check the google tag manager present in the page
*
* @param array $parameters
* @Given google tag manager id is :arg1
*/
public function setMinkParameters(array $parameters)
{
$this->minkParameters = $parameters;
public function tagManagerIdIs($id) {
if ($this->getSession()->getDriver() instanceof Selenium2Driver) {
$this->assertSession()->responseContains("www.googletagmanager.com/gtm.js?id=$id");
}
else {
$this->assertSession()->responseContains("www.googletagmanager.com/ns.html?id=$id");
}
}

/**
* Returns Mink instance.
* Check google tag manager data layer contain key value pair
*
* @return Mink
* @Given google tag manager data layer setting :arg1 should be :arg2
*/
public function getMink()
{
if (null === $this->mink) {
throw new \RuntimeException(
'Mink instance has not been set on Mink context class. ' .
'Have you enabled the Mink Extension?'
);
public function dataLayerSettingShouldBe($key, $value) {
$property_value = $this->getDataLayerValue($key);
if ($value != $property_value) {
throw new \Exception($value . ' is not the same as ' . $property_value);
}

return $this->mink;
}

/**
* Returns Mink session assertion tool.
*
* @param string|null $name name of the session OR active session will be used
* Check google tag manager data layer contain key value pair
*
* @return WebAssert
* @Given google tag manager data layer setting :arg1 should match :arg2
*/
public function assertSession($name = null)
{
return $this->getMink()->assertSession($name);
public function getDataLayerSettingShouldMatch($key, $regex) {
$property_value = $this->getDataLayerValue($key);
if (!preg_match($regex, $property_value)) {
throw new \Exception($property_value . ' does not match ' . $regex);
}
}

/**
* Returns Mink session.
*
* @param string|null $name name of the session OR active session will be used
* Get Google Tag Manager Data Layer value
*
* @return Session
* @param $key
* @return mixed
* @throws \Exception
*/
public function getSession($name = null)
{
return $this->getMink()->getSession($name);
}
protected function getDataLayerValue($key) {
$json_arr = $this->getDataLayerJson();

/**
* Check the google tag manager present in the page
*
* @Given google tag manager id is :arg1
*/
public function googleTagManagerIdIs($id)
{
$this->assertSession()->responseContains("www.googletagmanager.com/ns.html?id=$id");
// 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];
}
}
throw new \Exception($key . ' not found.');
}

/**
* Check google tag manager data layer contain key value pair
*
* @Given google tag manager data layer setting :arg1 should be :arg2
* Get dataLayer variable JSON.
*/
public function googleTagManagerDataLayerSettingShouldBe($key, $value) {
$propertyValue = $this->googleTagManagerGetDataLayerValue($key);
if ($value != $propertyValue) {
throw new \Exception($value . ' is not the same as ' . $propertyValue);
protected function getDataLayerJson() {
if ($this->getSession()->getDriver() instanceof Selenium2Driver) {
$json_arr = $this->getSession()->getDriver()->evaluateScript('return dataLayer;');
}
else {
$json_arr = json_decode($this->getDataLayerJsonFromSource(), TRUE);
}
}

/**
* Check google tag manager data layer contain key value pair
*
* @Given google tag manager data layer setting :arg1 should match :arg2
*/
public function googleTagManagerDataLayerSettingShouldMatch($key, $regex) {
$propertyValue = $this->googleTagManagerGetDataLayerValue($key);
if (!preg_match($regex, $propertyValue)) {
throw new \Exception($propertyValue . ' does not match ' . $regex);
// If it's not an array throw an exception.
if (!is_array($json_arr)) {
throw new \Exception('dataLayer variable is not an array.');
}

return $json_arr;
}

/**
* Get Google Tag Manager Data Layer value
*
* @param $key
* @return mixed
* @throws \Exception
* Get dataLayer variable JSON from raw source.
*/
protected function googleTagManagerGetDataLayerValue($key) {
// Get the html
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);
if (!isset($match[0])) {
throw new \Exception('dataLayer variable not found.');
throw new \Exception('dataLayer variable not found in source.');
}
$jsonArr = json_decode($match[1]);
// If it's not an array throw an exception
if (!is_array($jsonArr)) {
throw new \Exception('dataLayer variable is not an array.');
}
// Loop through the array and return the data layer value
foreach ($jsonArr as $jsonObj) {
if (isset($jsonObj->{$key})) {
return $jsonObj->{$key};
}

return $match[1];
}

/**
* Get dataLayer variable JSON from raw source.
*/
protected function getDataLayerJsonFromJS() {
$json_arr = $this->getSession()->getDriver()->evaluateScript('return dataLayer;');

if (empty($json_arr)) {
throw new \Exception('dataLayer variable not set on page.');
}
throw new \Exception($key . ' not found.');

return $json_arr;
}
}

0 comments on commit 63478c4

Please sign in to comment.