Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add sampling level parameter to query #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
40 changes: 39 additions & 1 deletion src/Widop/GoogleAnalytics/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ class Query

/** @var string */
protected $callback;

/** @var string */
protected $samplingLevel;

/**
* Creates a google analytics query.
Expand Down Expand Up @@ -219,6 +222,38 @@ public function addMetric($metric)
return $this;
}

/**
* Use this parameter to set the sampling level (i.e. the number
* of sessions used to calculate the result) for a reporting query.
* The allowed values are consistent with the web interface and include:
* DEFAULT - Returns response with a sample size that balances speed and accuracy.
* FASTER - Returns a fast response with a smaller sample size.
* HIGHER_PRECISION - Returns a more accurate response using a large sample size,
* but this may result in the response being slower.
*
* If not supplied, the DEFAULT sampling level will be used.
*
* @param string $samplingLevel Sets the number of sessions used
*
* @return \Widop\GoogleAnalytics\Query The query.
*/
public function setSamplingLevel($samplingLevel)
{

$this->samplingLevel = $samplingLevel;
return $this;
}

public function hasSamplingLevel()
{
return !empty($this->samplingLevel);
}

public function getSamplingLevel()
{
return $this->samplingLevel;
}

/**
* Checks if the google analytics query has dimensions.
*
Expand Down Expand Up @@ -553,11 +588,14 @@ public function build($accessToken)
if ($this->getPrettyPrint()) {
$query['prettyPrint'] = 'true';
}

if ($this->hasCallback()) {
$query['callback'] = $this->getCallback();
}

if ($this->hasSamplingLevel()) {
$query['samplingLevel'] = $this->getSamplingLevel();
}
return sprintf('%s?%s', self::URL, http_build_query($query));
}
}