Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. import csv
  2. from datetime import datetime, timedelta
  3. from google.cloud import bigquery
  4.  
  5.  
  6. def query_phone_contacts(start_date, end_date):
  7. """ Generate phone contacts query using BigQuery Standard SQL """
  8. return """
  9. SELECT
  10. PARSE_DATE('%Y%m%d', date) AS page_event_date,
  11. visitStartTime as timestamp,
  12. (SELECT value FROM UNNEST(h.customDimensions) WHERE index = 60) AS specialtyID,
  13. (SELECT value FROM UNNEST(h.customDimensions) WHERE index = 11) AS locationID,
  14. (SELECT value FROM UNNEST(h.customDimensions) WHERE index = 5) AS professionalID,
  15. (SELECT value FROM UNNEST(h.customDimensions) WHERE index = 42) AS session_id,
  16. (SELECT value FROM UNNEST(h.customDimensions) WHERE index = 61) AS persistent_session_id,
  17. (SELECT value FROM UNNEST(h.customDimensions) WHERE index = 9) AS sales_region_id,
  18. COUNT(DISTINCT(SELECT value FROM UNNEST(h.customDimensions) WHERE index = 134)) AS phoneAction,
  19. COUNT(*) AS altphoneAction
  20. FROM
  21. `seventh-circle-461.75615261.ga_sessions_*`,
  22. UNNEST(hits) AS h
  23. WHERE
  24. _TABLE_SUFFIX BETWEEN '{start_date}' AND '{end_date}'
  25. AND (
  26. (SELECT value FROM UNNEST(h.customDimensions) WHERE index = 97) like "%ad_click_type=phone%"
  27. OR
  28. (SELECT value FROM UNNEST(h.customDimensions) WHERE index = 97) like "%tel:%"
  29. )
  30. GROUP BY
  31. page_event_date,
  32. timestamp,
  33. specialtyID,
  34. locationID,
  35. professionalID,
  36. session_id,
  37. persistent_session_id,
  38. sales_region_id
  39. """.format(start_date=start_date, end_date=end_date)
  40.  
  41.  
  42. def get_phone_contacts(bq_client, query):
  43. """ Fetch phone contacts from GA sessions in BigQuery """
  44. query_job = bq_client.query(query, location='US')
  45. return query_job.result()
  46.  
  47.  
  48. def write_phone_contacts(data, filename):
  49. """ Write BigQuery results to CSV file """
  50. with open(filename, 'w') as fh:
  51. csv_writer = csv.writer(fh)
  52. for row in data:
  53. csv_writer.writerow(row.values())
  54. print('Total rows written to {}: {}'.format(filename, data.total_rows))
  55.  
  56.  
  57. def main():
  58. # set variables
  59. bq_client = bigquery.Client.from_service_account_json('service_account.json')
  60. yesterday = datetime.today() - timedelta(days=1)
  61. start_date = yesterday.strftime('%Y%m%d')
  62. # start_date = '20190315'
  63. # end_date = '20190315'
  64. end_date = yesterday.strftime('%Y%m%d')
  65.  
  66. # function calls
  67. phone_contacts_query = query_phone_contacts(start_date, end_date)
  68. phone_contacts_data = get_phone_contacts(bq_client, phone_contacts_query)
  69. write_phone_contacts(phone_contacts_data, filename='ga_sessions_phone_contacts.csv')
  70.  
  71.  
  72. if __name__ == '__main__':
  73. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement