-
-
Save seanwendt/ce6489f833cd0d70ca4dba5c0d560b80 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
// Chartboost node.js PIA S2S postback example | |
var data, descriptor, signature, options, req; | |
const CB_API_SECRET = "your_api_secret"; // CHARTBOOST API secret | |
const CB_API_TOKEN = "your_api_token"; // CHARTBOOST API token | |
const CB_HOST = "live.chartboost.com"; | |
const CB_PATH = "/event_service/v3/track"; | |
const CB_PORT = "443"; | |
var app_id = "5706c86b04b0166c04ee4881"; | |
var app_signature = "bb691366d0568ce74921d20465783c011dbe0e02"; | |
var ifa = "61109B4C-0F16-4FBC-82EC-5475313AE000"; // sample iOS IDFA | |
// var gaid = "617d2d50-ee7e-41e2-b375-bb04b47ebf5c"; // sample GAID | |
var product_id = "My Product ID"; // sample product_id | |
var price = 1.99; // sample price | |
var currency = "USD"; // sample currency code | |
var localized_title = "Gemz"; // sample title, optional | |
var localized_description = "Pretty Gemz"; // sample description, optional | |
function sendEvent(callback) { | |
// build the request body | |
data = JSON.stringify({ | |
"app_id": app_id, | |
"ifa": ifa, | |
"iap": { | |
"product_id": product_id, | |
"price": price, | |
"currency": currency, | |
"localized_title": localized_title, | |
"localized_description": localized_description, | |
}, | |
}); | |
// X-Chartboost-Signature is generated on *every request* using the following hashing algorithm | |
descriptor = "action:pia\n" + CB_API_SECRET + "\n" + app_signature + "\n" + data; | |
signature = crypto.createHash('sha256').update(descriptor).digest('hex'); | |
options = { | |
host: CB_HOST, | |
port: CB_PORT, | |
path: CB_PATH, | |
method: 'POST', | |
agent: false, | |
headers: { | |
'Content-Type': 'application/json', | |
'Content-Length': data.length, | |
'X-Chartboost-Token': CB_API_TOKEN, | |
'X-Chartboost-Signature': signature | |
} | |
}; | |
// Build the request using node https module | |
req = https.request(options, function(res) { | |
res.setEncoding('utf8'); | |
console.log("statusCode: ", res.statusCode); | |
res.on('data', function(chunk) { | |
console.log('Response: ' + chunk); | |
}); | |
}); | |
req.write(data); | |
req.end(); | |
callback(); | |
} | |
// Send it! | |
sendEvent( function() { | |
// print the data that's sent | |
console.log(data); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment