Trials and Tribulations in the Wild World of Web Analytics

You are in a helicopter.

The application below takes my eighteen hours (approximately) of Microsoft C# experience and builds upon Sean Gubler’s work posted on the Omniture Developer Connection Blog. In lieu of optimal code, I have gone for a verbose example that tries to show the what, why and how.

If you:

  • Follow Sean’s instructions
  • Replace his code example for mine
  • You should have a fully functional application which, for your top 5 site sections, displays:

  • Page Views
  • Visits
  • Visitors

This application was built as a proof of concept, before attempting a SSIS Package which places site section traffic in a SQL Server Database. A few minor changes are required to the code below, but it works quite well as a Script Component.

The DLLs Sean provides are compiled using .net 4, which does not play nice when building a SSIS Package using SQL Server 2008 R2 Business Intelligence Development Studio . To overcome this issue, I compiled the DLLs using .net 3.5 and have yet to experience any problems. If you go this route, you may hit a few snags with the DLLs being unsigned or not registered. If you do, drop me a comment and I will relay how I addressed.

Finally, onto the application.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Channels;
using Microsoft.ServiceModel.Samples.CustomToken;
using Adobe.OmnitureAPI;

namespace ConsoleAPIExample {
    class Program {
        static void Main(string[] args) {

            //API Parameters
            string strAPIUsername = "[API Username]";
            string strAPIKey = "[API Key]";
            string strAPIUrl = "https://api.omniture.com/admin/1.2/";

            //Report Parameters
            string strDateFrom = "[Date From]";
            string strDateTo = "[Date To]";
            string strRSID = "[RSID]";
            string strMetricId1 = "pageViews";
            string strMetricId2 = "visits";
            string strMetricId3 = "visitorsMonthly";
            string strElementId = "siteSection";
            int intResultsToGet = 5; //kept this at 5 just for debugging purposes
            
            //Output Messages
            string strRequestOut = "";
            string strResponseOut = "";
            string strResultsOut = "";
            string strErrorMsg = "";
            int tokenCount;

            //Used to check status of report
            int intCheckReadyCounter = 0;
            bool blnReportDone = false;

            OmnitureWebServicePortTypeClient client = 
            OmnitureWebServicePortTypeClient.getClient(strAPIUsername, strAPIKey, strAPIUrl);

            //Contains information for creating a report
            reportDescription scReport = new reportDescription();

            //Requesting three metrics and each reportDefinitionMetric identifies one metric
            reportDefinitionMetric scMetric1 = new reportDefinitionMetric();
            reportDefinitionMetric scMetric2 = new reportDefinitionMetric();
            reportDefinitionMetric scMetric3 = new reportDefinitionMetric();

            //A list of reportDefinitionMetrics
            reportDefinitionMetric[] scMetricList = new reportDefinitionMetric[3] {
                scMetric1, scMetric2, scMetric3
            };

            //Using two parameters of one element
            reportDefinitionElement scElement1 = new reportDefinitionElement();
            

            //An array of reportDefinitionElements
            reportDefinitionElement[] scElementList = new reportDefinitionElement[1] {
                scElement1
            };

            //Variables to hold responses for requests
            reportQueueResponse scResponse = new reportQueueResponse(); //response to initial report request
            report_status scReportStatus = new report_status(); //response to status checks
            reportResponse scReportResponse = new reportResponse(); //actual report results

            //Setting the report metrics
            scMetric1.id = strMetricId1;
            scMetric2.id = strMetricId2;
            scMetric3.id = strMetricId3;

            //Setting the parameters of the report element
            scElement1.id = strElementId;
            scElement1.top = intResultsToGet;

            //Setting report description
            scReport.reportSuiteID = strRSID;
            scReport.dateFrom = strDateFrom;
            scReport.dateTo = strDateTo;
            scReport.metrics = scMetricList;
            scReport.elements = scElementList;

            strRequestOut = "Requesting traffic to the top " + intResultsToGet + " ";
            strRequestOut = strRequestOut + "site sections for " + scReport.dateFrom + " ";
            strRequestOut = strRequestOut + "to " + scReport.dateTo + "\r\n\r\n\r\n";
            Console.Write(strRequestOut);

            //Queuing the report via the API
            scResponse = client.ReportQueueRanked(scReport);

            strResponseOut = "Report Request ID: " + scResponse.reportID.ToString() + "\r\n";
            strResponseOut = strResponseOut + "Report Request Status: " + scResponse.status.ToString() + "\r\n";
            strResponseOut = strResponseOut + "Report Request Status Msg: " + scResponse.statusMsg.ToString() + "\r\n\r\n";
            strResponseOut = strResponseOut + "Checking Every 10 Seconds for Report" + "\r\n\r\n\r\n";
            Console.Write(strResponseOut);

            //Timed delay used when checking report status
            while (intCheckReadyCounter < 6 && !blnReportDone) {
                System.Threading.Thread.Sleep(10000);

                //Checking report status via API
                scReportStatus = client.ReportGetStatus(scResponse.reportID);
                switch (scReportStatus.status.ToString()) {
                case "done":
                    blnReportDone = true;
                    break;
                case "ready":
                    intCheckReadyCounter++;
                    break;
                case "failed":
                default:
                    //Created string in this manner for readibility
                    strErrorMsg = "error returning results--";
                    strErrorMsg = strErrorMsg + "--rprtID--" + scResponse.reportID.ToString();
                    strErrorMsg = strErrorMsg + "--rqst status--" + scResponse.status.ToString();
                    strErrorMsg = strErrorMsg + "--status msg--" + scResponse.statusMsg.ToString();
                    strErrorMsg = strErrorMsg + "--rpt status--" + scReportStatus.status.ToString();
                    strErrorMsg = strErrorMsg + "--err code--" + scReportStatus.error_code.ToString();
                    strErrorMsg = strErrorMsg + "--err msg--" + scReportStatus.error_msg.ToString();
                    throw new InvalidOperationException(strErrorMsg);
                }

            }

            Console.Write("Fetching Results" + "\r\n\r\n\r\n");

            //Fetching the results via the API
            scReportResponse = client.ReportGetReport(scResponse.reportID);

            Console.Write("Section" + "  |  " + "PageViews" + "  |  " + "Visits" + "  |  " + "Visitors" + "\r\n");

            //Output results
            for (int i = 0; i < intResultsToGet; i++) {
                strResultsOut = scReportResponse.report.data[i].name.ToString() + "  |  ";
                strResultsOut = strResultsOut + scReportResponse.report.data[i].counts[0].ToString() + "  |  ";
                strResultsOut = strResultsOut + scReportResponse.report.data[i].counts[1].ToString() + "  |  ";
                strResultsOut = strResultsOut + scReportResponse.report.data[i].counts[2].ToString() + "\r\n";
                Console.Write(strResultsOut);
                strResultsOut = "";
            }

            //Thrown in to watch token usage
            tokenCount = client.CompanyGetTokenCount();

            Console.WriteLine("\r\n\r\n" + "Tokens remaining for the Month:  " + tokenCount + "\r\n\r\n");
            Console.Write("Please Close Window");

            //Pauses the App
            Console.ReadLine();
        }

    }
}
NO COMMENTS