Exemplo n.º 1
0
        public List<Visit> getVisitData(DateTime fromDate, DateTime toDate, Site site)
        {
            List<Visit> visits = new List<Visit>();

            int startIndex = 1;
            Boolean isMore;

            DataFeed feed = null;
            do
            {
                isMore = false;

                try
                {

                    feed = getGADataFeed(site, fromDate, toDate,
                                        "ga:date",
                                        "ga:visitors,ga:visits",
                                        "", "", startIndex);

                    if (feed != null)
                    {
                        foreach (DataEntry singleEntry in feed.Entries)
                        {
                            Visit visit = new Visit(singleEntry, site);
                            visits.Add(visit);
                            isMore = true;
                        }
                    }
                }
                catch (Exception rEx)
                {
                    log.Error("Error in getVisitData", rEx);
                }

                if (isMore) startIndex += numberOfGARecordsToRetrievePerRequest;
            } while (isMore);

            return visits;
        }
        public void recordAnalyticsVisit(Visit visit)
        {
            SqlConnection conn = new SqlConnection(connectionAnalytics);

            try
            {
                conn.Open();

                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;

                cmd.CommandText = @"IF NOT EXISTS(SELECT (1) FROM analytics_visits_site (nolock) WHERE Analytics_Date = @Date AND Site = @Account)
                                        INSERT INTO analytics_visits_site (Analytics_Date, Site, UniqueVisitors, Visits)
                                            VALUES (@Date, @Account, @UniqueVisitors, @Visits)
                                    ELSE
                                        UPDATE analytics_visits_site
                                        SET UniqueVisitors = @UniqueVisitors,
                                            Visits = @Visits
                                        WHERE Analytics_Date = @Date AND Site = @Account";

                cmd.Parameters.Add("@Date", SqlDbType.DateTime);
                cmd.Parameters["@Date"].Value = visit.gaDate;

                cmd.Parameters.Add("@Account", SqlDbType.NVarChar);
                cmd.Parameters["@Account"].Value = truncate(visit.gaAccount, 255);

                cmd.Parameters.Add("@UniqueVisitors", SqlDbType.Int);
                cmd.Parameters["@UniqueVisitors"].Value = visit.uniqueVisitors;

                cmd.Parameters.Add("@Visits", SqlDbType.Int);
                cmd.Parameters["@Visits"].Value = visit.visits;

                cmd.ExecuteNonQuery();
                cmd.Dispose();
            }
            catch (SqlException sqle)
            {
                log.Error("Error in recordAnalyticsVisitor", sqle);
            }
            finally
            {
                if (conn != null)
                {
                    if (conn.State == System.Data.ConnectionState.Open)
                    {
                        conn.Close();
                    }
                }
            }
        }