public List <AdminStats> AdminStatistics([FromBody] Analytics_Input analytics_Input)
        {
            //List < List < UserStats >> total_stats = new List<List<UserStats>>();

            List <AdminStats> admin_stats = new List <AdminStats>();

            GraphController use = new GraphController();

            Authorization auth = new Authorization(analytics_Input.extra);

            var result = auth.service.Data.Ga.Get("ga:" + analytics_Input.ga_id, analytics_Input.from_date, analytics_Input.to_date, analytics_Input.session + ",ga:newUsers");

            result.Dimensions = "ga:date, " + analytics_Input.country;
            result.Filters    = "ga:medium=@referral";
            try {
                var response = result.Execute();
                if (response.TotalResults != 0)
                {
                    foreach (var row in response.Rows)
                    {
                        AdminStats temp = new AdminStats()
                        {
                            date = new DateTime(), earning = 0.0, day = "", premium = 0, non_premium = 0, total_traffic = 0, total_earning = 0, country_stats = new List <CountryStats>()
                        };
                        // UserStats temp = user_stats.Last<UserStats>();


                        temp = admin_stats.FirstOrDefault(x => x.day == row[0]);

                        if (temp == null)
                        {
                            temp = new AdminStats()
                            {
                                date = new DateTime(), earning = 0.0, day = "", premium = 0, non_premium = 0, total_traffic = 0, total_earning = 0, country_stats = new List <CountryStats>()
                            };
                        }
                        if (!(temp.day.Equals(row[0])))
                        {
                            AdminStats stats = new AdminStats()
                            {
                                date = new DateTime(), earning = 0.0, day = "", premium = 0, non_premium = 0, total_traffic = 0, total_earning = 0, country_stats = new List <CountryStats>()
                            };

                            stats.day  = row[0];
                            stats.date = dateConverter(row[0]);
                            if (row[1].Equals("Canada"))
                            {
                                stats.premium += long.Parse(row[2]);
                                stats.earning += use.GetEarned(row[2], "premium");

                                stats.total_traffic += long.Parse(row[2]);
                            }
                            else
                            {
                                stats.non_premium   += long.Parse(row[2]);
                                stats.earning       += use.GetEarned(row[2], "non-premium");
                                stats.total_traffic += long.Parse(row[2]);
                            }

                            CountryStats cFind = new CountryStats()
                            {
                                country = row[1], sessions = "0", newSessions = "0"
                            };


                            cFind.sessions    = (Int64.Parse(cFind.sessions) + Int64.Parse(row[2])).ToString();
                            cFind.newSessions = (Int64.Parse(cFind.newSessions) + Int64.Parse(row[3])).ToString();
                            stats.country_stats.Add(cFind);
                            admin_stats.Add(stats);
                        }
                        else
                        {
                            admin_stats.Remove(temp);
                            CountryStats cTemp = new CountryStats();
                            if (row[1].Equals("Canada"))
                            {
                                temp.premium       += long.Parse(row[2]);
                                temp.earning       += use.GetEarned(row[2], "premium");
                                temp.total_traffic += long.Parse(row[2]);
                            }
                            else
                            {
                                temp.non_premium   += long.Parse(row[2]);
                                temp.earning       += use.GetEarned(row[2], "non-premium");
                                temp.total_traffic += long.Parse(row[2]);
                            }

                            CountryStats cFind = new CountryStats()
                            {
                                country = row[1], sessions = "0", newSessions = "0"
                            };

                            cFind.sessions    = (Int64.Parse(row[2])).ToString();
                            cFind.newSessions = (Int64.Parse(row[3])).ToString();
                            temp.country_stats.Add(cFind);
                            admin_stats.Add(temp);
                        }
                    }
                }
            }
            catch (Exception e)
            { }
            return(admin_stats);
        }
        public List <UserStats> statistics([FromBody] User_Analytic_Input analytics_Input)
        {
            //List < List < UserStats >> total_stats = new List<List<UserStats>>();
            GraphController  use               = new GraphController();
            List <UserStats> user_stats        = new List <UserStats>();
            List <get_user_traffic_Result> res = get_all_sites(analytics_Input.uid);

            foreach (var item in res)
            {
                Authorization auth = new Authorization(item.email);

                var result = auth.service.Data.Ga.Get("ga:" + item.ga_id, analytics_Input.from_date, analytics_Input.to_date, analytics_Input.session + ",ga:newUsers");
                result.Dimensions = "ga:date, " + analytics_Input.country;
                result.Filters    = "ga:campaign=@" + analytics_Input.extra;
                try {
                    var response = result.Execute();
                    if (response.TotalResults != 0)
                    {
                        foreach (var row in response.Rows)
                        {
                            UserStats temp = new UserStats()
                            {
                                date = new DateTime(), day = "", premium = 0, non_premium = 0, total_traffic = 0, total_earning = 0, country_stats = new List <CountryStats>()
                            };
                            // UserStats temp = user_stats.Last<UserStats>();


                            temp = user_stats.FirstOrDefault(x => x.day == row[0]);

                            if (temp == null)
                            {
                                temp = new UserStats()
                                {
                                    date = new DateTime(), day = "", premium = 0, non_premium = 0, total_traffic = 0, total_earning = 0, country_stats = new List <CountryStats>()
                                };
                            }
                            if (!(temp.day.Equals(row[0])))
                            {
                                UserStats stats = new UserStats()
                                {
                                    date = new DateTime(), day = "", premium = 0, non_premium = 0, total_traffic = 0, total_earning = 0, country_stats = new List <CountryStats>()
                                };

                                stats.day  = row[0];
                                stats.date = dateConverter(stats.day);

                                if (row[1].Equals("Canada"))
                                {
                                    stats.premium       += long.Parse(row[2]);
                                    stats.total_earning += use.GetEarned(row[2], "premium");

                                    stats.total_traffic += long.Parse(row[2]);
                                }
                                else
                                {
                                    stats.non_premium   += long.Parse(row[2]);
                                    stats.total_earning += use.GetEarned(row[2], "non-premium");
                                    stats.total_traffic += long.Parse(row[2]);
                                }

                                var cFind = stats.country_stats.FirstOrDefault(x => x.country == row[1]);
                                if (cFind == null)
                                {
                                    cFind = new CountryStats()
                                    {
                                        country = row[1], sessions = "0", newSessions = "0"
                                    };
                                }
                                else
                                {
                                    stats.country_stats.Remove(cFind);
                                }
                                cFind.sessions    = (Int64.Parse(cFind.sessions) + Int64.Parse(row[2])).ToString();
                                cFind.newSessions = (Int64.Parse(cFind.newSessions) + Int64.Parse(row[3])).ToString();
                                stats.country_stats.Add(cFind);
                                user_stats.Add(stats);
                            }
                            else
                            {
                                user_stats.Remove(temp);
                                CountryStats cTemp = new CountryStats();
                                if (row[1].Equals("Canada"))
                                {
                                    temp.premium       += long.Parse(row[2]);
                                    temp.total_earning += use.GetEarned(row[2], "premium");

                                    temp.total_traffic += long.Parse(row[2]);
                                }
                                else
                                {
                                    temp.non_premium   += long.Parse(row[2]);
                                    temp.total_earning += use.GetEarned(row[2], "non-premium");
                                    temp.total_traffic += long.Parse(row[2]);
                                }
                                var cFind = temp.country_stats.FirstOrDefault(x => x.country == row[1]);
                                if (cFind == null)
                                {
                                    cFind = new CountryStats()
                                    {
                                        country = row[1], sessions = "0", newSessions = "0"
                                    };
                                }
                                else
                                {
                                    temp.country_stats.Remove(cFind);
                                }
                                cFind.sessions    = (Int64.Parse(cFind.sessions) + Int64.Parse(row[2])).ToString();
                                cFind.newSessions = (Int64.Parse(cFind.newSessions) + Int64.Parse(row[3])).ToString();
                                temp.country_stats.Add(cFind);
                                user_stats.Add(temp);
                            }
                        }
                    }
                    // total_stats.Add(user_stats);
                }
                catch (Exception e)
                { }
            }

            return(user_stats);
        }
        public user_traffic_earning UserSessions([FromBody] User_Analytic_Input analytics_Input)
        {
            user_traffic_earning traffic = new user_traffic_earning()
            {
                todayEarned = 0, monthlyTraffic = 0, todayTraffic = 0, monthlyEarned = 0
            };
            ArticleController a             = new ArticleController();
            GraphController   use           = new GraphController();
            user_traffic      month_traffic = new user_traffic()
            {
                premium = 0, non_premium = 0
            };
            user_traffic today_traffic = new user_traffic()
            {
                premium = 0, non_premium = 0
            };
            List <get_user_traffic_Result> res = get_all_sites(analytics_Input.uid);
            var to     = System.DateTime.Now;
            var t_from = System.DateTime.Now;
            var m_from = System.DateTime.Now;

            if (to.Month == 1)
            {
                m_from = new DateTime(to.Year - 1, 12, to.Day);
            }
            else
            {
                m_from = new DateTime(to.Year, to.Month - 1, to.Day);
            }

            foreach (var item in res)
            {
                Authorization auth = new Authorization(item.email);

                var result  = auth.service.Data.Ga.Get("ga:" + item.ga_id, a.convertDate(m_from), a.convertDate(to), analytics_Input.session);
                var result1 = auth.service.Data.Ga.Get("ga:" + item.ga_id, a.convertDate(m_from), a.convertDate(to), analytics_Input.session);
                var result3 = auth.service.Data.Ga.Get("ga:" + item.ga_id, a.convertDate(t_from), a.convertDate(to), analytics_Input.session);
                var result4 = auth.service.Data.Ga.Get("ga:" + item.ga_id, a.convertDate(t_from), a.convertDate(to), analytics_Input.session);
                if ((analytics_Input.extra != null))
                {
                    if (!analytics_Input.extra.Equals(""))
                    {
                        result.Filters  = "ga:campaign=@" + analytics_Input.extra;
                        result1.Filters = "ga:campaign=@" + analytics_Input.extra + ";ga:country=@Canada";
                        result3.Filters = "ga:campaign=@" + analytics_Input.extra;
                        result4.Filters = "ga:campaign=@" + analytics_Input.extra + ";ga:country=@Canada";
                    }
                }
                try {
                    var today_result    = result3.Execute();
                    var today_result1   = result4.Execute();
                    var session_result  = result.Execute();
                    var session_result1 = result1.Execute();
                    int count           = (int)session_result.TotalResults;
                    if (count != 0)
                    {
                        month_traffic.non_premium += long.Parse(session_result.Rows[0].FirstOrDefault <string>());
                    }
                    //     month_traffic.non_premium = month_traffic.non_premium + 0;
                    int count2 = (int)session_result1.TotalResults;
                    if (count2 != 0)
                    {
                        month_traffic.premium += long.Parse(session_result1.Rows[0].FirstOrDefault <string>());
                    }

                    int count3 = (int)today_result.TotalResults;
                    if (count3 != 0)
                    {
                        today_traffic.non_premium += long.Parse(today_result.Rows[0].FirstOrDefault <string>());
                    }
                    //     month_traffic.non_premium = month_traffic.non_premium + 0;
                    int count4 = (int)today_result1.TotalResults;
                    if (count4 != 0)
                    {
                        today_traffic.premium += long.Parse(today_result1.Rows[0].FirstOrDefault <string>());
                    }
                    //     month_traffic.premium = month_traffic.premium + 0;
                }
                catch (Exception e)
                { }
            }


            month_traffic.non_premium = month_traffic.non_premium - month_traffic.premium;
            today_traffic.non_premium = today_traffic.non_premium - today_traffic.premium;
            traffic.monthlyTraffic    = month_traffic.premium + month_traffic.non_premium;
            traffic.todayTraffic      = today_traffic.premium + today_traffic.non_premium;
            traffic.monthlyEarned     = use.GetEarned((month_traffic.premium).ToString(), "premium") + use.GetEarned((month_traffic.non_premium).ToString(), "non-premium");
            traffic.todayEarned       = use.GetEarned((today_traffic.premium).ToString(), "premium") + use.GetEarned((today_traffic.non_premium).ToString(), "non-premium");

            return(traffic);
        }