Exemplo n.º 1
0
 public object ConvertIntermediateToObject(IPublishedElement owner, PublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview)
 {
     try {
         return(AnalyticsProfileSelection.Deserialize(inter + ""));
     } catch (Exception) {
         return(AnalyticsProfileSelection.Deserialize("{}"));
     }
 }
Exemplo n.º 2
0
        public object GetData(int pageId, string period = "yesterday")
        {
            IPublishedContent content = Umbraco.Content(pageId);

            if (content == null)
            {
                return(Request.CreateResponse(JsonMetaResponse.GetError("Page not found or not published.")));
            }

            IPublishedContent site = GetSiteNode(content);

            if (site == null)
            {
                return(Request.CreateResponse(JsonMetaResponse.GetError("Unable to determine site node.")));
            }

            AnalyticsProfileSelection selection = site.Value("analyticsProfile") as AnalyticsProfileSelection;

            // Get a reference to the configuration of this package
            AnalyticsConfig config = AnalyticsConfig.Current;

            string        profileId = null;
            GoogleService service   = null;

            if (selection != null && selection.IsValid)
            {
                profileId = selection.Profile.Id;

                AnalyticsConfigUser user = config.GetUserById(selection.User.Id);

                if (user != null)
                {
                    service = GoogleService.CreateFromRefreshToken(
                        user.Client.ClientId,
                        user.Client.ClientSecret,
                        user.RefreshToken
                        );
                }
            }

            // Fallback to app settings (if specified)
            if (service == null && config.HasAppSettings)
            {
                profileId = config.AppSettings.AnalyticsProfileId;

                service = GoogleService.CreateFromRefreshToken(
                    config.AppSettings.GoogleClientId,
                    config.AppSettings.GoogleClientSecret,
                    config.AppSettings.GoogleRefreshToken
                    );
            }

            if (String.IsNullOrWhiteSpace(profileId) || service == null)
            {
                return(Request.CreateResponse(JsonMetaResponse.GetError("The Analytics package is not configured.")));
            }



            AnalyticsDataMode mode = content.Id == site.Id ? AnalyticsDataMode.Site : AnalyticsDataMode.Page;

            Period p = Period.Parse(period);

            return(new {
                period = p,
                page = new {
                    id = content.Id,
                    name = content.Name,
                    url = content.Url
                },
                history = GetHistory(service.Analytics, profileId, content, mode, p)
            });
        }