How to run the keyword Performance Script?

140 views
Skip to first unread message

Raju

unread,
Mar 3, 2016, 9:07:54 AM3/3/16
to AdWords Scripts Forum

Iam run the keyword script from "keyword Performance code".Iam not change the any code & just iam change only Email Id,Spread Sheet URL and Date. then after we run the preview mode it shown "Missing ) after argument list. (line 76)
Dismiss
".

76 line:
 var spreadsheet = SpreadsheetApp.openByUrl(spreadsheet url,iam modify the spread sheet url ).copy(
      'Keyword Performance Report - ' +
      getDateStringInTimeZone('MMM dd, yyyy HH:mm:ss z'));


// Copyright 2015, Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @name Keyword Performance Report
 *
 * @overview The Keyword Performance Report script generates a Google
 *     Spreadsheet that contains keyword performance stats like quality score
 *     and average position of ads, as well as several distribution charts for
 *     an advertiser account. See
 *     https://developers.google.com/adwords/scripts/docs/solutions/keyword-performance
 *     for more details.
 *
 * @author AdWords Scripts Team [adwords...@googlegroups.com]
 *
 * @version 1.0.1
 *
 * @changelog
 * - version 1.0.1
 *   - Improvements to time zone handling.
 * - version 1.0
 *   - Released initial version.
 */


// Comma-separated list of recipients. Comment out to not send any emails.
var RECIPIENT_EMAIL = 'em...@example.com';

// URL of the default spreadsheet template. This should be a copy of http://goo.gl/cULxUX
var SPREADSHEET_URL = 'YOUR_SPREADSHEET_URL';

/**
 * This script computes a keyword performance report
 * and outputs it to a Google spreadsheet. The spreadsheet
 * url is logged and emailed.
 */

function main() {
 
Logger.log('Using template spreadsheet - %s.', SPREADSHEET_URL);
 
var spreadsheet = copySpreadsheet(SPREADSHEET_URL);
 
Logger.log('Generated new reporting spreadsheet %s based on the template ' +
     
'spreadsheet. The reporting data will be populated here.',
      spreadsheet
.getUrl());

 
var sheet = spreadsheet.getSheetByName('Report');
  sheet
.getRange(1, 2, 1, 1).setValue('Date');
  sheet
.getRange(1, 3, 1, 1).setValue(new Date());
  spreadsheet
.getRangeByName('account_id').setValue(AdWordsApp.currentAccount().
      getCustomerId
());
  outputQualityScoreData
(sheet);
  outputPositionData
(sheet);
 
Logger.log('Keyword performance report available at\n' +
      spreadsheet
.getUrl());
 
if (RECIPIENT_EMAIL) {
   
MailApp.sendEmail(RECIPIENT_EMAIL,
     
'Keyword Performance Report is ready',
      spreadsheet
.getUrl());
 
}
}

/**
 * Retrieves the spreadsheet identified by the URL.
 * @param {string} spreadsheetUrl The URL of the spreadsheet.
 * @return {SpreadSheet} The spreadsheet.
 */

function copySpreadsheet(spreadsheetUrl) {
 
var spreadsheet = SpreadsheetApp.openByUrl(spreadsheetUrl).copy(
     
'Keyword Performance Report - ' +
      getDateStringInTimeZone
('MMM dd, yyyy HH:mm:ss z'));

 
// Make sure the spreadsheet is using the account's timezone.
  spreadsheet
.setSpreadsheetTimeZone(AdWordsApp.currentAccount().getTimeZone());
 
return spreadsheet;
}

/**
 * Outputs Quality score related data to the spreadsheet
 * @param {Sheet} sheet The sheet to output to.
 */

function outputQualityScoreData(sheet) {
 
// Output header row
 
var header = [
   
'Quality Score',
   
'Num Keywords',
   
'Impressions',
   
'Clicks',
   
'CTR (%)',
   
'Cost'
 
];
  sheet
.getRange(3, 2, 1, 6).setValues([header]);

 
// Initialize
 
var qualityScoreMap = [];
 
for (i = 1; i <= 10; i++) {
    qualityScoreMap
[i] = {
      numKeywords
: 0,
      totalImpressions
: 0,
      totalClicks
: 0,
      totalCost
: 0.0
   
};
 
}

 
// Compute data
 
var keywordIterator = AdWordsApp.keywords()
     
.forDateRange('LAST_WEEK')
     
.withCondition('Impressions > 0')
     
.get();
 
while (keywordIterator.hasNext()) {
   
var keyword = keywordIterator.next();
   
var stats = keyword.getStatsFor('LAST_WEEK');
   
var data = qualityScoreMap[keyword.getQualityScore()];
   
if (data) {
      data
.numKeywords++;
      data
.totalImpressions += stats.getImpressions();
      data
.totalClicks += stats.getClicks();
      data
.totalCost += stats.getCost();
   
}
 
}

 
// Output data to spreadsheet
 
var rows = [];
 
for (var key in qualityScoreMap) {
   
var ctr = 0;
   
var cost = 0.0;
   
if (qualityScoreMap[key].numKeywords > 0) {
      ctr
= (qualityScoreMap[key].totalClicks /
        qualityScoreMap
[key].totalImpressions) * 100;
   
}
   
var row = [
      key
,
      qualityScoreMap
[key].numKeywords,
      qualityScoreMap
[key].totalImpressions,
      qualityScoreMap
[key].totalClicks,
      ctr
.toFixed(2),
      qualityScoreMap
[key].totalCost];
    rows
.push(row);
 
}
  sheet
.getRange(4, 2, rows.length, 6).setValues(rows);
}

/**
 * Outputs average position related data to the spreadsheet.
 * @param {Sheet} sheet The sheet to output to.
 */

function outputPositionData(sheet) {
 
// Output header row
  headerRow
= [];
 
var header = [
   
'Avg Position',
   
'Num Keywords',
   
'Impressions',
   
'Clicks',
   
'CTR (%)',
   
'Cost'
 
];
  headerRow
.push(header);
  sheet
.getRange(16, 2, 1, 6).setValues(headerRow);

 
// Initialize
 
var positionMap = [];
 
for (i = 1; i <= 12; i++) {
    positionMap
[i] = {
      numKeywords
: 0,
      totalImpressions
: 0,
      totalClicks
: 0,
      totalCost
: 0.0
   
};
 
}

 
// Compute data
 
var keywordIterator = AdWordsApp.keywords()
     
.forDateRange('LAST_WEEK')
     
.withCondition('Impressions > 0')
     
.get();
 
while (keywordIterator.hasNext()) {
   
var keyword = keywordIterator.next();
   
var stats = keyword.getStatsFor('LAST_WEEK');
   
if (stats.getAveragePosition() <= 11) {
     
var data = positionMap[Math.ceil(stats.getAveragePosition())];
   
} else {
     
// All positions greater than 11
     
var data = positionMap[12];
   
}
    data
.numKeywords++;
    data
.totalImpressions += stats.getImpressions();
    data
.totalClicks += stats.getClicks();
    data
.totalCost += stats.getCost();
 
}

 
// Output data to spreadsheet
 
var rows = [];
 
for (var key in positionMap) {
   
var ctr = 0;
   
var cost = 0.0;
   
if (positionMap[key].numKeywords > 0) {
      ctr
= (positionMap[key].totalClicks /
        positionMap
[key].totalImpressions) * 100;
   
}
   
var row = [
      key
<= 11 ? key - 1 + ' to ' + key : '>11',
      positionMap
[key].numKeywords,
      positionMap
[key].totalImpressions,
      positionMap
[key].totalClicks,
      ctr
.toFixed(2),
      positionMap
[key].totalCost
   
];
    rows
.push(row);
 
}
  sheet
.getRange(17, 2, rows.length, 6).setValues(rows);
}

/**
 * Produces a formatted string representing a given date in a given time zone.
 *
 * @param {string} format A format specifier for the string to be produced.
 * @param {date} date A date object. Defaults to the current date.
 * @param {string} timeZone A time zone. Defaults to the account's time zone.
 * @return {string} A formatted string of the given date in the given time zone.
 */

function getDateStringInTimeZone(format, date, timeZone) {
  date
= date || new Date();
  timeZone
= timeZone || AdWordsApp.currentAccount().getTimeZone();
 
return Utilities.formatDate(date, timeZone, format);
}

AdwordsScript.docx

Tyler Sidell (AdWords Scripts Team)

unread,
Mar 3, 2016, 10:26:26 AM3/3/16
to AdWords Scripts Forum
Hi Raju,

Are you replacing var SPREADSHEET_URL = 'YOUR_SPREADSHEET_URL'; with a copy of this template?

Example:

Thanks,
Tyler Sidell 
AdWords Scripts Team
Reply all
Reply to author
Forward
0 new messages