Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Javascript to Pull in Values from a Feed WordPress

  • SOLVED

We had a piece of script that was designed to bring in values from a Google Spreadsheet into an Adword account to dynamically update a value in the add

`// Current Promotions
var SPREADSHEET_URL = "https:www.domain.com";
var SHEET_NAME = "Sheet1";
var CAMPAIGN_NAME = "Campaign 1";
var AD_GROUP_NAME = "Ad Group 1";

/**
* The code to execute when running the script.
*/
function main() {
var adGroup = getAdGroup(AD_GROUP_NAME);
// If we couldn't find the Ad Group, log an error and halt.
if (adGroup) {
var rateData = getRateData();
updateAdParameterValues(adGroup, rateData);
} else {
Logger.log('Could not locate AdGroup with name ' + AD_GROUP_NAME);
}
}

function getAdGroup(name) {
var adGroupIterator = AdWordsApp.adGroups()
.withCondition('Name = "' + name + '"')
.withLimit(1)
.get();
if (adGroupIterator.hasNext()) {
return adGroupIterator.next();
}
}

function getRateData() {
var spreadsheet = SpreadsheetApp.openByUrl(SPREADSHEET_URL);
var sheet = spreadsheet.getSheetByName(SHEET_NAME);
var range = sheet.getRange(2, 1, sheet.getLastRow() - 1, sheet.getLastColumn());
var data = range.getValues();

// get the last values from the spreadsheet
var row = data[data.length-1];
var param1 = row[1];
var param2 = row[2];

// log returned data
Logger.log("rate: " + param1);
Logger.log("apr: " + param2);
return [param1, param2];
}

function updateAdParameterValues(adGroup, rateData) {
// Note that due to the way previewing works, this script won't update ad params for newly created keywords.
var keywordIter = adGroup.keywords().get();
while (keywordIter.hasNext()) {
var keyword = keywordIter.next();
keyword.setAdParam(1, rateData[0]);
keyword.setAdParam(2, rateData[1]);
}
}


I am attempting to reuse this but the feed is a live URL displaying rates for loans:

http://www.guaranteedrate.com/rateFeed.php

where the specific values are:

<_5YrArm>
<rate>
<lender>Guaranteed Rate</lender>
<rate>0.03125</rate>
<pointsDollars>0</pointsDollars>
<lenderFees>1310</lenderFees>
<totalFees>1310</totalFees>
<apr>0.02995</apr>
</rate>

Is there any way to have the Adwords script link to this URL feed and get these values?

Answers (2)

2014-01-14

John Cotton answers:

You're likely to get stuck with cross-domain security issues trying to get the javascript to read that data directly into the browser.

A far better and safer approach is to have your server do the work in PHP (get_file_contents or simplexml_load_file), cache it based on your own site needs and then have some WP ajax to query the values on your server from the javascript.

So your getRateDate function would be replaced by something like this:


function getRateData() {
var adGroup = getAdGroup(AD_GROUP_NAME);

if (adGroup) {
// See note below about ajax_url
jQuery.post( ad_group.ajax_url, { action: 'get_rate_data' }, function( response ) {
if( response.rates > 0 ) {
Logger.log("rate: " + param1);
Logger.log("apr: " + param2);
updateAdParameterValues(adGroup, [param1, param2]);
}
}, 'json' );
}


and your php:


function get_rate_data() {
$rate_data = array( 'rate': 22, 'apr' => 13 ); // Clearly this would need to reference the data from the XML feed

exit( json_encode( $rate_data ) );
}
add_action( 'wp_ajax_get_rate_data', 'get_rate_data' );
add_action( 'wp_ajax_nopriv_get_rate_data', 'get_rate_data' );


You don't need to hard code the ajax_url, just add it when you enqueue your script file:


wp_register_script( 'ad-group-rate', get_stylesheet_directory_uri() .'/js/my-javascript.js', 'jquery', 1.0, true );
wp_localize_script( 'ad-group-rate', 'ad_group', array( 'ajaxurl' => admin_url( 'admin-ajax.php') ) );


Greg Ellett comments:

awesome!

I think you are right, but the admin url would have to be placed in hardcoded because it is run inside the AdWords shell, not sure i could invoke the script.

if that is the case then the function would be


function getRateData() {

var adGroup = getAdGroup(AD_GROUP_NAME);



if (adGroup) {

// See note below about ajax_url

jQuery.post( 'admin-ajax.php', { action: 'get_rate_data' }, function( response ) {

if( response.rates > 0 ) {

Logger.log("rate: " + param1);

Logger.log("apr: " + param2);

updateAdParameterValues(adGroup, [param1, param2]);

}

}, 'json' );

}


Greg Ellett comments:

think you are right, but the admin url would have to be placed in hardcoded because it is run inside the AdWords shell, not sure i could invoke the script.

if that is the case then the function would be



function getRateData() {



var adGroup = getAdGroup(AD_GROUP_NAME);







if (adGroup) {



// See note below about ajax_url



jQuery.post( 'admin-ajax.php', { action: 'get_rate_data' }, function( response ) {



if( response.rates > 0 ) {



Logger.log("rate: " + param1);



Logger.log("apr: " + param2);



updateAdParameterValues(adGroup, [param1, param2]);



}



}, 'json' );



}

2014-01-14

Dbranes answers:

If you have this working from a Google Spreadsheet,
you might try to let the spreadsheet fetch the XML,
using for example:

=importXML("URL"; "xpath query")


Greg Ellett comments:

Yes, I have been able to ingest it that way. I just thought there would be a more direct route.