Can I email a csv file to myself

1,734 views
Skip to first unread message

parch

unread,
Aug 25, 2012, 12:53:36 AM8/25/12
to adwords...@googlegroups.com
After I run a keyword performance script. Can I email a csv file to myself?

Please let me know.

Thanks

Kevin Winter

unread,
Aug 28, 2012, 3:22:33 PM8/28/12
to adwords...@googlegroups.com
Hi,
  It depends on how you want to write your script.  If you want a CSV file instead of a spreadsheet, you could (for example) write the data you read to a list and have the email include the CSV.

For example:

function main() {
  var keywordIter = AdWordsApp.keywords()
      .withCondition('Impressions > 0')
      .forDateRange('LAST_7_DAYS')
      .get();
  var csv = 'Text,MatchType,Impressions,Clicks,Cost';
  while (keywordIter.hasNext()) {
    var keyword = keywordIter.next();
    var stats = keyword.getStatsFor('LAST_7_DAYS');
    var row = [keyword.getText(),
               keyword.getMatchType(),
               stats.getImpressions(),
               stats.getClicks(),
               stats.getCost()];
    csv += '\n' + row.join(',');
  }
  MailApp.sendEmail(
    'My Csv',
    '',
    {attachments:[{fileName: 'foo.csv', mimeType: 'text/csv', content: csv}]}
  );
}

- Kevin Winter
AdWords API Team

parch

unread,
Aug 30, 2012, 4:23:21 AM8/30/12
to adwords...@googlegroups.com
Thanks Kevin. This is exactly what I wanted. 

Another question. Is it possible to update a csv file on my computer instead of a Google spreadsheet?

Kevin Winter

unread,
Aug 30, 2012, 10:25:02 AM8/30/12
to adwords...@googlegroups.com
Hi,
  The short answer: no.

Long answer: if you had a web server hosted on your computer that allowed you to make an HTTP request to read and/or write to a file on your desktop, that could work.  Right now, your only integration option is HTTP requests (GET to pull a file from an external source, POST to send a file somewhere) or Google Spreadsheets.

You could also theoretically upload your CSV to Drive and have it converted to a Google Spreadsheet - and then interact with it via SpreadsheetApp and the google spreadsheet UI.

- Kevin Winter
AdWords Scripts Team

parch

unread,
Sep 3, 2012, 8:35:42 PM9/3/12
to adwords...@googlegroups.com
Thanks Kevin for your informative reply. 

LiefLokket

unread,
Feb 6, 2013, 5:09:06 PM2/6/13
to adwords...@googlegroups.com


How Would this script look if it were to only compile the entire cost of the account and sent it once per day.

Kevin Winter (AdWords Scripts Team)

unread,
Feb 7, 2013, 10:35:20 AM2/7/13
to adwords...@googlegroups.com
Hi,
  Well, this script has a different use case.  If you just want an email of the cost for your account each day, you could do something like this:

function main() {
  var totalCost = 0;
  var campaignIter = AdWordsApp.campaigns().get();
  while (campaignIter.hasNext()) {
    var campaign = campaignIter.next();
    var stats = campaign.getStatsFor('YESTERDAY');
    totalCost += stats.getCost();
  }
   MailApp.sendEmail('em...@example.com', "Yesterday's total cost", 'All campaigns: ' + totalCost);
}

Schedule it daily and this script will send yesterday's cost for all campaigns.

- Kevin Winter
AdWords Scripts Team

Robin

unread,
Apr 10, 2014, 9:43:39 AM4/10/14
to adwords...@googlegroups.com
Hi Kevin,

When I use your script, the total costs I get are about 10-15% lower then when I check it in my adwords account.
Why is there a difference?


Op donderdag 7 februari 2013 16:35:20 UTC+1 schreef Kevin Winter (AdWords Scripts Team):

Anash Oommen

unread,
Apr 21, 2014, 3:22:37 PM4/21/14
to adwords...@googlegroups.com
Hi Robin,

The values on reports depends on when you run the reports. In general, if you run the report at the same time, but see different values in UI and Scripts, let us know and we will investigate further.

