Skip to content

Instantly share code, notes, and snippets.

@sphvn
Last active October 4, 2016 07:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sphvn/cbfda6e6b506b478f7b7892b783c1c3a to your computer and use it in GitHub Desktop.
Save sphvn/cbfda6e6b506b478f7b7892b783c1c3a to your computer and use it in GitHub Desktop.
// FB curl example
// curl \
// -F 'pixel_id=<PIXEL_ID>' \
// -F 'name=My New Website Custom Audience' \
// -F 'subtype=WEBSITE' \
// -F 'retention_days=15' \
// -F 'rule={"url":{"i_contains":"shoes"}}' \
// -F 'prefill=1' \
// -F 'access_token=<ACCESS_TOKEN>' \
// https://graph.facebook.com/v2.7/act_<AD_ACCOUNT_ID>/customaudiences
(function(){
var req = new XMLHttpRequest();
var pathparts = window.location.pathname.split('/');
var endpath = pathparts[pathparts.length-1];
var kvps = [
{"pixel_id" : "<PIXEL_ID>"}
, {"name" : endpath}
, {"subtype" : "WEBSITE"}
, {"retention_days" : "15"}
, {"rule" : "{\"url\":{\"i_contains\":\"" + endpath + "\"}}"}
, {"prefill" : "1"}
, {"access_token" : "<ACCESS_TOKEN>"}
];
var toStrings = function(obj) {
return Object.keys(obj).map(function (key) {
return key + '=' + obj[key];
});
}
var toQuery = function(pv, cv, ci, arr) {
return pv + '&' + cv
}
var post = kvps.map(toStrings).reduce(toQuery);
req.open("POST", "https://graph.facebook.com/v2.7/act_<AD_ACCOUNT_ID>/customaudiences", true);
req.onreadystatechange = function () {
if (req.readyState != 4 || req.status != 200) return;
console.log("unable to post");
};
req.send(post);
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment