def get_sources(service): print 'get_sources' results=service.data().realtime().get( ids='ga:77364516', metrics='ga:activeVisitors', dimensions='ga:medium,ga:source', sort='-ga:activeVisitors').execute() rows=list(results.get('rows')) ''' ofile = open('d:\\d3\\charisma-master\\medium.csv', "wb") writer = csv.writer(ofile, delimiter=',',) writer.writerow(['Medium','Source','Active Visitors']) for row in rows: writer.writerow([row[0],row[1],row[2]] ) ofile.close() realtime=[] ifile=open("d:\\d3\\charisma-master\\r.csv",'rb') reader=csv.reader(ifile) headers = reader.next() for row in reader: realtime.append(int(row[0])) realtime= realtime[-180:] ifile.close() realtime.append(int(results.get('totalsForAllResults').get('ga:activeVisitors'))) ofile=open("d:\\d3\\charisma-master\\r.csv", "wb") writer=csv.writer(ofile) writer.writerow(['Vcount']) for row in realtime: writer.writerow([row]) #myfile.write( str(int(results.get('totalsForAllResults').get('ga:activeVisitors')) )) ofile.close() ''' ofile=open("d:\\d3\\charisma-master\\r1.csv", "wb") ofile.write(results.get('totalsForAllResults').get('ga:activeVisitors')) ofile.close() mongoInsert.upSertMongo(time.strftime("%Y-%m-%dT%H:%M"),'users',results.get('totalsForAllResults').get('ga:activeVisitors')) print 'success'
def get_Device(service): print 'get_Device' results= service.data().realtime().get( ids='ga:77364516', metrics='ga:activeVisitors', dimensions='ga:deviceCategory,ga:operatingSystem', sort='-ga:activeVisitors').execute() rows=list(results.get('rows')) ofile = open('d:\\d3\\charisma-master\\device.csv', "wb") writer = csv.writer(ofile, delimiter=',',) writer.writerow(['Device','OS','ActiveVisitors']) for row in rows: writer.writerow([row[0],row[1],row[2]] ) ofile.close() ofile=open("d:\\d3\\charisma-master\\r1.csv", "wb") ofile.write(results.get('totalsForAllResults').get('ga:activeVisitors')) ofile.close() mongoInsert.upSertMongo(time.strftime("%Y-%m-%dT%H:%M"),'users',results.get('totalsForAllResults').get('ga:activeVisitors')) print 'success'