PHP Class Piwik\Segment

A segment is a condition used to filter visits. They can, for example, select visits that have a specific browser or come from a specific country, or both. Individual segment dimensions (such as browserCode and countryCode) are defined by plugins. Read about the {@hook API.getSegmentDimensionMetadata} event to learn more. Plugins that aggregate data stored in Piwik can support segments by using this class when generating aggregation SQL queries. ### Examples **Basic usage** $idSites = array(1,2,3); $segmentStr = "browserCode==ff;countryCode==CA"; $segment = new Segment($segmentStr, $idSites); $query = $segment->getSelectQuery( $select = "table.col1, table2.col2", $from = array("table", "table2"), $where = "table.col3 = ?", $bind = array(5), $orderBy = "table.col1 DESC", $groupBy = "table2.col2" ); Db::fetchAll($query['sql'], $query['bind']); **Creating a _null_ segment** $idSites = array(1,2,3); $segment = new Segment('', $idSites); $segment->getSelectQuery will return a query that selects all visits
Show file Open project: piwik/piwik Class Usage Examples

Protected Properties

Property Type Description
$availableSegments
$idSites array
$segmentExpression Piwik\Segment\SegmentExpression
$string string

Public Methods

Method Description
__construct ( string $segmentCondition, array $idSites ) Constructor.
__toString ( ) : string Returns the segment string.
getHash ( ) : string Returns a hash of the segment condition, or the empty string if the segment condition is empty.
getSelectQuery ( string $select, array $from, false | string $where = false, array | string $bind = [], false | string $orderBy = false, false | string $groupBy = false, integer $limit, integer $offset ) : string Extend an SQL query that aggregates data over one of the 'log_' tables with segment expressions.
getString ( ) : string Returns the segment condition.
isEmpty ( ) Returns true if the segment is empty, false if otherwise.
willBeArchived ( ) : boolean Detects whether the Piwik instance is configured to be able to archive this segment. It checks whether the segment will be either archived via browser or cli archiving. It does not check if the segment has been archived. If you want to know whether the segment has been archived, the actual report data needs to be requested.

Protected Methods

Method Description
getCleanedExpression ( $expression )
initializeSegment ( $string, $idSites )

Private Methods

Method Description
getAvailableSegments ( )
getExpressionsWithUnionsResolved ( $expressions )
getSegmentByName ( $name )

Method Details

__construct() public method

Constructor.
public __construct ( string $segmentCondition, array $idSites )
$segmentCondition string The segment condition, eg, `'browserCode=ff;countryCode=CA'`.
$idSites array The list of sites the segment will be used with. Some segments are dependent on the site, such as goal segments.

__toString() public method

Returns the segment string.
public __toString ( ) : string
return string

getCleanedExpression() protected method

protected getCleanedExpression ( $expression )

getHash() public method

Returns a hash of the segment condition, or the empty string if the segment condition is empty.
public getHash ( ) : string
return string

getSelectQuery() public method

Extend an SQL query that aggregates data over one of the 'log_' tables with segment expressions.
public getSelectQuery ( string $select, array $from, false | string $where = false, array | string $bind = [], false | string $orderBy = false, false | string $groupBy = false, integer $limit, integer $offset ) : string
$select string The select clause. Should NOT include the **SELECT** just the columns, eg, `'t1.col1 as col1, t2.col2 as col2'`.
$from array Array of table names (without prefix), eg, `array('log_visit', 'log_conversion')`.
$where false | string (optional) Where clause, eg, `'t1.col1 = ? AND t2.col2 = ?'`.
$bind array | string (optional) Bind parameters, eg, `array($col1Value, $col2Value)`.
$orderBy false | string (optional) Order by clause, eg, `"t1.col1 ASC"`.
$groupBy false | string (optional) Group by clause, eg, `"t2.col2"`.
$limit integer Limit number of result to $limit
$offset integer Specified the offset of the first row to return
return string The entire select query.

getString() public method

Returns the segment condition.
public getString ( ) : string
return string

initializeSegment() protected method

protected initializeSegment ( $string, $idSites )
$string
$idSites

isEmpty() public method

Returns true if the segment is empty, false if otherwise.
public isEmpty ( )

willBeArchived() public method

This method does not take any date/period into consideration. Meaning a Piwik instance might be able to archive this segment in general, but not for a certain period if eg the archiving of range dates is disabled.
public willBeArchived ( ) : boolean
return boolean

Property Details

$availableSegments protected property

protected $availableSegments

$idSites protected property

protected array $idSites
return array

$segmentExpression protected property

protected SegmentExpression,Piwik\Segment $segmentExpression
return Piwik\Segment\SegmentExpression

$string protected property

protected string $string
return string