Cheers,
Anash P. Oommen,
AdWords Scripts Team

Anuraag Khandelwal

unread,
Nov 26, 2014, 1:18:22 PM11/26/14
to adwords...@googlegroups.com
Hi Kevin,

I need the cost for each active campaign in an account every day and sent as an attached csv/spreadsheet in the email. How would that happen?

Thanks,
Anuraag

Matt Greenland

unread,
Dec 2, 2014, 8:24:45 PM12/2/14
to adwords...@googlegroups.com
Kevin's example above works for that, with a few changes. The basic idea of building up the file contents and sending them along as an attachment stays the same:

function main() {
 
var campaignIter = AdWordsApp.campaigns()
     
.withCondition('Status = ENABLED')
     
.get();
 
var csv = 'Campaign,Cost';

 
while (campaignIter.hasNext()) {
   
var campaign = campaignIter.next();
   
var stats = campaign.getStatsFor('YESTERDAY');

   
var row = [campaign.getName(),

               stats
.getCost()];
    csv
+= '\n' + row.join(',');
 
}
 
MailApp.sendEmail(
   
'em...@example.com',
   
'My Csv',
   
'',
   
{attachments:[{fileName: 'foo.csv', mimeType: 'text/csv', content: csv}]}
 
);
}


If all you need is a csv every day, another alternative might be to create a scheduled report in the AdWords UI.

Anash Oommen

unread,
Dec 3, 2014, 9:58:29 AM12/3/14
to adwords...@googlegroups.com
Hi Anuraag,

I updated your other thread.

Cheers,
Anash P. Oommen,
AdWords Scripts Team.

Peter Leshaw

unread,
Mar 10, 2017, 3:17:27 AM3/10/17
to AdWords Scripts Forum
This is a great resource. I'm trying to use this script to create a little more in depth report sent each day. It's essentially the same report, but with a few more pieces of data:

Campaign
Ad Group
Date
Clicks
Cost
Impressions
Conversions

I'm can almost figure it out, but I don't think I know the right names to references... Any help would be great.


Thanks!

Peter Leshaw

unread,
Mar 14, 2017, 11:33:06 AM3/14/17
to AdWords Scripts Forum
All right. I'm not sure if anyone's out there, but I got somewhere. I'm now just trying to get "Phone Conversions" as a row... Here's the script I have so far. Any thoughts on how to bring in Phone Call Conversions?


function main() {
  var campaignIter = AdWordsApp.campaigns()
      .withCondition('Status = ENABLED')
      .get();
  var csv = 'Account,Campaign,MonthDate,DayDate,YearDate,Impressions,Clicks,Cost,Conversions,ConversionRate';


  while (campaignIter.hasNext()) {
    var campaign = campaignIter.next();
    var account = AdWordsApp.currentAccount();

    var stats = campaign.getStatsFor('YESTERDAY');
    var today = new Date();
    var yesterday = new Date(today);
    yesterday.setDate(today.getDate() - 1); //setDate also supports negative values, which cause the month to rollover.
 
 

    var row = [account.getName(),
               campaign.getName(),
               yesterday.getMonth()+1,
               yesterday.getDate(),
               yesterday.getFullYear(),
               stats.getImpressions(),
               stats.getClicks(),
               stats.getCost(),
               stats.getConversions(),
               stats.getConversionRate()];

             
          
    csv += '\n' + row.join(',');
  }
  MailApp.sendEmail(
    'em...@emailaddress.com',
    'AdWords Export Test',
    '',
    {attachments:[{fileName: 'adwordsexport.csv', mimeType: 'text/csv', content: csv}]}
  );
}



Thanks!

Anthony Madrigal

unread,
Mar 14, 2017, 3:22:06 PM3/14/17
to AdWords Scripts Forum
Hi Peter,

You can use the Campaign Performance Report to get conversion data on your campaigns. You will want to use AllConversions since it includes phone call conversions. The field ExternalConversionSource will give you the source of which your conversions came from (i.e., phone calls).

Regards,
Anthony
AdWords Scripts Team
Reply all
Reply to author
Forward
0 new messages