def get_account(self): if not self._account: connection = Connection(settings.GOOGLE_ACCOUNT, settings.GOOGLE_ACCOUNT_PASSWORD) self._account = connection.get_account(settings.GOOGLE_ACCOUNT_PROFILE_ID) return self._account
def analitics(start_date, end_date): try: from googleanalytics import Connection from googleanalytics.data import DataPoint, DataSet import datetime google_account_email = '*****@*****.**' # agregue el email de la cuenta de google analytics o la cuenta con que se registro google_account_password = '******' # UTILICE SU CUENTA POR FAVOR # SI METES EL PASSWORD MAL TE VA TIRAR 403 ERROR connection = Connection(google_account_email, google_account_password) accounts = connection.get_accounts() # aqui estan todas mis cuentas #print accounts[2].profile_id # aqui saque uno de mis cuentas para saber que hay enn esa lista dir(accounts[2]) y asi conoces mas aparte del profile_id # accounts[2].profile_id esto me devolvio 23492578 # para entender mejor https://github.com/clintecker/python-googleanalytics/blob/master/USAGE.md # a mi pagina con id 23492578 le voy a calcular unos analytics account = connection.get_account(accounts[0].profile_id) #data = account.get_data(start_date, end_date, metrics=['visits'],dimensions=['city']) # tire el querys filters = [] data_source = account.get_data(start_date=start_date, end_date=end_date, dimensions=['source'], metrics=['visits', ], sort=['-visits', ], filters=filters) data_source = map(lambda source: {'nombre': source[0][0], 'valor': source[1][0]}, data_source.list) ###################################################################################################################################################### data_city = account.get_data(start_date, end_date, metrics=['visits'], dimensions=['city'], sort=['-visits', ]) # tire el querys data_city = map(lambda source: {'nombre': source[0][0], 'valor': source[1][0]}, data_city.list) return data_city, data_source except: return [], []
def execute(self, *args, **options): output = StringIO.StringIO() writer = csv.writer(output) writer.writerow(['Old Url', 'New Url', 'Response Code']) writer.writerow(["/old.html", "/new", '301, 302 or 410']) if options["use_analytics"]: try: from googleanalytics import Connection except ImportError: raise CommandError('Must install python-googleanalytics') if not all( [options["ga_user"], options["ga_pwd"], options["ga_profile"] ]): raise CommandError( 'Must specify your google analytics user, password and profile' ) connection = Connection(options["ga_user"], options["ga_pwd"]) account = connection.get_account(options["ga_profile"]) end_date = datetime.date.today() num_months = int(options["num_analytics_months"]) start_date = end_date - datetime.timedelta(num_months * 365 / 12) data = account.get_data(start_date=start_date, end_date=end_date, dimensions=['pagepath'], metrics=['visits']) sorted_data = sorted(data.dict.iteritems(), key=operator.itemgetter(1), reverse=True) for url, visits in sorted_data: writer.writerow([csv_safe(url), '', '']) print output.getvalue()
def execute(self, *args, **options): num_rows = options["num_rows"] output = StringIO.StringIO() writer = csv.writer(output) writer.writerow(['Old Url','New Url','Response Code']) writer.writerow(["/old.html","/new",'301, 302 or 410']) if options["use_analytics"]: try: from googleanalytics import Connection except ImportError: raise CommandError('Must install python-googleanalytics') if not all([options["ga_user"], options["ga_pwd"], options["ga_profile"]]): raise CommandError('Must specify your google analytics user, password and profile') connection = Connection(options["ga_user"], options["ga_pwd"]) account = connection.get_account(options["ga_profile"]) end_date = datetime.date.today() num_months = int(options["num_analytics_months"]) start_date = end_date - datetime.timedelta(num_months*365/12) data = account.get_data(start_date=start_date, end_date=end_date, dimensions=['pagepath'], metrics=['visits']) sorted_data = sorted(data.dict.iteritems(), key=operator.itemgetter(1), reverse=True) for url, visits in sorted_data: writer.writerow([csv_safe(url),'','']) else: [writer.writerow(['','','']) for row in range(0,num_rows)] print output.getvalue()
def get_namespace(self): coord = [] table_id = self.resource.get_property('table_id') if with_stats and table_id: try: connection = Connection(self.GMAIL_ACCOUNT, self.GMAIL_PASSWORD) account = connection.get_account(table_id) coord = self.get_coord(account) except Exception: pass return {'coord': coord, 'title': self.title}
def analitics(start_date, end_date): try: from googleanalytics import Connection from googleanalytics.data import DataPoint, DataSet import datetime google_account_email = '*****@*****.**' # agregue el email de la cuenta de google analytics o la cuenta con que se registro google_account_password = '******' # UTILICE SU CUENTA POR FAVOR # SI METES EL PASSWORD MAL TE VA TIRAR 403 ERROR connection = Connection(google_account_email, google_account_password) accounts = connection.get_accounts() # aqui estan todas mis cuentas #print accounts[2].profile_id # aqui saque uno de mis cuentas para saber que hay enn esa lista dir(accounts[2]) y asi conoces mas aparte del profile_id # accounts[2].profile_id esto me devolvio 23492578 # para entender mejor https://github.com/clintecker/python-googleanalytics/blob/master/USAGE.md # a mi pagina con id 23492578 le voy a calcular unos analytics account = connection.get_account(accounts[0].profile_id) #data = account.get_data(start_date, end_date, metrics=['visits'],dimensions=['city']) # tire el querys filters = [] data_source = account.get_data(start_date=start_date, end_date=end_date, dimensions=['source'], metrics=[ 'visits', ], sort=[ '-visits', ], filters=filters) data_source = map( lambda source: { 'nombre': source[0][0], 'valor': source[1][0] }, data_source.list) ###################################################################################################################################################### data_city = account.get_data(start_date, end_date, metrics=['visits'], dimensions=['city'], sort=[ '-visits', ]) # tire el querys data_city = map( lambda source: { 'nombre': source[0][0], 'valor': source[1][0] }, data_city.list) return data_city, data_source except: return [], []
def get_metadata(): connection = Connection(settings.OPPS_GANALYTICS_ACCOUNT, settings.OPPS_GANALYTICS_PASSWORD, settings.OPPS_GANALYTICS_APIKEY) query = Query.objects.filter(date_available__lte=timezone.now(), published=True) for q in query: account = connection.get_account('{0}'.format(q.account.profile_id)) filters = [[f.filter.field, f.filter.operator, f.filter.expression, f.filter.combined or ''] for f in QueuryFilter.objects.filter(query=query)] start_date = datetime.date.today() if q.start_date: start_date = datetime.date(q.start_date.year, q.start_date.month, q.start_date.day) end_date = datetime.date.today() if q.end_date: end_date = datetime.date(q.end_date.year, q.end_date.month, q.end_date.day) metrics = ['pageviews', 'timeOnPage', 'entrances'] dimensions = ['pageTitle', 'pagePath'] data = [] count_data = len(data) while count_data == 0: data = account.get_data(start_date, end_date, metrics=metrics, dimensions=dimensions, filters=filters, max_results=1000, sort=['-pageviews']) start_date -= datetime.timedelta(days=1) end_date -= datetime.timedelta(days=1) count_data = len(data) for row in data.list: report, create = Report.objects.get_or_create(url=row[0][1]) if report: report.pageview = row[1][0] report.timeonpage = row[1][1] report.entrances = row[1][2] report.save()
def get_accounts(): connection = Connection(settings.OPPS_GANALYTICS_ACCOUNT, settings.OPPS_GANALYTICS_PASSWORD, settings.OPPS_GANALYTICS_APIKEY) accounts = connection.get_accounts() for a in accounts: obj, create = Account.objects.get_or_create(profile_id=a.profile_id, account_id=a.account_id, account_name=a.account_name, title=a.title) if not create: obj.account_id = a.account_id obj.account_name = a.account_name obj.title = a.title obj.save()
def update_ga(): connection = Connection('*****@*****.**', 'xiaofan_andc') #live_ads = Ad.objects.filter(start_day__lte=yesterday, end_day__gte=yesterday).exclude(ga_pid='') live_ads = Ad.objects.filter(ga_pid='35636139') if list(live_ads) == []: print 'ga no ads' return for ad in live_ads: account = connection.get_account(ad.ga_pid) try_times = 3 i = 0 while(i<try_times): try: data = account.get_data(yesterday, yesterday, metrics=['visits','pageviews','timeOnSite', 'bounces', 'entrances'], dimensions=['source']) break except: continue for d in data.list: tracking_code = d[0][0] visits = d[1][0] pageviews = d[1][1] timeOnsite = d[1][2] bounces = d[1][3] entrances = d[1][4] if len(tracking_code) == 5 and tracking_code.isdigit(): try: flight = Flight.objects.get(DE_ad_id = tracking_code) except Flight.DoesNotExist: continue try: ub = UserBehaviour.objects.get(tracking_code = tracking_code,startdate=yesterday,level='ga') except UserBehaviour.DoesNotExist: ub = UserBehaviour(startdate=yesterday,tracking_code= tracking_code,\ pv=pageviews, visits=visits,time_onsite=timeOnsite, bounces=int(bounces), entrances=int(entrances), level='ga') ub.save()
def update_ga_campaign(ga_id): connection = Connection('*****@*****.**', 'xiaofan_andc') account = connection.get_account(ga_id) live_ad = Ad.objects.get(ga_pid=ga_id) days = (live_ad.end_day - live_ad.start_day).days for n in range(days): day = live_ad.start_day + datetime.timedelta(days=n) try_times = 3 i = 0 while(i<try_times): try: data = account.get_data(day, day, metrics=['visits','pageviews','timeOnSite', 'bounces', 'entrances'], dimensions=['source']) break except: continue for d in data.list: tracking_code = d[0][0] visits = d[1][0] pageviews = d[1][1] timeOnsite = d[1][2] bounces = d[1][3] entrances = d[1][4] if len(tracking_code) == 5 and tracking_code.isdigit(): try: flight = Flight.objects.get(DE_ad_id = tracking_code) except Flight.DoesNotExist: continue try: ub = UserBehaviour.objects.get(tracking_code = tracking_code,startdate=day,level='ga') except UserBehaviour.DoesNotExist: ub = UserBehaviour(startdate=day, tracking_code= tracking_code,\ pv=pageviews, visits=visits,time_onsite=timeOnsite, bounces=int(bounces), entrances=int(entrances), level='ga') ub.save()
from django.core.management import setup_environ from django.core.management import execute_manager import settings setup_environ(settings) from gazjango.popular.models import StoryRank from gazjango.articles.models.stories import Article # should be moved to settings when everything works username = '******' password = '******' account = '12436739' connection = Connection(username, password) account = connection.get_account(account) end_date = datetime.date.today() start_date = datetime.date.today() - datetime.timedelta(days=60) results = account.get_data(start_date=start_date, end_date=end_date, dimensions=[ 'pagepath', ], metrics=[ 'pageviews', ]) results = results.list
def get_analytics_connection(): connection = Connection('*****@*****.**', 's0d4!SODA@') #connection = Connection('email', 'senha') account = connection.get_accounts()[3] return account
# # It uses a python googleanalytics lib # You can download it at: # http://tinyurl.com/python-googleanalytics # # By Johann Vivot # # Example to get Accounts from googleanalytics import Connection connection = Connection('*****@*****.**', 'xxxx') accounts = connection.get_accounts() # Example to get Pageviews account = connection.get_account('9060294') start_date = datetime.date(2011, 10, 21) end_date = datetime.date(2011, 10, 21) data = account.get_data(start_date, end_date, metrics=['pageviews']) data.list
if datedelta: dates += [ from_date + datetime.timedelta(days=r) for r in range(1, datedelta.days + 1) ] if configfile: try: config = json.load(open(configfile, 'r')) except Exception, e: raise CommandError( "Couldn't parse configfile '{0}'. Make sure it exists and is the correct format.\n" .format(configfile)) else: config = json.load(open(settings.GA_CONFIG, 'r')) conn = Connection(settings.GA_EMAIL, settings.GA_PASSWORD) for aday in dates: start = datetime.datetime.combine(aday.date(), datetime.time(0, 0, 0)) end = datetime.datetime.combine(aday.date(), datetime.time(23, 59, 59)) for project in config['projects']: metrics = None try: p = Project.objects.get(slug=project['slug']) self.stdout.write( "Fetching data for '{project}' on {date:%Y-%m-%d}\n". format(date=aday, project=p.name))
from django.core.management import setup_environ from django.core.management import execute_manager import settings setup_environ(settings) from gazjango.popular.models import StoryRank from gazjango.articles.models.stories import Article # should be moved to settings when everything works username = '******' password = '******' account = '12436739' connection = Connection(username,password) account = connection.get_account(account) end_date = datetime.date.today() start_date = datetime.date.today() - datetime.timedelta(days = 60) results = account.get_data(start_date=start_date,end_date=end_date,dimensions=['pagepath',],metrics=['pageviews',]) results = results.list for item in results: path = item[0][0] pageviews = item[1][0] url_list = path.split('/') if len(url_list) == 6: year = url_list[1] month = url_list[2]
self.stdout.write("from_date: {0}\n".format(from_date)) self.stdout.write("to_date : {0}\n".format(to_date)) dates = [from_date] if datedelta: dates += [from_date + datetime.timedelta(days=r) for r in range(1, datedelta.days+1)] if configfile: try: config = json.load(open(configfile, 'r')) except Exception, e: raise CommandError( "Couldn't parse configfile '{0}'. Make sure it exists and is the correct format.\n".format(configfile) ) else: config = json.load(open(settings.GA_CONFIG, 'r')) conn = Connection(settings.GA_EMAIL, settings.GA_PASSWORD) for aday in dates: start = datetime.datetime.combine(aday.date(), datetime.time(0, 0, 0)) end = datetime.datetime.combine(aday.date(), datetime.time(23, 59, 59)) for project in config['projects']: metrics = None try: p = Project.objects.get(slug=project['slug']) self.stdout.write("Fetching data for '{project}' on {date:%Y-%m-%d}\n".format(date=aday, project=p.name)) if project.has_key('profile_id'): acct = conn.get_account(project['profile_id']) # profile_id
def publish_extensions(self, handler): handler._out.write( '<%s><![CDATA[%s]]></%s>' % ("description", self.do_not_autooutput_description, "description")) handler._out.write('<%s><![CDATA[%s]]></%s>' % ("title", self.do_not_autooutput_title, "title")) # get the comman line args # SYNTAX is: python ga_api2mpp.py myusername\@gmail.com mypassssword 1234567 username = sys.argv[1] password = sys.argv[2] profileid = sys.argv[3] domain = 'http://healyourchurchwebsite.com/' # we'll need this for full urls & guids connection = Connection( username, password) # pass in id & pw as strings **if** not in config file # let's go ahead and create the request data variables # see the following for args: http://code.google.com/apis/analytics/docs/gdata/dimsmets/dimsmets.html maxresults = 50 account = connection.get_account(profileid) start_date = datetime.date(2010, 02, 01) end_date = datetime.date(2011, 02, 28) metrics = [ 'uniquePageviews', 'pageviews', 'timeOnPage', 'bounces', 'entrances', 'exits' ] dimensions = ['pageTitle', 'pagePath'] sorts = [ '-uniquePageviews', '-pageviews',
def get_metadata(verbose=False): if verbose: print('getting get_metadata') if not settings.OPPS_GANALYTICS_STATUS: return None connection = Connection(settings.OPPS_GANALYTICS_ACCOUNT, settings.OPPS_GANALYTICS_PASSWORD, settings.OPPS_GANALYTICS_APIKEY) if verbose: print(connection) default_site = Site.objects.get(pk=1) default_domain = 'http://' + default_site.domain query = Query.objects.filter(date_available__lte=timezone.now(), published=True) if verbose: print(query) for q in query: if verbose: print(q.name) account = connection.get_account('{0}'.format(q.account.profile_id)) if verbose: print(account) filters = [[f.filter.field, f.filter.operator, f.filter.expression, f.filter.combined or ''] for f in QueryFilter.objects.filter(query=q)] if verbose: print(filters) start_date = datetime.date.today() if q.start_date: start_date = datetime.date(q.start_date.year, q.start_date.month, q.start_date.day) end_date = datetime.date.today() if q.end_date: end_date = datetime.date(q.end_date.year, q.end_date.month, q.end_date.day) metrics = ['pageviews', 'timeOnPage', 'entrances'] dimensions = ['pageTitle', 'pagePath'] data = [] count_data = len(data) while count_data == 0: data = account.get_data(start_date, end_date, metrics=metrics, dimensions=dimensions, filters=filters, max_results=1000, sort=['-pageviews']) start_date -= datetime.timedelta(days=1) end_date -= datetime.timedelta(days=1) count_data = len(data) # print len(data.list) if verbose: print(len(data.list)) for row in data.list: if verbose: print("ROW:") print(row) try: url = row[0][1][:255] if verbose: print("URL:") print(url) if url.startswith('/'): url = default_domain + url if not url.startswith("http"): url = "http://" + url _url = urlparse(url) url = "{url.scheme}://{url.netloc}{url.path}".format(url=_url) if verbose: print(url) report, create = Report.objects.get_or_create(url=url) if verbose: print(report) print(create) if report: report.pageview = row[1][0] report.timeonpage = row[1][1] report.entrances = row[1][2] report.save() if verbose: print("CONTAINER:") print(report.container) except Exception as e: if verbose: print(str(e)) pass log_it("get_metadata")
self.description = NoOutput() # This disables the Py2GenRSS "Automatic" output of the description, which would be escaped. self.do_not_autooutput_title = self.title self.title = NoOutput() # This disables the Py2GenRSS "Automatic" output of the title, which would be escaped. PyRSS2Gen.RSSItem.publish(self, handler) def publish_extensions(self, handler): handler._out.write('<%s><![CDATA[%s]]></%s>' % ("description", self.do_not_autooutput_description, "description")) handler._out.write('<%s><![CDATA[%s]]></%s>' % ("title", self.do_not_autooutput_title, "title")) # get the comman line args # SYNTAX is: python ga_api2mpp.py myusername\@gmail.com mypassssword 1234567 username = sys.argv[1] password = sys.argv[2] profileid = sys.argv[3] domain = 'http://healyourchurchwebsite.com/' # we'll need this for full urls & guids connection = Connection(username, password) # pass in id & pw as strings **if** not in config file # let's go ahead and create the request data variables # see the following for args: http://code.google.com/apis/analytics/docs/gdata/dimsmets/dimsmets.html maxresults = 50 account = connection.get_account(profileid) start_date = datetime.date(2010, 02, 01) end_date = datetime.date(2011, 02, 28) metrics = ['uniquePageviews','pageviews','timeOnPage','bounces','entrances','exits'] dimensions = ['pageTitle','pagePath'] sorts = ['-uniquePageviews','-pageviews',] filters = [ ['uniquePageviews', '>=', '50',] ] # yeah, note this one is different # now let's plug in the variables into the get_data() call