Guest User

Untitled

a guest
Jan 29th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. /**
  2. * Expenses Export Script
  3. */
  4.  
  5. const DB_URL = "jdbc:mysql://11.22.33.44:3306/dashboard";
  6. const DB_USER = "dashboard_www";
  7. const DB_PASS = "...";
  8.  
  9. /**
  10. * Main entry point for the script.
  11. */
  12. function main() {
  13. MccApp.accounts()
  14. .executeInParallel("processAccount", "afterProcessAccounts");
  15. }
  16.  
  17. /**
  18. * Get yesterday's results from the current account and return them as JSON.
  19. */
  20. function processAccount() {
  21. var account = AdWordsApp.currentAccount();
  22. var campaignIterator = AdWordsApp.campaigns()
  23. .withCondition("Status = ENABLED")
  24. .get();
  25.  
  26. // ...
  27.  
  28. while (campaignIterator.hasNext()) {
  29. var campaign = campaignIterator.next();
  30. var stats = campaign.getStatsFor("YESTERDAY");
  31. var cost = stats.getCost().toFixed(2) * 100;
  32.  
  33. // ...
  34. }
  35.  
  36. // There are multiple campaigns per airport. Essentially, this function sums
  37. // up the total cost of all campaigns associated with an airport. This is
  38. // done for every account.
  39.  
  40. return JSON.stringify(/* ... */);
  41. }
  42.  
  43. /**
  44. * Insert the results into the database.
  45. */
  46. function afterProcessAccounts(results) {
  47. var conn = Jdbc.getConnection(DB_URL, DB_USER, DB_PASS);
  48. var time = Jdbc.newTimestamp(new Date().getTime());
  49.  
  50. conn.setAutoCommit(false);
  51.  
  52. // Loop through the results ...
  53.  
  54. var query = "INSERT INTO statistics (site_id, airport, cost, created_at, updated_at) VALUES ((SELECT id FROM sites WHERE name = ?), ?, ?, ?, ?);";
  55. var stmt = conn.prepareStatement(query);
  56.  
  57. stmt.setString(1, /* site name */);
  58. stmt.setString(2, /* airport name */);
  59. stmt.setInt(3, /* total cost */);
  60. stmt.setTimestamp(4, time);
  61. stmt.setTimestamp(5, time);
  62. stmt.executeUpdate();
  63.  
  64. // End
  65.  
  66. conn.commit();
  67. conn.close();
  68. }
Add Comment
Please, Sign In to add comment