예제 #1
0
        public object GetAccounts(string userId)
        {
            AnalyticsConfig cfg = AnalyticsConfig.Current;

            AnalyticsConfigUser user = cfg.GetUserById(userId);

            if (user == null)
            {
                return(Request.CreateResponse(JsonMetaResponse.GetError(HttpStatusCode.NotFound, "User not found.")));
            }

            GoogleService google = GoogleService.CreateFromRefreshToken(
                user.Client.ClientId,
                user.Client.ClientSecret,
                user.RefreshToken
                );

            var response1 = google.Analytics.Management.GetAccounts(new AnalyticsGetAccountsOptions(1000));
            var response2 = google.Analytics.Management.GetWebProperties(new AnalyticsGetWebPropertiesOptions(1000));
            var response3 = google.Analytics.Management.GetProfiles(new AnalyticsGetProfilesOptions(1000));

            var accounts      = response1.Body.Items;
            var webProperties = response2.Body.Items;
            var profiles      = response3.Body.Items;

            var body = new Models.Api.Selector.UserModel(user, accounts, webProperties, profiles);

            return(body);
        }
예제 #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)
            });
        }