コード例 #1
0
        public DataTable getDayWiseAnalyticsApi(string profileId, Guid user)
        {
            DataTable dtAnalytics = new DataTable();

            try
            {
                Analytics    objAlyt = new Analytics();
                oAuthTokenGa obj     = new oAuthTokenGa();
                GoogleAnalyticsAccountRepository objGaAccRepo   = new GoogleAnalyticsAccountRepository();
                GoogleAnalyticsStats             objGaStats     = new GoogleAnalyticsStats();
                GoogleAnalyticsStatsRepository   objGaStatsRepo = new GoogleAnalyticsStatsRepository();
                GoogleAnalyticsAccount           objGaAcc       = objGaAccRepo.getGoogelAnalyticsAccountDetailsById(profileId, user);
                string strRefresh = obj.GetAccessToken(objGaAcc.RefreshToken);
                if (!strRefresh.StartsWith("["))
                {
                    strRefresh = "[" + strRefresh + "]";
                }
                JArray objArray = JArray.Parse(strRefresh);
                foreach (var itemRefresh in objArray)
                {
                    objGaAcc.AccessToken = itemRefresh["access_token"].ToString();
                }
                XmlDocument xDoc    = new XmlDocument();
                DateTime    startdt = DateTime.Now.AddMonths(-3);
                DateTime    enddt   = DateTime.Now;
                xDoc.LoadXml(objAlyt.getAnalyticsData(objGaAcc.GaProfileId, "metrics=ga:visits&dimensions=ga:day", startdt.ToShortDateString().Replace("/", "-"), "2013-07-25", objGaAcc.AccessToken));
                dtAnalytics = getdatafromXml(xDoc);
                for (int i = 0; i < dtAnalytics.Rows.Count; i++)
                {
                    objGaStats.EntryDate   = DateTime.Now;
                    objGaStats.GaAccountId = objGaAcc.GaAccountId;
                    if (dtAnalytics.Rows[i]["title"].ToString().Contains("ga:day"))
                    {
                        objGaStats.gaDate = dtAnalytics.Rows[i]["title"].ToString().Substring(8);
                    }
                    objGaStats.gaVisits    = dtAnalytics.Rows[i]["ga:visitors"].ToString();
                    objGaStats.Id          = Guid.NewGuid();
                    objGaStats.UserId      = user;
                    objGaStats.GaProfileId = objGaAcc.GaProfileId;
                    if (!objGaStatsRepo.checkGoogleAnalyticsDateStatsExists(objGaAcc.GaProfileId, "day", dtAnalytics.Rows[i]["title"].ToString(), user))
                    {
                        objGaStatsRepo.addGoogleAnalyticsStats(objGaStats);
                    }
                    else
                    {
                        objGaStatsRepo.updateGoogleAnalyticsStats(objGaStats);
                    }
                }
            }
            catch (Exception Err)
            {
                Console.Write(Err.StackTrace);
            }
            return(dtAnalytics);
        }
コード例 #2
0
        public DataTable getRegionWiseAnalyticsApi(string profileId)
        {
            DataTable dtAnalytics = new DataTable();

            try
            {
                User         user    = (User)HttpContext.Current.Session["LoggedUser"];
                Analytics    objAlyt = new Analytics();
                oAuthTokenGa obj     = new oAuthTokenGa();
                GoogleAnalyticsAccountRepository objGaAccRepo   = new GoogleAnalyticsAccountRepository();
                GoogleAnalyticsStats             objGaStats     = new GoogleAnalyticsStats();
                GoogleAnalyticsStatsRepository   objGaStatsRepo = new GoogleAnalyticsStatsRepository();
                GoogleAnalyticsAccount           objGaAcc       = objGaAccRepo.getGoogelAnalyticsAccountDetailsById(profileId, user.Id);
                string strRefresh = obj.GetAccessToken(objGaAcc.RefreshToken);
                if (!strRefresh.StartsWith("["))
                {
                    strRefresh = "[" + strRefresh + "]";
                }
                JArray objArray = JArray.Parse(strRefresh);
                foreach (var itemRefresh in objArray)
                {
                    objGaAcc.AccessToken = itemRefresh["access_token"].ToString();
                }
                XmlDocument xDoc = new XmlDocument();
                xDoc.LoadXml(objAlyt.getAnalyticsData(objGaAcc.GaProfileId, "metrics=ga:visits&dimensions=ga:year", "2013-01-01", "2013-07-15", objGaAcc.AccessToken));
                dtAnalytics = getdatafromXml(xDoc);
                for (int i = 0; i < dtAnalytics.Rows.Count; i++)
                {
                    objGaStats.EntryDate   = DateTime.Now;
                    objGaStats.GaAccountId = objGaAcc.GaAccountId;
                    if (dtAnalytics.Rows[i]["title"].ToString().Contains("ga:year"))
                    {
                        objGaStats.gaYear = dtAnalytics.Rows[i]["title"].ToString().Substring(8);
                    }
                    objGaStats.gaVisits    = dtAnalytics.Rows[i]["ga:visitors"].ToString();
                    objGaStats.Id          = Guid.NewGuid();
                    objGaStats.UserId      = user.Id;
                    objGaStats.GaProfileId = objGaAcc.GaProfileId;
                    if (!objGaStatsRepo.checkGoogleAnalyticsDateStatsExists(objGaAcc.GaProfileId, "year", dtAnalytics.Rows[i]["title"].ToString(), user.Id))
                    {
                        objGaStatsRepo.addGoogleAnalyticsStats(objGaStats);
                    }
                    else
                    {
                        objGaStatsRepo.updateGoogleAnalyticsStats(objGaStats);
                    }
                }
            }
            catch (Exception Err)
            {
                Console.Write(Err.StackTrace);
            }
            return(dtAnalytics);
        }