Google_Service_AnalyticsReporting_DimensionFilterClause中的多个过滤器

时间:2017-05-18 08:51:32

标签: php google-analytics google-analytics-api

此处:https://ga-dev-tools.appspot.com/query-explorer/

我可以使用多个过滤器,用' OR'和' AND。

示例:

 ga:eventAction==display;ga:eventCategory==ban,ga:eventAction==button;ga:eventCategory==ban

说:

 "display and ban" or "button and ban"

如何使用Google Analytics for Php重现这一点?

我试过这个

 $filter_clause = new Google_Service_AnalyticsReporting_DimensionFilterClause();

 $filter = new Google_Service_AnalyticsReporting_DimensionFilter();
 $filter->setDimensionName(array('ga:eventAction','ga:eventCategory'));
 $filter->setOperator('EXACT');
 $filter->setExpressions(array('display','ban' ));

 $filter_clause->setFilters( $filter );

 $filter1 = new Google_Service_AnalyticsReporting_DimensionFilter();
 $filter1->setDimensionName(array('ga:eventAction','ga:eventCategory'));
 $filter1->setOperator('EXACT');
 $filter1->setExpressions(array('button','ban'));

 $filter_clause->setFilters( $filter1 );

或者这个:

 $filter_clause->setFilters(array($filter, $filter1));

但没有任何作用。

1 个答案:

答案 0 :(得分:0)

对于我的过滤条件:ga:medium =〜organic,ga:sourcemedium =〜^ yandex.ru /引荐; ga:eventlabel =〜gotoreg 或ga:medium =〜organic; ga:eventlabel =〜gotoreg,ga:sourcemedium =〜^ yandex.ru /引荐; ga:eventlabel =〜gotoreg。

代码将如下所示:

/**
 * @return Google_Service_AnalyticsReporting_ReportRequest
 */
protected function createRequest(): Google_Service_AnalyticsReporting_ReportRequest
{
    parent::createRequest();
    $eventLabelDimensionFilterClause = $this->setEventLabelDimensionFilterClause();
    $sourceMediumDimensionFilterClause = $this->setSourceMediumDimensionFilterClause();
    $this->request->setDimensionFilterClauses([$eventLabelDimensionFilterClause,$sourceMediumDimensionFilterClause,]);

    return $this->request;
}

/**
 * @return array
 */
protected function getDimensions(): array
{
    $hostname = $this->setHostnameDimension();
    $source = $this->setSourceDimension();
    $medium = $this->setMediumDimension();
    $landingPagePath = $this->setLandingPagePathDimension();
    $date = $this->setDateDimension();

    return [$hostname, $source, $medium, $landingPagePath, $date,];
}

/**
 * @return Google_Service_AnalyticsReporting_Dimension
 */
protected function setLandingPagePathDimension(): Google_Service_AnalyticsReporting_Dimension
{
    //Create the landingPagePath dimension.
    $landingPagePath = new Google_Service_AnalyticsReporting_Dimension();
    $landingPagePath->setName("ga:landingPagePath");

    return $landingPagePath;
}

/**
 * @return Google_Service_AnalyticsReporting_DimensionFilterClause
 */
protected function setSourceMediumDimensionFilterClause(): Google_Service_AnalyticsReporting_DimensionFilterClause
{
    $mediumDimensionFilter = $this->setMediumDimensionFilter();
    $sourcemediumDimensionFilter = $this->setSourcemediumDimensionFilter();
    $dimensionFilterClause = new Google_Service_AnalyticsReporting_DimensionFilterClause();
    $dimensionFilterClause->setFilters([$sourcemediumDimensionFilter, $mediumDimensionFilter]);
    $dimensionFilterClause->setOperator('OR');

    return $dimensionFilterClause;
}

/**
 * @return Google_Service_AnalyticsReporting_DimensionFilterClause
 */
protected function setEventLabelDimensionFilterClause(): Google_Service_AnalyticsReporting_DimensionFilterClause
{
    $eventLabelDimensionFilter = $this->setEventLabelDimensionFilter();
    $dimensionFilterClause = new Google_Service_AnalyticsReporting_DimensionFilterClause();
    $dimensionFilterClause->setFilters([$eventLabelDimensionFilter]);
    $dimensionFilterClause->setOperator('AND');

    return $dimensionFilterClause;
}

/**
 * @return Google_Service_AnalyticsReporting_SegmentDimensionFilter
 */
protected function setMediumDimensionFilter(): Google_Service_AnalyticsReporting_SegmentDimensionFilter
{
    // Create Dimension Filter.
    $dimensionFilter = new Google_Service_AnalyticsReporting_SegmentDimensionFilter();
    $dimensionFilter->setDimensionName("ga:medium");
    $dimensionFilter->setOperator("REGEXP");
    $dimensionFilter->setExpressions("organic");

    return $dimensionFilter;
}

/**
 * @return Google_Service_AnalyticsReporting_SegmentDimensionFilter
 */
protected function setSourcemediumDimensionFilter(): Google_Service_AnalyticsReporting_SegmentDimensionFilter
{
    // Create Dimension Filter.
    $dimensionFilter = new Google_Service_AnalyticsReporting_SegmentDimensionFilter();
    $dimensionFilter->setDimensionName("ga:sourcemedium");
    $dimensionFilter->setOperator("REGEXP");
    $dimensionFilter->setExpressions("^yandex.ru / referral");

    return $dimensionFilter;
}

/**
 * @return Google_Service_AnalyticsReporting_SegmentDimensionFilter
 */
protected function setEventLabelDimensionFilter(): Google_Service_AnalyticsReporting_SegmentDimensionFilter
{
    // Create Dimension Filter.
    $dimensionFilter = new Google_Service_AnalyticsReporting_SegmentDimensionFilter();
    $dimensionFilter->setDimensionName("ga:eventlabel");
    $dimensionFilter->setOperator("REGEXP");
    $dimensionFilter->setExpressions(["GoToReg"]);
    $dimensionFilter->setCaseSensitive(false);

    return $dimensionFilter;
}

您的过滤器代码将与此类似。