File: accountFeed.cs

package info (click to toggle)
gdata-sharp 2.2.0.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 12,092 kB
  • sloc: cs: 67,781; xml: 38,234; python: 163; makefile: 149; sh: 27
file content (268 lines) | stat: -rw-r--r-- 8,917 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Copyright 2010 Google Inc. All Rights Reserved.

/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.Collections.Generic;
using System.Text;
using Google.Analytics;
using Google.GData.Analytics;
using Google.GData.Client;
using Google.GData.Extensions;

namespace SampleAnalyticsClient
{
  public class AccountFeedExample
  {

    private static String CLIENT_USERNAME = "INSERT_LOGIN_EMAIL_HERE";
    private static String CLIENT_PASS = "INSERT_PASSWORD_HERE";

    public AccountFeed accountFeed;

    public static void Main(String[] args)
    {

      AccountFeedExample example;

      try
      {
        example = new AccountFeedExample();
      }
      catch (AuthenticationException e)
      {
        Console.Error.WriteLine("Authentication failed : " + e.Message);
        return;
      }
      catch (Exception e)
      {
        Console.Error.WriteLine("Authentication Failed : " + e.Message);
        return;
      }


      example.printFeedDetails();
      example.printAdvancedSegments();
      example.printCustomVarForOneEntry();
      example.printGoalsForOneEntry();
      example.printAccountEntries();
    }

    /**
     * Creates a new service object, attempts to authorize using the Client Login
     * authorization mechanism and requests data from the Google Analytics API.
     * @throws AuthenticationException if an error occurs with authorizing with
     *     Google Accounts.
     * @throws IOException if a network error occurs.
     * @throws ServiceException if an error occurs with the Google Analytics API.
     */
    public AccountFeedExample()
    {

      // Configure GA API.
      AnalyticsService asv = new AnalyticsService("gaExportAPI_acctSample_v2.0");

      // Client Login Authorization.
      asv.setUserCredentials(CLIENT_USERNAME, CLIENT_PASS);

      // GA Account Feed query uri.

      AccountQuery query = new AccountQuery();

      // Send our request to the Analytics API and wait for the results to
      // come back.
      accountFeed = asv.Query(query);
    }

    /**
     * Prints the important Google Analytics related data in the Account Feed.
     */
    public void printFeedDetails()
    {
      Console.WriteLine("\n-------- Important Feed Data --------");
      Console.WriteLine(
          "\nFeed Title     = " + accountFeed.Title +
          "\nTotal Results  = " + accountFeed.TotalResults +
          "\nStart Index    = " + accountFeed.StartIndex +
          "\nItems Per Page = " + accountFeed.ItemsPerPage +
          "\nFeed ID        = " + accountFeed.Id);
    }

    /**
     * Prints the advanced segments for this user.
     */
    public void printAdvancedSegments()
    {
      Console.WriteLine("\n-------- Advanced Segments --------");
      if (accountFeed.Segments.Count == 0)
      {
        Console.WriteLine("No advanced segments found");
      }
      else
      {
        foreach (Segment segment in accountFeed.Segments)
        {
          Console.WriteLine(
            "\nSegment Name       = " + segment.Name +
            "\nSegment ID         = " + segment.Id +
            "\nSegment Definition = " + segment.Definition.Value);
        }
      }
    }

    /**
     * Prints custom variable information for the first profile that has custom
     * variables configured.
     */
    public void printCustomVarForOneEntry()
    {
      Console.WriteLine("\n-------- Custom Variables --------");
      if (accountFeed.Entries.Count == 0)
      {
        Console.WriteLine("No entries found.");
      }
      else
      {
        // Go through each entry to see if any has a Custom Variable defined.
        foreach (AccountEntry entry in accountFeed.Entries)
        {
          if (entry.CustomVariables.Count > 0)
          {
            foreach (CustomVariable customVariable in entry.CustomVariables)
            {
              Console.WriteLine(
                  "\nCustom Variable Index = " + customVariable.Index +
                  "\nCustom Variable Name  = " + customVariable.Name +
                  "\nCustom Variable Scope = " + customVariable.Scope);
            }
            return;
          }
        }
        Console.WriteLine("\nNo custom variables defined for this user");
      }
    }

    /**
     * Prints all the goal information for one profile.
     */
    public void printGoalsForOneEntry()
    {
      Console.WriteLine("\n-------- Goal Configuration --------");
      if (accountFeed.Entries.Count == 0)
      {
        Console.WriteLine("No entries found.");
      }
      else
      {
        // Go through each entry to see if any have Goal information.
        foreach (AccountEntry entry in accountFeed.Entries)
        {
          if (entry.Goals.Count > 0)
          {
            foreach (Goal goal in entry.Goals)
            {
              // Print common information for all Goals in this profile.
              Console.WriteLine("\n----- Goal -----");
              Console.WriteLine(
                  "\nGoal Number = " + goal.Number +
                  "\nGoal Name   = " + goal.Name +
                  "\nGoal Value  = " + goal.Value +
                  "\nGoal Active = " + goal.Active);
              if (goal.Destination != null)
              {
                printDestinationGoal(goal.Destination);
              }
              else if (goal.Engagement != null)
              {
                printEngagementGoal(goal.Engagement);
              }
            }
            return;
          }
        }
      }
    }

    /**
     * Prints the important information for destination goals including all the
     * configured steps if they exist.
     * @param destination the destination goal configuration.
     */
    public void printDestinationGoal(Destination destination)
    {
      Console.WriteLine("\n\t----- Destination Goal -----");
      Console.WriteLine(
          "\n\tExpression      = " + destination.Expression +
          "\n\tMatch Type      = " + destination.MatchType +
          "\n\tStep 1 Required = " + destination.Step1Required +
          "\n\tCase Sensitive  = " + destination.CaseSensitive);

      // Print goal steps.
      if (destination.Steps.Count > 0)
      {
        Console.WriteLine("\n\t----- Destination Goal Steps -----");
        foreach (Step step in destination.Steps)
        {
          Console.WriteLine(
              "\n\tStep Number = " + step.Number +
              "\n\tStep Name   = " + step.Name +
              "\n\tStep Path   = " + step.Path);
        }
      }
    }


    /**
     * Prints the important information for Engagement Goals.
     * @param engagement The engagement goal configuration.
     */
    public void printEngagementGoal(Engagement engagement)
    {
      Console.WriteLine("\n\t----- Engagement Goal -----");
      Console.WriteLine(
          "\n\tGoal Type       = " + engagement.Type +
          "\n\tGoal Comparison = " + engagement.Comparison +
          "\n\tGoal Threshold  = " + engagement.Threshold);
    }

    /**
     * Prints the important Google Analytics related data in each Account Entry.
     */
    public void printAccountEntries()
    {
      Console.WriteLine("\n-------- First 1000 Profiles In Account Feed --------");
      if (accountFeed.Entries.Count == 0)
      {
        Console.WriteLine("No entries found.");
      }
      else
      {
        foreach (AccountEntry entry in accountFeed.Entries)
        {
          Console.WriteLine(
            "\nWeb Property Id = " + entry.Properties[3].Value +
            "\nAccount Name    = " + entry.Properties[1].Value +
            "\nAccount ID      = " + entry.Properties[0].Value +
            "\nProfile Name    = " + entry.Title.Text +
            "\nProfile ID      = " + entry.Properties[2].Value +
            "\nTable Id        = " + entry.ProfileId.Value +
            "\nCurrency        = " + entry.Properties[4].Value +
            "\nTimeZone        = " + entry.Properties[5].Value +
            (entry.CustomVariables.Count > 0 ? "\nThis profile has custom variables" : "") +
            (entry.Goals.Count > 0 ? "\nThis profile has goals" : ""));
        }
      }
    }
  }
}