Using Campaign Monitor with Codenvy

Use Codenvy Factory to instantly start working with a sample Campaign Monitor API project:


This tutorial will explain the details of integrating the Campaign Monitor API into Codenvy. Campaign Monitor API allows the integration of your application with Campaign Monitor to be able to create mailing lists, add subscribers to your mailing lists and then send the users well designed campaign newsletters. The explanation has been done with a very basic Java project that makes use of the Campaign Monitor API to explain some basic features provided by the API.

Register With Campaign Monitor

Before proceeding to the creation of the Java project, please register at https://signup.campaignmonitor.com/signup with your email id to create your account and generate the API key which will be used for the integration:

campaignmonitor-signup

On registering, you will receive an email asking for confirming your email address and after successful confirmation, you can see your API key at the ‘Account Settings’ section:

campaignmonitor-apikey

You can get more information about the Campaign Monitor API at
http://www.campaignmonitor.com/api/. There are different subsections providing information about various types of functionalities offered by the API such as creating mailing lists, adding subscribers, creating campaign newsletters, sending campaigns. Each subsection will detail the request URL required to invoke the specific API method that you can use in your integration process and the list of input parameters and the response fields.
There is also a Java wrapper available for the API at http://campaignmonitor.github.io/createsend-java/ which provides interface methods for all the API functionalities. Our sample Codenvy project uses the Java wrapper to explain the usage of the API.

Create a Project

Now, login to your Codenvy workspace and create a WAR project named as ‘CampaignMonitorSample’. This will have the following structure:
campaignmonitor-structure

Create pom.xml File

To make use of the Campaign Monitor API through the Java wrapper, please update the pom.xml to include the Java wrapper ‘createsend-java’ jar into your project as Maven dependency:

  <dependencies>
    <dependency>
      <groupId>com.createsend</groupId>
      <artifactId>createsend-java</artifactId>
      <version>4.0.1</version>
    </dependency>
  </dependencies>
  <repositories>
     <repository>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Maven Repository Switchboard</name>
      <url>http://repo1.maven.org/maven2</url>
     </repository>
  </repositories>

This will include this jar in your project as follows:
campaignmonitor-dependencies

Create Java Class

Now create a Java class named CampaignMonitorSample.java that will talk to the Campaign Monitor API via the Java wrapper and invoke various functionalities of the API. The Java class authenticates the API calls with the API key, creates a client, creates a mailing list for the client and adds a subscriber to the mailing list and then creates a campaign newsletters for the mailing list and then sends the campaign. All this is done inside the method execute() in the sample source Java class.

   public void execute() throws CreateSendException {
   
        ApiKeyAuthenticationDetails auth = new ApiKeyAuthenticationDetails(
            "23874632747823472374872347jhwhjhwe");
        Clients clientAPI = new Clients(auth);
        
        Client newClient = new Client();
        newClient.CompanyName = "abcpqr";
        newClient.Country = "United Kingdom";
        newClient.TimeZone = "(GMT) Coordinated Universal Time";
        
        
        newClient.ClientID = clientAPI.create(newClient);
        System.out.printf("Result of client create: %s\n", clientAPI.getClientID());
        
        Lists listAPI = new Lists(auth);
        Date subscribersFrom = new Date();
        
        List list = new List();
        list.Title = "Java API Test List";
        list.ConfirmedOptIn = false;
        list.UnsubscribeSetting = "OnlyThisList";
        listAPI.create(clientAPI.getClientID(), list);
        
        System.out.printf("Result of list create: %s\n", listAPI.getListID());
        System.out.printf("Result of list details: %s\n", listAPI.details());
        
        CustomFieldForCreate customField = new CustomFieldForCreate();
        customField.DataType = "Text";
        customField.FieldName = "Website";
        
        String key = listAPI.createCustomField(customField);
        
        Subscribers subscriberAPI = new Subscribers(auth, listAPI.getListID());
        
        SubscriberToAdd subscriber = new SubscriberToAdd();
        subscriber.Resubscribe = true;
        subscriber.EmailAddress = "abc@pqr.com";
        subscriber.Name = "Java Test Sub 1";
        subscriber.CustomFields = new CustomField[] {
            new CustomField()
        };
        subscriber.CustomFields[0].Key = key;
        subscriber.CustomFields[0].Value = "http://www.abcpqr.com";
        
        System.out.printf("Result of subscriber add: %s\n", subscriberAPI.add(subscriber));
        
        Campaigns campaignAPI = new Campaigns(auth);
        Date resultsAfter = new Date();
        
        CampaignForCreation newCampaign = new CampaignForCreation();
        newCampaign.FromEmail = "def@xyz.com";
        newCampaign.FromName = "Java Wrapper";
        newCampaign.HtmlUrl = URI.create(
            "http://www.campaignmonitor.com/uploads/templates/previews/template-1-left-sidebar/index.html");
        newCampaign.TextUrl = URI.create(
            "http://www.campaignmonitor.com/uploads/templates/previews/template-1-left-sidebar/textversion.txt");
        newCampaign.ListIDs = new String[] { listAPI.getListID() };
        newCampaign.Name = "API Testing Campaign: " + new Date();
        newCampaign.ReplyTo = "def@xyz.com";
        newCampaign.SegmentIDs = new String[0];
        newCampaign.Subject = "Java Wrapper Test: " + new Date();

        campaignAPI.create(clientAPI.getClientID(), newCampaign);
        System.out.printf("Result of campaign create: %s\n", campaignAPI.getCampaignID());
               
        Date scheduledDate = new Date();
        campaignAPI.send("def@xyz.com", scheduledDate);

        System.out.printf("Result of get campaign summary: %s\n", campaignAPI.summary());
    }

Create index.jsp File

Your Codenvy project will have an index.jsp which will invoke the Java class -

<%
    com.codenvy.CampaignMonitorSample campaignMonitorSample = new com.codenvy.CampaignMonitorSample();
    campaignMonitorSample.execute();
%>

Now you build and run your Codenvy project and the system out statements will be displayed in the browser thereby showing the successful completion of each action:

campaignmonitor-outputlogs

At the same time, you can check your Campaign Monitor account and you will see the mailing list and the campaign created and the details of the campaign. Also, the recipients will have received your campaign newsletter as well:

campaignmonitor-outputaccount1

campaignmonitor-outputaccount2

In a similar way, you can use other available API methods and integrate the Campaign Monitor API into your application to meet your application needs.