2

I am very new to this and only started looking at python this week.

I am currently trying to extract data from Google Analytics and need to extract multiple days of data. I am however having issues trying to loop the code for multiple days:

CODE:

for date in daterange( start, end ):
    # EXTRACT OVERALL VISITS AND UNIQUE VISITS;
        def get_results(service, profile_id):
            return service.data().ga().get(
                ids='ga:' + profile_id,
                start_date=str(date),
                end_date=str(date),
                metrics='ga:visits,ga:newVisits,ga:visitors').execute()

This however doesn't seem to loop, but give me the data for the last date defined.

2 Answers 2

3

Your for loop is repeatedly defining a function, but never calling it.

Try this:

def get_results(service, profile_id, date):
    return service.data().ga().get(
            ids='ga:' + profile_id,
            start_date=str(date),
            end_date=str(date),
            metrics='ga:visits,ga:newVisits,ga:visitors').execute()

for date in daterange(start_date, end_date):
    get_results(service, profile_id, date)
Sign up to request clarification or add additional context in comments.

3 Comments

I think I am calling the function later on here is the bit after:
Sorry if i am a bit slow on this one. It keeps saying Service not defined for the last line, when it is.
I don't know what to tell you about the "service not defined" error. I don't have Google Analytics so I can't run this code.
0
for date in daterange( start, end ):

    # EXTRACT OVERALL VISITS AND UNIQUE VISITS;
        def get_results(service, profile_id):
            return service.data().ga().get(
                ids='ga:' + profile_id,
                start_date=str(date),
                end_date=str(date),
                metrics='ga:visits,ga:newVisits,ga:visitors').execute()
    # EXTRACT UNIQUE PAGE VIEWS FOR IMPORTANT STAGES;

        def print_results(results):
            if results:
                # Print data nicely for the user.
                print (results)
                #print 'First Profile: %s' % results.get('profileInfo').get('profileName')
                #print 'Total Visits: %s' % results.get('rows')[0][0]
                #print 'Total New Visits: %s' % results.get('rows')[0][1]
                #print 'Total Unique Visitors: %s' % results.get('rows')[0][2]
                #print 'Unique Visitors Starting the Survey: %s' % results2.get('rows')[0][0]
                #print 'Unique Visitors Completing the Survey: %s' % results3.get('rows')[0][0]
                #print 'Unique Visitors Registering: %s' % results4.get('rows')[0][0]
                #print f

                # OUTPUT TO FILE.
               # f.write (str(date) + ',' + results.get('rows')[0][0] + ',' + results.get('rows')[0][2] + ',' + results2.get('rows')[0][0] + ',' + results3.get('rows')[0][0] + ',' + results4.get('rows')[0][0] + '\n')
                #f.close()
            else:
                print 'No results found'

2 Comments

Now, instead of repeatedly defining a function inside the for loop, this code repeatedly defines two functions. This code doesn't call the functions; it just defines them. I am confused as to why you posted this.
Yes maybe I am not including enough of the code for you to get what it is doing. Basically I have hacked (poorly) some code from google and trying to bend it to suit my needs. I will attach the whole code for your to see.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.