Created
October 17, 2013 00:47
-
-
Save jhedstrom/7017508 to your computer and use it in GitHub Desktop.
Test for custom Google Analytics variables via Behat and Mink.
This file contains hidden or 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
<?php | |
/** | |
* @Then /^the google "(?P<variable>[^"]*)" variable should be set to "(?P<value>[^"]*)"$/ | |
*/ | |
public function assertGoogleAnalyticsCustomVariable($variable, $value) { | |
$map = array( | |
// Custom variable name => Custom variable index. | |
'gender' => 1, | |
'age range' => 2, | |
'zipcode' => 3, | |
); | |
if (!in_array($variable, array_keys($map))) { | |
throw new \Exception(sprintf('No custom variable "%s" defined in %s.', $variable, __FUNCTION__)); | |
} | |
$index = $map[$variable]; | |
$script = <<<JS | |
var pageTracker = _gat._getTrackerByName(); | |
return pageTracker._getVisitorCustomVar($index); | |
JS; | |
$result = $this->getSession()->getDriver()->getWebDriverSession()->execute(array('script' => $script, 'args' => array())); | |
if ($result != $value) { | |
throw new \Exception(sprintf('The custom variable "%s" with value "%s" was not found on the page.', $variable, $value)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment