daisy
daisy

Reputation: 357

Google Analytics shows data only till previous day using c#

I have a code to fetch Google Analytics data for a website in c# in Visual studio, it shows or fetch data till previous day, what about today's data?

Below is the code used.

     var today = DateTime.Now;
        var todayDate = today.ToString("yyyy-MM-dd");
        string date = "2012-01-01";
        DateTime dt = Convert.ToDateTime(date);
        var startDate = dt.ToString("yyyy-MM-dd");
        var websiteProfileID = "ga:XXXXXXXX";
        string serviceAccountEmail = "[email protected]";
        var certificate = new X509Certificate2(@"D:/RCGA-fdrdrd250d7c.p12", "notasecret", X509KeyStorageFlags.Exportable);

        var credential = new ServiceAccountCredential(
        new ServiceAccountCredential.Initializer(serviceAccountEmail)
        {
            Scopes = new[] { AnalyticsService.Scope.Analytics }
        }.FromCertificate(certificate));

        // Create the service.
        var service = new AnalyticsService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "GoogleAnalytics",
        });

        var viewTraffic = service.Data.Ga.Get(websiteProfileID, startDate, todayDate, "ga:hits");
        viewTraffic.Dimensions = "ga:userType,ga:date";
        viewTraffic.MaxResults = 10000;
        viewTrafficData = viewTraffic.Execute();

        foreach (List<string> row in viewTrafficData.Rows)
        {
            foreach (string col in row)
            {
                TextWriter tsw = new StreamWriter(@"D:\Hello.txt", true);
                tsw.WriteLine(col);
                tsw.Close();
            }

        }

Upvotes: 1

Views: 1444

Answers (2)

Jordi
Jordi

Reputation: 44

Up till last week I did get Google Analytics data from the V3 API for today/now by using todays date as 'end' date (and 30 days ago as 'start' date): pageviews/unique visitors data was updated every 10 to 20 minutes. I literally saw the number of pageviews rise while I was browsing new content on my website (which shows the pageviews/visitors stats on each page)

Since last week I'm no longer receiving such fresh data, it now takes up to 12-24 hours. For those hours my new pages (news articles) just show 0 for the first 12 hours.

It could be a glitch in the API, because the recent data (besides the Realtime tab) is available in all other reports on the Google Analytics website. At this moment I can see data of the first ~15 minutes of the current hour.

However, it could also be related to their new service '360 Analytics' (paid), which was released 2 days ago (March 1) and offers 'realtime' data. The timing of this product release and my API calls no longer returning fresh data could indicate a strategic move. I tried to reach out to the Google Analytics team, but did not get any response on why the data is no longer fresh. If this is the case, it would be polite if Google actively communicated about this since it's causing regression in the V3 API.

Update March 5, 2017: Since this morning I'm receiving recent (fresh)data again from Google Analytics V3 API for data related to 'today'. Maybe you can try re-running your existing code to see if the API is returning the data you expected in the first place?

Upvotes: 0

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 117301

The core reporting API V3 does support the term 'today' so you could just use the term today.

var todayDate = "today";

Values must match [0-9]{4}-[0-9]{2}-[0-9]{2}|today|yesterday|[0-9]+(daysAgo).

However you need to remember something about Google Analytics data. The data has not completed processing for 24 - 48 hours. That means that if you request todays data then check again tomorrow or in two days the numbers will probably be different.

The Google Analytics reporting API V4 returns a parameter called isgolden which will tell you if the data has completed processing or not.

isDataGolden boolean Indicates if response to this request is golden or not. Data is golden when the exact same request will not produce any new results if asked at a later point in time

However the Google Analytics Reporting API v4 does not have the 'today' option for date ranges. You will have to create your own get todays date in your script. There are samples for v4 here there are more samples here.

Upvotes: 1

Related Questions