Example 4: Retrieving data for specified dimensions and metrics
(Return to main article: Samples for Java (SOAP) v1)
1. Define a service connection for the ReportService:
ReportserviceStub reportStub = new ReportserviceStub();
2. Define a request for the getData() method:
ReportserviceStub.GetData dataReq = new ReportserviceStub.GetData();
3. Set up the login/password/profile id request parameters:
dataReq.setLogin("URCHIN_LOGIN"); dataReq.setPassword("URCHIN_PASSWORD"); dataReq.setProfileId(PROFILE_ID);
4. Set up the request parameters for the following scenarios:
- Retrieve a list of visitors and associated metrics for a specific date range:
dataReq.setStartDate(new SimpleDateFormat("yyyy-MM-dd").parse("2008-01-01")); dataReq.setEndDate(new SimpleDateFormat("yyyy-MM-dd").parse("2009-01-01")); dataReq.setDimensions("u:visitor_id"); dataReq.setMetrics("u:pages,u:visits"); dataReq.setMaxResults(5);
- Retrieve data for a specific date range and apply filter
// Define parameters. dataReq.setStartDate(new SimpleDateFormat("yyyy-MM-dd").parse("2008-01-01")); dataReq.setEndDate(new SimpleDateFormat("yyyy-MM-dd").parse("2009-01-01")); dataReq.setDimensions("u:utm_source,u:utm_medium,u:utm_campaign"); dataReq.setMetrics("u:pages,u:visits,u:transactions"); dataReq.setFilters("u:utm_source=~direct,u:pages>0"); dataReq.setMaxResults(5);
- Retrieve visitor list
dataReq.setStartDate(new SimpleDateFormat("yyyy-MM-dd").parse("2008-01-01")); dataReq.setEndDate(new SimpleDateFormat("yyyy-MM-dd").parse("2009-01-01")); dataReq.setDimensions("u:visitor_id"); dataReq.setMaxResults(5);
- Retrieve transaction list sorted by id (descending order)
// Define parameters dataReq.setStartDate(new SimpleDateFormat("yyyy-MM-dd").parse("2008-01-01")); dataReq.setEndDate(new SimpleDateFormat("yyyy-MM-dd").parse("2009-01-01")); dataReq.setDimensions("u:transaction_id"); dataReq.setMetrics("u:transactions,u:revenue"); dataReq.setSort("-u:transactions"); dataReq.setMaxResults(5);
- Retrieve totals aggregated by month for specific date range sorted by one metric
dataReq.setStartDate(new SimpleDateFormat("yyyy-MM-dd").parse("2008-01-01")); dataReq.setEndDate(new SimpleDateFormat("yyyy-MM-dd").parse("2009-01-01")); dataReq.setDimensions("u:transaction_id"); dataReq.setMetrics("u:transactions,u:revenue"); dataReq.setSort("-u:transactions"); dataReq.setMaxResults(5);
5. Retrieve the data:
ReportserviceStub.GetDataResponse dataRsp = reportStub.getData(dataReq);
6. Parse and display the data.
For complete sample code, see the implementations of GetDataWithImplicitStorage(), GetDataFiltered(), GetDataLimited(), GetDataSorted(), and GetDataAggregated() in the AdminService.java file.
(Return to main article: Samples for Java (SOAP) v1)