Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Expenses Export Script
- */
- const DB_URL = "jdbc:mysql://11.22.33.44:3306/dashboard";
- const DB_USER = "dashboard_www";
- const DB_PASS = "...";
- /**
- * Main entry point for the script.
- */
- function main() {
- MccApp.accounts()
- .executeInParallel("processAccount", "afterProcessAccounts");
- }
- /**
- * Get yesterday's results from the current account and return them as JSON.
- */
- function processAccount() {
- var account = AdWordsApp.currentAccount();
- var campaignIterator = AdWordsApp.campaigns()
- .withCondition("Status = ENABLED")
- .get();
- // ...
- while (campaignIterator.hasNext()) {
- var campaign = campaignIterator.next();
- var stats = campaign.getStatsFor("YESTERDAY");
- var cost = stats.getCost().toFixed(2) * 100;
- // ...
- }
- // There are multiple campaigns per airport. Essentially, this function sums
- // up the total cost of all campaigns associated with an airport. This is
- // done for every account.
- return JSON.stringify(/* ... */);
- }
- /**
- * Insert the results into the database.
- */
- function afterProcessAccounts(results) {
- var conn = Jdbc.getConnection(DB_URL, DB_USER, DB_PASS);
- var time = Jdbc.newTimestamp(new Date().getTime());
- conn.setAutoCommit(false);
- // Loop through the results ...
- var query = "INSERT INTO statistics (site_id, airport, cost, created_at, updated_at) VALUES ((SELECT id FROM sites WHERE name = ?), ?, ?, ?, ?);";
- var stmt = conn.prepareStatement(query);
- stmt.setString(1, /* site name */);
- stmt.setString(2, /* airport name */);
- stmt.setInt(3, /* total cost */);
- stmt.setTimestamp(4, time);
- stmt.setTimestamp(5, time);
- stmt.executeUpdate();
- // End
- conn.commit();
- conn.close();
- }
Add Comment
Please, Sign In to add comment