Esempio n. 1
0
 public UserModel(AnalyticsConfigUser user)
 {
     Id     = user.Id;
     UserId = user.UserId;
     Email  = user.Email;
     Name   = user.Name;
 }
        public UserModel(AnalyticsConfigUser user, AnalyticsAccount[] accounts, AnalyticsWebProperty[] webProperties, AnalyticsProfile[] profiles)
        {
            Id = user.Id;

            Dictionary <string, AnalyticsProfile[]> profiles2 = profiles.GroupBy(x => x.WebPropertyId).ToDictionary(x => x.Key, x => x.ToArray());

            Dictionary <string, AccountModel> accounts2 = (
                from account in accounts
                select new AccountModel(account)
                ).ToDictionary(x => x.Id);

            foreach (AnalyticsWebProperty webProperty in webProperties)
            {
                // Attempt to get the parent account (if not found, skip th web property)
                if (accounts2.TryGetValue(webProperty.AccountId, out AccountModel am) == false)
                {
                    continue;
                }

                profiles2.TryGetValue(webProperty.Id, out AnalyticsProfile[] wpp);

                WebPropertyModel wpm = new WebPropertyModel(webProperty, wpp);

                am.WebProperties.Add(wpm);
            }

            Accounts = accounts2.Values.ToArray();
        }
Esempio n. 3
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);
        }
Esempio n. 4
0
        public object DeleteUser(string id)
        {
            var config = AnalyticsConfig.Current;

            AnalyticsConfigUser user = config.GetUserById(id);

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

            config.DeleteUser(user, Security.CurrentUser);

            return(true);
        }
Esempio n. 5
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)
            });
        }