Skip to content

Instantly share code, notes, and snippets.

@engintekin
Created March 1, 2015 07:04
Show Gist options
  • Save engintekin/dce8d486d38b1d7fb000 to your computer and use it in GitHub Desktop.
Save engintekin/dce8d486d38b1d7fb000 to your computer and use it in GitHub Desktop.
customer.io java adapter
public class CustomerIOAdapter
{
private static final String CUSTOMER_IO_SITE_ID = "site-id";
private static final String CUSTOMER_IO_API_KEY = "api-key";
public void sendEvent(String id, String eventName, Map<String, Object> map) throws IOException
{
HttpPost request = new HttpPost("/api/v1/customers/" + id + "/events");
List<NameValuePair> postParameters = new ArrayList<>();
postParameters.add(new BasicNameValuePair("name", eventName));
for (String key : map.keySet())
{
postParameters.add(new BasicNameValuePair("data[" + key + "]", map.get(key).toString()));
}
request.setEntity(new UrlEncodedFormEntity(postParameters, "UTF-8"));
execute(request);
}
public void createOrUpdateCustomer(String id, Map<String, Object> map) throws IOException
{
HttpPut request = new HttpPut("/api/v1/customers/" + id);
List<NameValuePair> postParameters = new ArrayList<>();
for (String key : map.keySet())
{
postParameters.add(new BasicNameValuePair(key, map.get(key).toString()));
}
request.setEntity(new UrlEncodedFormEntity(postParameters, "UTF-8"));
execute(request);
}
public void deleteCustomer(String id) throws IOException
{
HttpDelete request = new HttpDelete("/api/v1/customers/" + id);
execute(request);
}
private CloseableHttpResponse execute(HttpRequestBase request) throws IOException
{
HttpHost target = new HttpHost("track.customer.io", 443, "https");
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(target.getHostName(), target.getPort()), new UsernamePasswordCredentials(CUSTOMER_IO_SITE_ID, CUSTOMER_IO_API_KEY));
CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
CloseableHttpResponse response = httpclient.execute(target, request);
EntityUtils.consume(response.getEntity());
response.close();
httpclient.close();
System.out.println(response);
return response;
}
}
public class CustomerIOUsage
{
public static void main(String[] args)
{
createOrUpdateSomeUser();
sendSomeEvent();
}
public static void createOrUpdateSomeUser()
{
Map<String, Object> params = new HashMap<>();
params.put("email", "someemail@mail");
params.put("created_at", new DateTime().getMillis() / 1000L);
params.put("unsubscribed", !Email.isValid("someemail@mail")); //you can unsubscribe a user if the mail is invalid
params.put("[customProperty1]", "custom value 1");
params.put("[customProperty2]", "custom value 2");
try
{
new CustomerIOAdapter().createOrUpdateCustomer("[yourApplicationUserId]", params);
}
catch (IOException e)
{
e.printStackTrace();
}
}
public static void sendSomeEvent()
{
private Map<String, Object> params = new HashMap<>();
params.put("[customParam1]", "custom value 1");
params.put("[customParam2]", "custom value 2");
new CustomerIOAdapter().sendEvent("[yourApplicationUserId]", "[yourEventName]", params);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment