Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// ====================================================================================================
//
// Cloud Code for analytics, write your code here to customize the GameSparks platform.
//
// For details of the GameSparks Cloud Code API see https://docs.gamesparks.com/
//
// ====================================================================================================
var baseURL = "https://api2.amplitude.com/2/httpapi"
//Required
var event_type = Spark.getData().event_type;
var api_key = Spark.getData().api_key;
//One of these required
var user_id = Spark.getData().user_id;
var device_id = Spark.getData().device_id;
//Others
var app_version = Spark.getData().app_version;
var os_name = Spark.getData().os_name;
var os_version = Spark.getData().os_version;
var device_model = Spark.getData().device_model;
var country = Spark.getData().country;
var language = Spark.getData().language;
function sendEvent(dataObject) {
var jsonRequestObject = JSON.stringify(requestObject);
var postRequest = Spark.getHttp(baseURL).setHeaders(
{
"Content-Type": "application/json"
}
).postForm(requestObject);
Spark.setScriptData("RESPONSE_RAW", '{"Status":"Success"}');
}
if ((user_id || device_id) && event_type && api_key) {
Spark.getLog().info( "request recieved" );
var dataObject = {};
dataObject.user_id = user_id;
dataObject.event_type = event_type;
if (device_id) {
dataObject.device_id = device_id;
}
if (user_id) {
dataObject.user_id = user_id;
}
if (app_version) {
dataObject.app_version = app_version;
}
if (os_name) {
dataObject.os_name = os_name;
}
if (os_version) {
dataObject.os_version = os_version;
}
if (device_model) {
dataObject.device_model = device_model;
}
if (country) {
dataObject.country = country;
}
if (language) {
dataObject.language = language;
}
var requestObject = {};
requestObject.api_key = api_key;
requestObject.events = [];
requestObject.event[0] = dataObject;
sendEvent(requestObject);
}else {
Spark.getLog().error("Required fields not present.");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment