0

I have been creating an automatic report generation script that will take data from a user usage report and put into a google sheet. My script has been running fine so far but the data that I get back seems a little odd to me.

I generate reports from a function I created called report raw. I am new to coding so go easy but this is how I am pulling information. I then put it into a dictionary and use that dictionary to create reports over several days. The idea of the report is to track usage of google apps to create another way to look at attendance. For instance, the report will look at every weekday, compare each day, and then make a metric based on their usage.

def report_raw(date_entry,userkey, parameters,rep,maxresults):
    date = Date()
    creds = google_authenticate()
    service = build('admin', 'reports_v1', credentials=creds)
    user_log = {}
    fieldnames = config_settings('d:/data/reports/data.csv','')[0]['Fieldnames']
    #this calls the API to generate a user report with specific variables
    results = service.userUsageReport().get(userKey='all',date=date_entry, maxResults = maxresults).execute()
    if 'usageReports' not in results:
            return 'data is not ready for export'
    else:
        for result in results.get('usageReports'):
            email = result['entity']['userEmail']
            activities = result['parameters']
            user_log[email] = {}
            
            for activity in activities:
                for fieldname in fieldnames: 
                    if activity['name'] == fieldname:
                        for key,value in activity.items():
                            if key == 'name':
                                x = value
                            user_log[email][x] = value
    user_log['date'] = {'date' : str(date_entry), 'weekday' : date.today.weekday()}
    return user_log

The problem I am running into is that if I generate a report for a specific date like 2021-01-25 I will get interaction and login times for 2021-01-26 on some users whereas most others stick within the 2021-01-25 time frame. When I generate the next day's report, in this case 2021-01-26, the users who showed activity on 2021-01-26 don't have a change in value. On top of this some users have a interaction time for 1970-01-01(which I am assuming means they have never interacted with the service since it has started tracking their info as that is the start of Unix time). I wanted to know if anyone has played around with user usage reports and if there is a mistake in how I am generating the report. Thanks!

1 Answers1

0

Two things:

  • Keep in mind that reports are being created wiith up to 24 hours delay which can lead to issues if you want to retrieve the activity for "today".
  • Date needs to be a timestamp is in the ISO 8601 format, yyyy-mm-dd (do not provide the weekday). Log your date object to make sure that it is formatted correctly. Also be careful with time zones!

Reference for date and other parameters: Method: userUsageReport.get

ziganotschka
  • 25,866
  • 2
  • 16
  • 33
  • The activity issues I am having with dates not syncing up is happening for dates that occur more than a week ago. From what I have learned from a google interaction I had last week if someone makes a classroom interaction request on Monday but it completes on Tuesday it will still show up on Monday's report because that is when the request was made. As for the Date that you are talking about, I created a Date class which I use to simplify date creation. It looks for the nearest Monday where a full week's report is available and then exports a date object for that date( in ISO format). – Noah Padgett Feb 08 '21 at 13:32
  • What about `date_entry`? is it a valid `yyyy-mm-dd` string? – ziganotschka Feb 08 '21 at 15:45