Google Analytics Api: How to use Custom Variables in the code?

大兔子大兔子 提交于 2019-12-11 05:29:27

问题


Trying to emulate how I do it on the Google Analytics Dashboard.

How a Segment can be created with a Custom Variable Value? I have tried writing following code but it does not return any result back.
Can Custom Variable Value set as a 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:


A few things to help you in the long run.

  1. Move away from custom variables as they were replaced with custom dimensions for Universal Analytics.
  2. Use the API explorer to build and share your request for debugging.
  3. Initially query without a filter or a segment to ensure the data you are looking for is present in the View you are querying.

With that said to create a request with a segment on a custom variable:

Java Example

  // 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();

Things to check.:

  1. Ensure that you are using the correct custom variable. Did you mean to search for ga:customVarName1?
  2. Query for the variable without the segment or filter to ensure the data is present before filtering/segmenting.
  3. Ensure that you are using the right operator.

Finally if you create a segment in the UI you can always query by segment ID in the API:

Java request by segment ID.

  // 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));

To get the segment id you can call the segments.list method in the Analytics Management API.



来源:https://stackoverflow.com/questions/37596951/google-analytics-api-how-to-use-custom-variables-in-the-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!