Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.25 KB | None | 0 0
  1. import argparse
  2. import sys
  3. import csv
  4. import string
  5. import ssl
  6.  
  7. from apiclient.errors import HttpError
  8. from apiclient import sample_tools
  9. from oauth2client.client import AccessTokenRefreshError
  10. from datetime import timedelta, date, datetime
  11.  
  12. class SampledDataError(Exception): pass
  13.  
  14.  
  15. def main(argv, request_list):
  16.   # Authenticate and construct service.
  17.   service, flags = sample_tools.init(
  18.       argv, 'analytics', 'v3', __doc__, __file__,
  19.       scope='https://www.googleapis.com/auth/analytics.readonly')
  20.  
  21.   # Try to make a request to the API. Print the results or handle errors.
  22.   try:
  23.     profile_id = profile_ids[profile]
  24.     if not profile_id:
  25.       print 'Could not find a valid profile for this user.'
  26.     else:
  27.       for start_date, end_date in date_ranges:
  28. #        limit = ga_query(service, profile_id, 0,
  29. #                                 start_date, end_date).get('totalResults')
  30. #        for pag_index in xrange(0, limit, 10000):
  31.         for i in range(len(request_list)):
  32.  
  33.           results = ga_query(service, profile_id,
  34.                                      start_date, end_date, request_list[i]['segment'])
  35.  
  36.           print_results(results, start_date, end_date, request_list[i]['name'],i)
  37.  
  38.   except TypeError, error:    
  39.     # Handle errors in constructing a query.
  40.     print ('There was an error in constructing your query : %s' % error)
  41.  
  42.   except HttpError, error:
  43.     # Handle API errors.
  44.     print ('Arg, there was an API error : %s : %s' %
  45.            (error.resp.status, error._get_reason()))
  46.  
  47.   except AccessTokenRefreshError:
  48.     # Handle Auth errors.
  49.     print ('The credentials have been revoked or expired, please re-run '
  50.            'the application to re-authorize')
  51.  
  52.   except SampledDataError:
  53.     # force an error if ever a query returns data that is sampled!
  54.     print ('Error: Query contains sampled data!')
  55.    
  56.   except ssl.SSLError, error:
  57.     print error
  58.  
  59.  
  60.  
  61. def ga_query(service, profile_id, start_date, end_date, ga_segment):
  62.   print ga_segment
  63.  
  64.   return service.data().ga().get(
  65.       ids='ga:' + profile_id,
  66.       start_date=start_date,
  67.       end_date=end_date,
  68.       metrics='ga:sessions',
  69.       dimensions='ga:date,ga:hostname,ga:deviceCategory',
  70.       segment=ga_segment,
  71.       filters='ga:hostname=~^www.(aeg.(be|co.uk|com.es|de|nl)|electrolux.(ch|fr|it|pl|ru|se))$',
  72.       samplingLevel='HIGHER_PRECISION').execute()
  73.      
  74.  
  75. def print_results(results, start_date, end_date, request_name,i):
  76.   """Prints out the results.
  77.  
  78.  This prints out the profile name, the column headers, and all the rows of
  79.  data.
  80.  
  81.  Args:
  82.    results: The response returned from the Core Reporting API.
  83.  """
  84.  
  85.   # New write header
  86. #  if i == 0:
  87. #    if (start_date, end_date) == date_ranges[0]:
  88. #      print 'Profile Name: %s' % results.get('profileInfo').get('profileName')
  89. #      columnHeaders = results.get('columnHeaders')
  90. #      cleanHeaders = []
  91. #
  92. #      cleanHeaders = [str(h['name']) for h in columnHeaders]
  93. #      cleanHeaders.append('Metric')
  94. #
  95. #      writer.writerow(cleanHeaders)
  96.   print 'Now pulling data from %s to %s.' %(start_date, end_date)
  97.   current_date = datetime.strptime(start_date, "%Y-%m-%d").strftime("%d/%m/%Y")
  98.  
  99.  
  100.   # Print data table.
  101.   if results.get('rows', []):
  102.     print results.get('rows')
  103.     for row in results.get('rows'):
  104.       row.append(request_name)
  105.       for i in range(len(row)):
  106.        
  107.         old, new = row[i], str()
  108.         for s in old:
  109.           new += s if s in string.printable else ''
  110.         row[i] = new
  111.       row.append(str(current_date))
  112.       writer.writerow(row)
  113.  
  114.   else:
  115.     print 'No Rows Found'
  116.  
  117.   limit = results.get('totalResults')
  118.   return None
  119.  
  120.  
  121. ##profile_ids = profile_ids = {'My Profile 1':  '1234567',
  122.                              #'My Profile 2':  '1234568',
  123.                  #'My Profile 3':  '1234569',
  124.                              #'My Profile 4':  '1234561'}
  125.  
  126. # Uncomment this line & replace with 'profile name': 'id' to query a single profile
  127. # Delete or comment out this line to loop over multiple profiles.
  128.  
  129. profile_ids = {'T1_All_Sites':  '180255716'}#{'ELUX Sweden': '136792100','ELUX Italy': '136785306','ELUX Russia': '136854754','ELUX Poland': '136756525','ELUX France': '136781829','ELUX Switzerland': '136846376','AEG UK':'136678083','AEG Belgium': '136774720','AEG Netherlands': '136778122','AEG Germany': '136784414','AEG Spain': '136756133'}
  130.  
  131.  
  132. #{'T1_All_Sites':  '180255716'}{'Fake Rollup Core Goals':  '148389067'}
  133.  
  134.  
  135.  
  136. def daterange(start_date, end_date):
  137.     for n in range(int ((end_date - start_date).days)):
  138.         yield start_date + timedelta(n)
  139. dates_list = []
  140. start_date = date(2018, 10, 24)
  141. end_date = date(2019, 6, 17)
  142. for single_date in daterange(start_date, end_date):
  143.     add_date = single_date.strftime("%Y-%m-%d")
  144.     datetuple = (add_date, add_date)
  145.     dates_list.append(datetuple)
  146. print dates_list
  147.  
  148.  
  149. date_ranges = dates_list ##,
  150.                # ('2015-10-01',
  151.                # '2015-10-31'),
  152.                # ('2015-11-01',
  153.                 # '2015-11-30',
  154.                # ('2015-12-01',
  155.                # '2015-12-31')]
  156.            
  157. request_list = [{"name":"start_Yesterday","segment":"sessions::condition::ga:pagePath=~(/mypages/(product-registration|register-a-product)/)|(/productregistration/start)"},
  158. {"name":"complete_Yesterday","segment":"sessions::condition::ga:pagePath=~(/mypages/(product-registration|register-a-product)/confirmation)|(/productregistration/personal_details)"},
  159. {"name":"product-search_Yesterday","segment":"sessions::condition::ga:pagePath=~(/mypages/(product-registration|register-a-product)/(.*)search)"},
  160. {"name":"product-details_Yesterday","segment":"sessions::condition::ga:pagePath=~(/mypages/(product-registration|register-a-product)/(.*)details)"},
  161. {"name":"incentive_Yesterday","segment":"sessions::condition::ga:pagePath=~(/mypages/(product-registration|register-a-product)/(.*)incentive)"},
  162. {"name":"loginSuccess_Yesterday","segment":"sessions::condition::ga:pagePath=~(/mypages/(product-registration|register-a-product)/)|(/productregistration/start);ga:pagePath=~/mypages(.*)loginStatus=True"},
  163. {"name":"createAccountSuccess_Yesterday","segment":"sessions::condition::ga:pagePath=~(/mypages/(product-registration|register-a-product)/)|(/productregistration/start);ga:pagePath=~/mypages(.*)createStatus=True"},
  164. {"name":"prod-search-start_Yesterday","segment":"sessions::condition::ga:eventAction=~click(.*)product(.*)box"},
  165. {"name":"prod-search-btn_Yesterday","segment":"sessions::condition::ga:eventAction==ProductSearchBtn"},
  166. {"name":"complete+incentive_Yesterday","segment":"sessions::condition::ga:pagePath=~(/mypages/(product-registration|register-a-product)/confirmation)|(/productregistration/personal_details);ga:pagePath=~(/mypages/(product-registration|register-a-product)/(.*)incentive)"},
  167. {"name":"product-details+incentive_Yesterday","segment":"sessions::condition::ga:pagePath=~(/mypages/(product-registration|register-a-product)/(.*)details);ga:pagePath=~(/mypages/(product-registration|register-a-product)/(.*)incentive)"},
  168. {"name":"loginStart_Yesterday","segment":"sessions::condition::ga:pagePath=~(/mypages/login/default)|((/mypages/(product-registration|register-a-product))(.*)/(login-attempt|auth))"},
  169. {"name":"createAccountStart_Yesterday","segment":"sessions::condition::ga:pagePath=~modal-create-account(.*)open"},
  170. {"name":"PS-search_Yesterday","segment":"sessions::condition::ga:pagePath=~/mypages/(product-registration|register-a-product)/search(.*)searchtype(.*)photo"},
  171. {"name":"PS-incentive_Yesterday","segment":"sessions::condition::ga:pagePath=~/mypages/(product-registration|register-a-product)/incentive(.*)searchtype(.*)photo"},
  172. {"name":"PS-auth_Yesterday","segment":"sessions::condition::ga:pagePath=~/mypages/(product-registration|register-a-product)/auth(.*)searchtype(.*)photo"},
  173. {"name":"PS-product-details_Yesterday","segment":"sessions::condition::ga:pagePath=~/mypages/(product-registration|register-a-product)/details(.*)searchtype(.*)photo"},
  174. {"name":"PS-complete_Yesterday","segment":"sessions::condition::ga:pagePath=~/mypages/(product-registration|register-a-product)/confirmation(.*)searchtype(.*)photo"},
  175. {"name":"MS-search_Yesterday","segment":"sessions::condition::ga:pagePath=~/mypages/(product-registration|register-a-product)/search(.*)searchtype(.*)input"},
  176. {"name":"MS-incentive_Yesterday","segment":"sessions::condition::ga:pagePath=~/mypages/(product-registration|register-a-product)/incentive(.*)searchtype(.*)input"},
  177. {"name":"MS-auth_Yesterday","segment":"sessions::condition::ga:pagePath=~/mypages/(product-registration|register-a-product)/auth(.*)searchtype(.*)input"},
  178. {"name":"MS-product-details_Yesterday","segment":"sessions::condition::ga:pagePath=~/mypages/(product-registration|register-a-product)/details(.*)searchtype(.*)input"},
  179. {"name":"MS-complete_Yesterday","segment":"sessions::condition::ga:pagePath=~/mypages/(product-registration|register-a-product)/confirmation(.*)searchtype(.*)input"},
  180. {"name":"MS-FoundSearches_Yesterday","segment":"sessions::condition::ga:pagePath=~(.*)searchtype(.*)input;ga:eventcategory==ProductRegistration;ga:eventaction==ProductSearchResults"},
  181. {"name":"MS-NotFoundSearches_Yesterday","segment":"sessions::condition::ga:pagePath=~(.*)searchtype(.*)input;ga:eventcategory==ProductRegistration;ga:eventaction==ProductSearchResults"},
  182. {"name":"PS-FoundSearches_Yesterday","segment":"sessions::condition::ga:pagePath=~(.*)searchtype(.*)photo;ga:eventcategory==ProductRegistration;ga:eventaction==ProductSearchResults"},
  183. {"name":"PS-NotFoundSearches_Yesterday","segment":"sessions::condition::ga:pagePath=~(.*)searchtype(.*)photo;ga:eventcategory==ProductRegistration;ga:eventaction==ProductSearchResults"},
  184. {"name":"PS-BtnClick_Yesterday","segment":"sessions::condition::ga:eventcategory==ProductRegistration;ga:eventaction==Click:PhotosearchUploadBtn"},
  185. {"name":"MS-BoxClick_Yesterday","segment":"sessions::condition::ga:eventcategory==ProductRegistration;ga:eventaction==Click:ProductSearchBox"},
  186. {"name":"PS-PNCFound_Yesterday","segment":"sessions::condition::ga:eventcategory==ProductRegistration;ga:eventaction==PhotoSearch;ga:eventlabel==PNCFound"},
  187. #{"name":"PS-PNCNotFound_Yesterday","segment":""},
  188. #{"name":"PS-ConfirmImage_Yesterday","segment":""},
  189. {"name":"PS-IncentiveAccept_Yesterday","segment":"sessions::condition::ga:eventcategory==ProductRegistration;ga:eventaction==Incentives;ga:eventLabel==Click:YESBtn;ga:pagePath=~incentive(.*)searchtype(.*)photo"},
  190. {"name":"PS-IncentiveDecline_Yesterday","segment":"sessions::condition::ga:eventcategory==ProductRegistration;ga:eventaction==Incentives;ga:eventLabel==Click:NOBtn;ga:pagePath=~incentive(.*)searchtype(.*)photo"},
  191. {"name":"MS-IncentiveAccept_Yesterday","segment":"sessions::condition::ga:eventcategory==ProductRegistration;ga:eventaction==Incentives;ga:eventLabel==Click:YESBtn;ga:pagePath=~incentive(.*)searchtype(.*)input"},
  192. {"name":"MS-IncentiveDecline_Yesterday","segment":"sessions::condition::ga:eventcategory==ProductRegistration;ga:eventaction==Incentives;ga:eventLabel==Click:NOBtn;ga:pagePath=~incentive(.*)searchtype(.*)input"},
  193. {"name":"PS-UploadReceipt_Yesterday","segment":"sessions::condition::ga:eventcategory==ProductRegistration;ga:eventaction==Click:UploadReceiptBtn;ga:pagePath=~details(.*)searchtype(.*)photo"},
  194. {"name":"MS-UploadReceipt_Yesterday","segment":"sessions::condition::ga:eventcategory==ProductRegistration;ga:eventaction==Click:UploadReceiptBtn;ga:pagePath=~details(.*)searchtype(.*)input"},
  195. {"name":"SubmissionError_Yesterday","segment":"sessions::condition::ga:eventcategory==ProductRegistration;ga:eventaction==Info:SubmissionError"},
  196. {"name":"Initial-Interaction-Any_Yesterday","segment":"sessions::condition::ga:eventcategory==ProductRegistration;ga:eventaction==Click:PhotosearchUploadBtn,ga:eventaction==Click:ProductSearchBox"}]
  197.  
  198. for profile in sorted(profile_ids):
  199.   path = './' #replace with path to your folder where csv file with data will be written
  200.   filename = 'post_purchase_prod_reg_june18.csv' #replace with your filename. Note %s is a placeholder variable and the profile name you specified on row 162 will be written here
  201.   with open(path + filename, 'at') as f:
  202.     writer = csv.writer(f, lineterminator='\n')
  203.     if __name__ == '__main__': main(sys.argv,request_list)
  204.   print "Profile done. Next profile..."
  205.  
  206. print "All profiles done."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement