public object GetSiteData(int siteId, string period, bool cache = true) { DashboardHelpers.EnsureCurrentUserCulture(); try { // Get the site IDashboardSite site = DashboardContext.Current.GetSiteById(siteId); if (site == null) { throw new DashboardException(HttpStatusCode.NotFound, "Site ikke fundet", "Et site det angivne ID blev ikke fundet"); } // Get analytics information IAnalyticsSite analytics = site as IAnalyticsSite; if (analytics == null || !analytics.HasAnalytics) { throw new DashboardException(HttpStatusCode.InternalServerError, "Analytics", "Det valgte side understøtter eller er ikke konfigureret til visning af statistik fra Google Analytics"); } // Build the query DataQuery query = DataQuery.GetFromPeriod(analytics, period, cache); query.Type = DataQueryType.Site; // Generate the response object DataResponse res = query.GetResponse(); // Return a nice JSON response return(JsonMetaResponse.GetSuccess(res)); } catch (DashboardException ex) { return(JsonMetaResponse.GetError(HttpStatusCode.InternalServerError, ex.Title + ": " + ex.Message)); } catch (Exception ex) { return(JsonMetaResponse.GetError(HttpStatusCode.InternalServerError, "Oopsie (" + ex.GetType() + "): " + ex.Message, ex.StackTrace.Split('\n'))); } }
/// <summary> /// Initializes a new data query for the specified <code>period</code>. /// </summary> /// <param name="site">The site to get data for.</param> /// <param name="period">The period to get data for.</param> /// <param name="enableCaching">Specifies whether caching should be enabled.</param> public static DataQuery GetFromPeriod(IAnalyticsSite site, string period, bool enableCaching) { switch (period) { case "lastyear": return(new DataQuery(site, 365, enableCaching)); case "lastmonth": return(new DataQuery(site, 31, enableCaching)); case "lastweek": return(new DataQuery(site, 7, enableCaching)); default: return(new DataQuery(site, 1, enableCaching)); } }
public object GetPageData(int siteId, int pageId, string period, bool cache = true) { DashboardHelpers.EnsureCurrentUserCulture(); try { // Look for the site with the specified ID IDashboardSite site = DashboardContext.Current.GetSiteById(siteId); if (site == null) { throw new DashboardException(HttpStatusCode.NotFound, "Site ikke fundet", "Et site det angivne ID blev ikke fundet"); } // Attempt to cast the site to an Analytics site IAnalyticsSite analytics = site as IAnalyticsSite; if (analytics == null || !analytics.HasAnalytics) { throw new DashboardException(HttpStatusCode.InternalServerError, "Analytics", "Det valgte side understøtter eller er ikke konfigureret til visning af statistik fra Google Analytics"); } // Get the published content of the page IPublishedContent content = UmbracoContext.ContentCache.GetById(pageId); // Build the query DataQuery query = DataQuery.GetFromPeriod(analytics, period, cache); query.Type = DataQueryType.Page; query.PageId = content.Id; // Set the URL for the query. The protocol and domain is stripped so we only have the path query.PageUrl = Regex.Replace(content.Url, "^http(s|)://[a-z0-9-.]+/", "/"); // Google Analytics sees the same URL with and without a trailing slash as two different pages, so we should tell the query to check both string pageUrlTrimmed = query.PageUrl.TrimEnd('/'); string pageUrlSlashed = pageUrlTrimmed + '/'; query.PageUrls = String.IsNullOrEmpty(pageUrlTrimmed) ? new[] { pageUrlSlashed } : new[] { pageUrlTrimmed, pageUrlSlashed }; // Generate the response object DataResponse res = query.GetResponse(); // Return a nice JSON response return(JsonMetaResponse.GetSuccess(res)); } catch (DashboardException ex) { return(JsonMetaResponse.GetError(HttpStatusCode.InternalServerError, ex.Title + ": " + ex.Message)); } catch (Exception ex) { return(JsonMetaResponse.GetError(HttpStatusCode.InternalServerError, "Oopsie (" + ex.GetType() + "): " + ex.Message, ex.StackTrace.Split('\n'))); } }
public DataQuery(IAnalyticsSite site, int days, bool enableCaching) { Site = site; EnableCaching = enableCaching; Days = days; Service = GoogleService.CreateFromRefreshToken(site.Analytics.ClientId, site.Analytics.ClientSecret, site.Analytics.RefreshToken); CurrentStartDate = DateTime.Today.AddDays(-days); CurrentEndDate = DateTime.Today.AddSeconds(-1); TimeSpan diff = CurrentEndDate.AddSeconds(1).Subtract(CurrentStartDate); PreviousStartDate = CurrentStartDate.Subtract(diff); PreviousEndDate = CurrentEndDate.Subtract(diff); }
/// <summary> /// Initializes a new data query for the specified <code>period</code>. /// </summary> /// <param name="site">The site to get data for.</param> /// <param name="period">The period to get data for.</param> /// <param name="enableCaching">Specifies whether caching should be enabled.</param> public static DataQuery GetFromPeriod(IAnalyticsSite site, string period, bool enableCaching) { switch (period) { case "lastyear": return new DataQuery(site, 365, enableCaching); case "lastmonth": return new DataQuery(site, 31, enableCaching); case "lastweek": return new DataQuery(site, 7, enableCaching); default: return new DataQuery(site, 1, enableCaching); } }
/// <summary> /// Initializes a new data query for the specified amount of <code>days</code>. /// </summary> /// <param name="site">The site to get data for.</param> /// <param name="days">The amount of days defining the period to get data for.</param> /// <param name="enableCaching">Specifies whether caching should be enabled.</param> public static DataQuery GetFromDays(IAnalyticsSite site, int days, bool enableCaching) { return new DataQuery(site, days, enableCaching); }
/// <summary> /// Initializes a new data query for the specified amount of <code>days</code>. /// </summary> /// <param name="site">The site to get data for.</param> /// <param name="days">The amount of days defining the period to get data for.</param> /// <param name="enableCaching">Specifies whether caching should be enabled.</param> public static DataQuery GetFromDays(IAnalyticsSite site, int days, bool enableCaching) { return(new DataQuery(site, days, enableCaching)); }