public DataTable getCountryAnalyticsApi(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.getGoogelAnalyticsAccountHomeDetailsById(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(); xDoc.LoadXml(objAlyt.getAnalyticsData(objGaAcc.GaProfileId, "metrics=ga:visits&dimensions=ga:country", "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:country")) objGaStats.gaCountry = dtAnalytics.Rows[i]["title"].ToString().Substring(11); objGaStats.gaVisits = dtAnalytics.Rows[i]["ga:visitors"].ToString(); objGaStats.GaProfileId = objGaAcc.GaProfileId; objGaStats.Id = Guid.NewGuid(); objGaStats.UserId = user; if (!objGaStatsRepo.checkGoogleAnalyticsDateStatsExists(objGaAcc.GaProfileId, "country", dtAnalytics.Rows[i]["title"].ToString(), user)) objGaStatsRepo.addGoogleAnalyticsStats(objGaStats); else objGaStatsRepo.updateGoogleAnalyticsStats(objGaStats); } } catch (Exception Err) { Console.Write(Err.StackTrace); } return dtAnalytics; }
public string GetGoogleAnalyticsData(string ProfileId, string AccessToken, string HostName) { try { HostName = HostName.Replace("www.", ""); string arrVisit = string.Empty; string arrViews = string.Empty; string arrTwtMentions = string.Empty; string arrActicleandBlogs = string.Empty; string finalToken = string.Empty; oAuthTokenGa objToken = new oAuthTokenGa(); string finaltoken = objToken.GetAccessToken(AccessToken); try { JObject objArray = JObject.Parse(finaltoken); finalToken = objArray["access_token"].ToString(); } catch (Exception ex) { finalToken = AccessToken; Console.WriteLine(ex.StackTrace); } Analytics _Analytics = new Analytics(); DateTime startDate = DateTime.UtcNow.AddDays(-90); while (startDate.Date < DateTime.UtcNow.Date) { try { string visits = string.Empty; string pageviews = string.Empty; try { string analytics = _Analytics.getAnalyticsData(ProfileId, "ga:visits,ga:pageviews", startDate.ToString("yyyy-MM-dd"), startDate.AddDays(2).ToString("yyyy-MM-dd"), finalToken); JObject JData = JObject.Parse(analytics); visits = JData["totalsForAllResults"]["ga:visits"].ToString(); pageviews = JData["totalsForAllResults"]["ga:pageviews"].ToString(); arrVisit += visits + ","; arrViews += pageviews + ","; } catch (Exception ex) { arrVisit += "0" + ","; arrViews += "0" + ","; } long startUnixTime = startDate.Date.ToUnixTimestamp(); long endUnixTime = startDate.AddDays(3).Date.AddSeconds(-1).ToUnixTimestamp(); var ret = TwtsearchRepo.Find<Domain.Socioboard.MongoDomain.TwitterUrlMentions>(t => t.HostName.Equals(HostName)); var task = Task.Run(async () => { return await ret; }); IList<Domain.Socioboard.MongoDomain.TwitterUrlMentions> lstTwitterUrlMentions = task.Result.ToList(); int twtCount = lstTwitterUrlMentions.Count(t => t.Feeddate > startUnixTime && t.Feeddate <= endUnixTime); arrTwtMentions += twtCount.ToString() + ","; var ret1 = ArticlesAndBlogsRepo.Find<Domain.Socioboard.MongoDomain.ArticlesAndBlogs>(t => t.HostName.Equals(HostName)); var task1 = Task.Run(async () => { return await ret1; }); IList<Domain.Socioboard.MongoDomain.ArticlesAndBlogs> lstArticlesAndBlogs=task1.Result.ToList(); int artucleCount = lstArticlesAndBlogs.Count(t => t.Created_Time > startUnixTime && t.Created_Time <= endUnixTime); arrActicleandBlogs += artucleCount.ToString() + ","; startDate = startDate.AddDays(3); } catch (Exception ex) { logger.Error("GetGoogleAnalyticsData1 => " + ex.Message); } } arrVisit = arrVisit.TrimEnd(','); arrViews = arrViews.TrimEnd(','); arrTwtMentions = arrTwtMentions.TrimEnd(','); arrActicleandBlogs = arrActicleandBlogs.TrimEnd(','); Domain.Socioboard.Domain.GoogleAnalyticsReport _GoogleAnalyticsReport = new Domain.Socioboard.Domain.GoogleAnalyticsReport(); _GoogleAnalyticsReport.GaProfileId = ProfileId; _GoogleAnalyticsReport.Views = arrViews; _GoogleAnalyticsReport.Visits = arrVisit; _GoogleAnalyticsReport.TwitterMention = arrTwtMentions; _GoogleAnalyticsReport.Article_Blogs = arrActicleandBlogs; _GoogleAnalyticsAccountRepository.AddGoogleAnalyticsReport(_GoogleAnalyticsReport); } catch (Exception ex) { logger.Error("GetGoogleAnalyticsData2 => " + ex.Message); } return "Updated Successfuly"; }
public string UpdateGoogleAnalyticsAccount(string ProfileId, string AccessToken, string HostName) { try { HostName = HostName.Replace("www.", ""); Analytics _Analytics = new Analytics(); string visits = string.Empty; string pageviews = string.Empty; string finalToken = string.Empty; oAuthTokenGa objToken = new oAuthTokenGa(); string finaltoken = objToken.GetAccessToken(AccessToken); JObject objArray = JObject.Parse(finaltoken); try { finalToken = objArray["access_token"].ToString(); // break; } catch (Exception ex) { Console.WriteLine(ex.StackTrace); } try { string analytics = _Analytics.getAnalyticsData(ProfileId, "ga:visits,ga:pageviews", DateTime.UtcNow.AddDays(-7).ToString("yyyy-MM-dd"), DateTime.UtcNow.ToString("yyyy-MM-dd"), finalToken); JObject JData = JObject.Parse(analytics); visits = JData["totalsForAllResults"]["ga:visits"].ToString(); pageviews = JData["totalsForAllResults"]["ga:pageviews"].ToString(); double startUnixTime = DateTime.UtcNow.AddDays(-7).Date.ToUnixTimestamp(); double endUnixTime = DateTime.UtcNow.Date.ToUnixTimestamp(); var ret = TwtsearchRepo.Find<Domain.Socioboard.MongoDomain.TwitterUrlMentions>(t => t.HostName.Equals(HostName)); var task = Task.Run(async () => { return await ret; }); int twtCount = task.Result.Where(t => t.Feeddate > startUnixTime && t.Feeddate < endUnixTime).ToList().Count; var ret1 = ArticlesAndBlogsRepo.Find<Domain.Socioboard.MongoDomain.ArticlesAndBlogs>(t => t.HostName.Equals(HostName)); var task1 = Task.Run(async () => { return await ret1; }); int articleCount = task1.Result.Where(t => t.Created_Time > startUnixTime && t.Created_Time < endUnixTime).ToList().Count; _GoogleAnalyticsAccountRepository.updateGoogelAnalyticsUser(ProfileId, Double.Parse(visits), Double.Parse(pageviews), Double.Parse(twtCount.ToString()), Double.Parse(articleCount.ToString())); } catch (Exception ex) { logger.Error("UpdateGoogleAnalyticsAccount1 => " + ex.Message); } } catch (Exception ex) { logger.Error("UpdateGoogleAnalyticsAccount2 => " + ex.Message); } return "Successfuly Updated"; }