Skip to content

Instantly share code, notes, and snippets.

@jhedstrom
Created October 17, 2013 00:47
Show Gist options
  • Save jhedstrom/7017508 to your computer and use it in GitHub Desktop.
Save jhedstrom/7017508 to your computer and use it in GitHub Desktop.
Test for custom Google Analytics variables via Behat and Mink.
<?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