Skip to content

Instantly share code, notes, and snippets.

@mpjura
Last active Aug 29, 2015
Embed
What would you like to do?
AE
var aeConfig = {
//URL of the Audience Engine end point
endpoint: "engine.hdmtools.com/get_teaser_block.json.php",
//Array of "blocks" (lists of stories) created in Audience Engine
blocks: [
{
//Each block has a name
name: "Smart Tout?",
//The container of the module being populated by Audience Engine
//Ex: container: ".smart-tout"
container: "selector",
//This is a map that shows which json field value goes into which dom node
//Ex: { "title": ".smart-tout--title", "dek": ".smart-tout--dek" }
map: {
"field": "selector",
"field": "selector"
}
}
]
};
function audienceEngine( config ) {
var endpoint = config.endpoint;
var page_id, pepe_visitor;
//this should be the article id.. can probably get it from HRST object
page_id = 1234567;
//this is a cookie that suren sets.. change to a valid getCookie function call
pepe_visitor = getCookie("pepe_visitor");
$.each( config.blocks, function( i, block ) {
//Send the request to Audience Engine for each block
$.ajax({
url: config.endpoint,
data: {
block_code: block.name,
pepe_visitor: pepe_visitor,
page_id: page_id
},
crossDomain: true,
dataType: "json"
})
//Success callback populates the fields
.done(function( resp ){
var el = $( block.container ),
map = block.map,
items = resp.items;
$.each( resp.items, function( i, item ){
for ( var field in map ) {
if ( item[ field ] ) {
//if the field we want is in the response
el.find( map[ field ] ).html( item[ field ] );
} else {
//if the field we want isn't there, hide the element
el.find( map[ field ] ).hide();
}
}
});
})
//Error callback hides the module. We might not want to do this, idk.
.fail(function(){
$( block.container ).hide();
});
});
}
audienceEngine( aeConfig );
function getCookie(){}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment