/clients/cs/samples/Analytics_AccountFeed_Sample/accountFeed.cs

http://google-gdata.googlecode.com/ · C# · 268 lines · 188 code · 25 blank · 55 comment · 16 complexity · ed0300124c6a6fda456821e242702295 MD5 · raw file

  1. // Copyright 2010 Google Inc. All Rights Reserved.
  2. /* Licensed under the Apache License, Version 2.0 (the "License");
  3. * you may not use this file except in compliance with the License.
  4. * You may obtain a copy of the License at
  5. *
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. */
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Text;
  17. using Google.Analytics;
  18. using Google.GData.Analytics;
  19. using Google.GData.Client;
  20. using Google.GData.Extensions;
  21. namespace SampleAnalyticsClient
  22. {
  23. public class AccountFeedExample
  24. {
  25. private static String CLIENT_USERNAME = "INSERT_LOGIN_EMAIL_HERE";
  26. private static String CLIENT_PASS = "INSERT_PASSWORD_HERE";
  27. public AccountFeed accountFeed;
  28. public static void Main(String[] args)
  29. {
  30. AccountFeedExample example;
  31. try
  32. {
  33. example = new AccountFeedExample();
  34. }
  35. catch (AuthenticationException e)
  36. {
  37. Console.Error.WriteLine("Authentication failed : " + e.Message);
  38. return;
  39. }
  40. catch (Exception e)
  41. {
  42. Console.Error.WriteLine("Authentication Failed : " + e.Message);
  43. return;
  44. }
  45. example.printFeedDetails();
  46. example.printAdvancedSegments();
  47. example.printCustomVarForOneEntry();
  48. example.printGoalsForOneEntry();
  49. example.printAccountEntries();
  50. }
  51. /**
  52. * Creates a new service object, attempts to authorize using the Client Login
  53. * authorization mechanism and requests data from the Google Analytics API.
  54. * @throws AuthenticationException if an error occurs with authorizing with
  55. * Google Accounts.
  56. * @throws IOException if a network error occurs.
  57. * @throws ServiceException if an error occurs with the Google Analytics API.
  58. */
  59. public AccountFeedExample()
  60. {
  61. // Configure GA API.
  62. AnalyticsService asv = new AnalyticsService("gaExportAPI_acctSample_v2.0");
  63. // Client Login Authorization.
  64. asv.setUserCredentials(CLIENT_USERNAME, CLIENT_PASS);
  65. // GA Account Feed query uri.
  66. AccountQuery query = new AccountQuery();
  67. // Send our request to the Analytics API and wait for the results to
  68. // come back.
  69. accountFeed = asv.Query(query);
  70. }
  71. /**
  72. * Prints the important Google Analytics related data in the Account Feed.
  73. */
  74. public void printFeedDetails()
  75. {
  76. Console.WriteLine("\n-------- Important Feed Data --------");
  77. Console.WriteLine(
  78. "\nFeed Title = " + accountFeed.Title +
  79. "\nTotal Results = " + accountFeed.TotalResults +
  80. "\nStart Index = " + accountFeed.StartIndex +
  81. "\nItems Per Page = " + accountFeed.ItemsPerPage +
  82. "\nFeed ID = " + accountFeed.Id);
  83. }
  84. /**
  85. * Prints the advanced segments for this user.
  86. */
  87. public void printAdvancedSegments()
  88. {
  89. Console.WriteLine("\n-------- Advanced Segments --------");
  90. if (accountFeed.Segments.Count == 0)
  91. {
  92. Console.WriteLine("No advanced segments found");
  93. }
  94. else
  95. {
  96. foreach (Segment segment in accountFeed.Segments)
  97. {
  98. Console.WriteLine(
  99. "\nSegment Name = " + segment.Name +
  100. "\nSegment ID = " + segment.Id +
  101. "\nSegment Definition = " + segment.Definition.Value);
  102. }
  103. }
  104. }
  105. /**
  106. * Prints custom variable information for the first profile that has custom
  107. * variables configured.
  108. */
  109. public void printCustomVarForOneEntry()
  110. {
  111. Console.WriteLine("\n-------- Custom Variables --------");
  112. if (accountFeed.Entries.Count == 0)
  113. {
  114. Console.WriteLine("No entries found.");
  115. }
  116. else
  117. {
  118. // Go through each entry to see if any has a Custom Variable defined.
  119. foreach (AccountEntry entry in accountFeed.Entries)
  120. {
  121. if (entry.CustomVariables.Count > 0)
  122. {
  123. foreach (CustomVariable customVariable in entry.CustomVariables)
  124. {
  125. Console.WriteLine(
  126. "\nCustom Variable Index = " + customVariable.Index +
  127. "\nCustom Variable Name = " + customVariable.Name +
  128. "\nCustom Variable Scope = " + customVariable.Scope);
  129. }
  130. return;
  131. }
  132. }
  133. Console.WriteLine("\nNo custom variables defined for this user");
  134. }
  135. }
  136. /**
  137. * Prints all the goal information for one profile.
  138. */
  139. public void printGoalsForOneEntry()
  140. {
  141. Console.WriteLine("\n-------- Goal Configuration --------");
  142. if (accountFeed.Entries.Count == 0)
  143. {
  144. Console.WriteLine("No entries found.");
  145. }
  146. else
  147. {
  148. // Go through each entry to see if any have Goal information.
  149. foreach (AccountEntry entry in accountFeed.Entries)
  150. {
  151. if (entry.Goals.Count > 0)
  152. {
  153. foreach (Goal goal in entry.Goals)
  154. {
  155. // Print common information for all Goals in this profile.
  156. Console.WriteLine("\n----- Goal -----");
  157. Console.WriteLine(
  158. "\nGoal Number = " + goal.Number +
  159. "\nGoal Name = " + goal.Name +
  160. "\nGoal Value = " + goal.Value +
  161. "\nGoal Active = " + goal.Active);
  162. if (goal.Destination != null)
  163. {
  164. printDestinationGoal(goal.Destination);
  165. }
  166. else if (goal.Engagement != null)
  167. {
  168. printEngagementGoal(goal.Engagement);
  169. }
  170. }
  171. return;
  172. }
  173. }
  174. }
  175. }
  176. /**
  177. * Prints the important information for destination goals including all the
  178. * configured steps if they exist.
  179. * @param destination the destination goal configuration.
  180. */
  181. public void printDestinationGoal(Destination destination)
  182. {
  183. Console.WriteLine("\n\t----- Destination Goal -----");
  184. Console.WriteLine(
  185. "\n\tExpression = " + destination.Expression +
  186. "\n\tMatch Type = " + destination.MatchType +
  187. "\n\tStep 1 Required = " + destination.Step1Required +
  188. "\n\tCase Sensitive = " + destination.CaseSensitive);
  189. // Print goal steps.
  190. if (destination.Steps.Count > 0)
  191. {
  192. Console.WriteLine("\n\t----- Destination Goal Steps -----");
  193. foreach (Step step in destination.Steps)
  194. {
  195. Console.WriteLine(
  196. "\n\tStep Number = " + step.Number +
  197. "\n\tStep Name = " + step.Name +
  198. "\n\tStep Path = " + step.Path);
  199. }
  200. }
  201. }
  202. /**
  203. * Prints the important information for Engagement Goals.
  204. * @param engagement The engagement goal configuration.
  205. */
  206. public void printEngagementGoal(Engagement engagement)
  207. {
  208. Console.WriteLine("\n\t----- Engagement Goal -----");
  209. Console.WriteLine(
  210. "\n\tGoal Type = " + engagement.Type +
  211. "\n\tGoal Comparison = " + engagement.Comparison +
  212. "\n\tGoal Threshold = " + engagement.Threshold);
  213. }
  214. /**
  215. * Prints the important Google Analytics related data in each Account Entry.
  216. */
  217. public void printAccountEntries()
  218. {
  219. Console.WriteLine("\n-------- First 1000 Profiles In Account Feed --------");
  220. if (accountFeed.Entries.Count == 0)
  221. {
  222. Console.WriteLine("No entries found.");
  223. }
  224. else
  225. {
  226. foreach (AccountEntry entry in accountFeed.Entries)
  227. {
  228. Console.WriteLine(
  229. "\nWeb Property Id = " + entry.Properties[3].Value +
  230. "\nAccount Name = " + entry.Properties[1].Value +
  231. "\nAccount ID = " + entry.Properties[0].Value +
  232. "\nProfile Name = " + entry.Title.Text +
  233. "\nProfile ID = " + entry.Properties[2].Value +
  234. "\nTable Id = " + entry.ProfileId.Value +
  235. "\nCurrency = " + entry.Properties[4].Value +
  236. "\nTimeZone = " + entry.Properties[5].Value +
  237. (entry.CustomVariables.Count > 0 ? "\nThis profile has custom variables" : "") +
  238. (entry.Goals.Count > 0 ? "\nThis profile has goals" : ""));
  239. }
  240. }
  241. }
  242. }
  243. }