-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from dennisinteractive/1_selium_driver
#1 support GTM implementations without noscript fallback
- Loading branch information
Showing
2 changed files
with
76 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |