Google Analytics Api:如何在代码中使用自定义变量?

时间:2016-06-02 16:06:58

标签: google-analytics google-analytics-api google-analytics-v4

尝试模拟我在Google Analytics信息中心上执行此操作的方式。enter image description here

如何使用Custom Variable Value创建细分?我曾尝试编写以下代码,但它不会返回任何结果。
可以Custom Variable Value设置为DimensionFilter吗?

// Create Dimension Filter.
          SegmentDimensionFilter dimensionFilter = new SegmentDimensionFilter()
              .setDimensionName("ga:customVarValue1")
              .setOperator("EXACT")
              .setExpressions(Arrays.asList("account_id"));


          // Create Segment Filter Clause.
          SegmentFilterClause segmentFilterClause = new SegmentFilterClause()
              .setDimensionFilter(dimensionFilter);

          // Create the Or Filters for Segment.
          OrFiltersForSegment orFiltersForSegment = new OrFiltersForSegment()
              .setSegmentFilterClauses(Arrays.asList(segmentFilterClause));

          // Create the Simple Segment.
          SimpleSegment simpleSegment = new SimpleSegment()
              .setOrFiltersForSegment(Arrays.asList(orFiltersForSegment));

          // Create the Segment Filters.
          SegmentFilter segmentFilter = new SegmentFilter()
              .setSimpleSegment(simpleSegment);

          // Create the Segment Definition.
          SegmentDefinition segmentDefinition = new SegmentDefinition()
              .setSegmentFilters(Arrays.asList(segmentFilter));

          // Create the Dynamic Segment.
          DynamicSegment dynamicSegment = new DynamicSegment()
              .setSessionSegment(segmentDefinition)
              .setName("Sessions with Safari browser");

          // Create the Segments object.
          Segment segment = new Segment()
              .setDynamicSegment(dynamicSegment);

          // Create the ReportRequest object.
          ReportRequest request = new ReportRequest()
              .setViewId(VIEW_ID)
              .setDateRanges(Arrays.asList(dateRange))
              .setDimensions(Arrays.asList(segmentDimensions))
              .setSegments(Arrays.asList(segment))
              .setMetrics(Arrays.asList(sessions));

1 个答案:

答案 0 :(得分:1)

从长远来看,有一些事情可以帮助你。

  1. 远离自定义变量,因为custom dimensions替换为Universal Analytics
  2. 使用API explorer构建和共享调试请求。
  3. 最初在没有过滤器或细分的情况下进行查询,以确保您要查找的视图中存在您要查找的数据。
  4. 据说用自定义变量创建一个段请求:

    Java示例

      // Create the DateRange object.
      DateRange dateRange = new DateRange();
      dateRange.setStartDate("2015-06-15");
      dateRange.setEndDate("2015-06-30");
    
      // Create the Metrics object.
      Metric sessions = new Metric()
          .setExpression("ga:sessions")
          .setAlias("sessions");
    
      //Create the custom var  dimension.
      Dimension customVarValue1 = new Dimension()
          .setName("ga:customVarValue1");
    
      // Create the segment dimension.
      Dimension segmentDimensions = new Dimension()
          .setName("ga:customVarValue1");
    
      // Create Dimension Filter.
      SegmentDimensionFilter dimensionFilter = new SegmentDimensionFilter()
          .setDimensionName("ga:customVarValue1")
          .setOperator("PARTIAL")
          .setExpressions(Arrays.asList("account_id"));
    
      // Create Segment Filter Clause.
      SegmentFilterClause segmentFilterClause = new SegmentFilterClause()
          .setDimensionFilter(dimensionFilter);
    
      // Create the Or Filters for Segment.
      OrFiltersForSegment orFiltersForSegment = new OrFiltersForSegment()
          .setSegmentFilterClauses(Arrays.asList(segmentFilterClause));
    
      // Create the Simple Segment.
      SimpleSegment simpleSegment = new SimpleSegment()
          .setOrFiltersForSegment(Arrays.asList(orFiltersForSegment));
    
      // Create the Segment Filters.
      SegmentFilter segmentFilter = new SegmentFilter()
          .setSimpleSegment(simpleSegment);
    
      // Create the Segment Definition.
      SegmentDefinition segmentDefinition = new SegmentDefinition()
          .setSegmentFilters(Arrays.asList(segmentFilter));
    
      // Create the Dynamic Segment.
      DynamicSegment dynamicSegment = new DynamicSegment()
          .setSessionSegment(segmentDefinition)
          .setName("Sessions with Safari browser");
    
      // Create the Segments object.
      Segment segment = new Segment()
          .setDynamicSegment(dynamicSegment);
    
      // Create the ReportRequest object.
      ReportRequest request = new ReportRequest()
          .setViewId("XXXX")
          .setDateRanges(Arrays.asList(dateRange))
          .setDimensions(Arrays.asList(customVarValue1, segmentDimensions))
          .setSegments(Arrays.asList(segment))
          .setMetrics(Arrays.asList(sessions));
    
      // Create the GetReportsRequest object.
      GetReportsRequest getReport = new GetReportsRequest()
          .setReportRequests(Arrays.asList(request));
    
      // Call the batchGet method.
      GetReportsResponse response = analyticsreporting.reports().batchGet(getReport)
          .execute();
    

    要检查的事情。:

    1. 确保您使用的是正确的自定义变量。您的意思是搜索ga:customVarName1吗?
    2. 查询没有段或过滤器的变量,以确保在过滤/分段之前存在数据。
    3. 确保您使用的是operator
    4. 最后,如果您在UI中创建细分,则始终可以按API中的细分ID进行查询:

      按段ID的Java请求。

        // Create the Segments object for returning users.
        Segment segment = new Segment()
            .setSegmentId("gaid::-3");
      
        // Create the ReportRequest object.
        ReportRequest request = new ReportRequest()
            .setViewId("XXXX")
            .setDateRanges(Arrays.asList(dateRange))
            .setDimensions(Arrays.asList(browser, segmentDimensions))
            .setSegments(Arrays.asList(segment))
            .setMetrics(Arrays.asList(sessions));
      
        // Create the GetReportsRequest object.
        GetReportsRequest getReport = new GetReportsRequest()
            .setReportRequests(Arrays.asList(request));
      

      要获取细分受众群ID,您可以在Google Analytics管理API中调用segments.list方法。