Last active
October 4, 2016 07:26
-
-
Save sphvn/cbfda6e6b506b478f7b7892b783c1c3a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// 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