Created
March 15, 2013 00:52
-
-
Save crowdmatt/5166634 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script><!-- required to getJSON from our server --> | |
<script src="https://datejs.googlecode.com/files/date.js"></script><!-- required to parse dates in ISO format --> | |
<script> | |
var device_tracking_type = "mac_address"; | |
var current_user_device_type = "android"; | |
var current_user_country_code = "ca"; | |
var current_user_mac_address = "11:11:11:11"; | |
var current_user_location = "37.8188,-122.4784"; | |
var current_impression_uuid = "mynetworkimpression-iowd23e823jsd094"; | |
var current_time = new Date(); | |
$.getJSON('http://deals.crowdmob.com/api/networks/{YOUR_CROWDMOB_PERMALINK}/ads.json', function(data) { | |
console.log("Returning the markup for the highest bid:", highestCpcCampaignMarkup(data)); | |
}); | |
// Returns the ad markup for the campaign that has the highest bid matching the current_user_device_type, in the country defined by current_user_country_code, at the current time, or null if there isn't a matching campaign. | |
function highestCpcCampaignMarkup(data) { | |
var campaigns = data.campaigns; | |
var lastHighestCampaign = null; | |
for (var i = 0; i < campaigns.length; ++i) { | |
var campaign = campaigns[i]; | |
// First, make sure we are between the right dates, right now (or the campaign doesn't end at a specific date), that isn't paused | |
if (Date.parse(campaign.starts_at) <= current_time && (campaign.ends_at == null || Date.parse(campaign.ends_at) > current_time) && campaign.paused_at == null) { | |
// Second, make sure that this campaign has a bid for the device type, in the region, and that that region is enabled | |
if (campaign.bids[current_user_device_type] && campaign.bids[current_user_device_type][current_user_country_code] && campaign.bids[current_user_device_type][current_user_country_code].enabled) { | |
// Finally, if the matching campaign has a higher bid than the last one found, mark it as found | |
if (lastHighestCampaign == null || lastHighestCampaign[current_user_device_type][current_user_country_code].network_cpi_bid < campaign.bids[current_user_device_type][current_user_country_code].network_cpi_bid) { | |
lastHighestCampaign = campaign; | |
} | |
} | |
} | |
} | |
if (lastHighestCampaign) { | |
var CAMPAIGN_TEMPLATE = "<a href='{{CLICKTHRU_URL}}'><img src='{{APPICON}}'/>{{APPTITLE}}<br/>{{APPDESCRIPTION}}</a>"; | |
// First replace the Click-Thru URL Parameters | |
var finalizedClickThruUrl = lastHighestCampaign.click_through_url.replace( | |
'DEVICE_TYPE', current_user_device_type | |
).replace( | |
'DEVICE_UUID_TYPE', device_tracking_type | |
).replace( | |
'DEVICE_UUID', current_user_mac_address | |
).replace( | |
'SOURCE_CLICK_UUID', current_impression_uuid | |
).replace( | |
'USER_LOCATION', current_user_location | |
); | |
return CAMPAIGN_TEMPLATE.replace( | |
"{{CLICKTHRU_URL}}", finalizedClickThruUrl | |
).replace( | |
"{{APPICON}}", lastHighestCampaign.app.icon_url | |
).replace( | |
"{{APPTITLE}}", lastHighestCampaign.app.name | |
).replace( | |
"{{APPDESCRIPTION}}", lastHighestCampaign.app.description | |
); | |
} | |
else { | |
return null; | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment