Search code examples
javasparkpost

SparkPost - not receiving Click Events


So, I have an http endpoint for receiving different types of events from SparkPost (such as Delivery, Bounce, Complaint, Open Track, ...). Everything works just fine, but I don't get any posts about Click events. Here is what I've tried so far:

 private void sendEmail(String from, String[] recipients) throws SparkPostException {
    TransmissionWithRecipientArray transmission = new TransmissionWithRecipientArray();

    ArrayList<String> tags = new ArrayList<String>();
    tags.add("tag #1");
    tags.add("tag #2");
    // Populate Recipients
    List<RecipientAttributes> recipientArray = new ArrayList<RecipientAttributes>();
    for (String recipient : recipients) {
        RecipientAttributes recipientAttribs = new RecipientAttributes();
        recipientAttribs.setAddress(new AddressAttributes(recipient));
        recipientAttribs.setTags(tags);
        recipientArray.add(recipientAttribs);
    }
    transmission.setRecipientArray(recipientArray);

    // Populate Substitution Data
    Map<String, Object> substitutionData = new HashMap<String, Object>();
    substitutionData.put("link", "http://www.google.com?utm_campaign=test_campaign");


    OptionsAttributes optionsAttributes = new OptionsAttributes();
    optionsAttributes.setClickTracking(true); // THIS DOESN'T SEEM TO MAKE A DIFFERENCE
    optionsAttributes.setOpenTracking(true);


    transmission.setSubstitutionData(substitutionData);
    transmission.setOptions(optionsAttributes);
    transmission.setCampaignId("test_campaign");

    Map<String, String> metadata = new HashMap<String, String>();
    metadata.put("user_type", "test");
    transmission.setMetadata(metadata);
    transmission.setReturnPath("example@some-mail.com");
    // Populate Email Body
    TemplateContentAttributes contentAttributes = new TemplateContentAttributes();
    contentAttributes.setFrom(new AddressAttributes(from));
    contentAttributes.setSubject("Your subject content here.");
    contentAttributes.setText("Your Text content here.");

    contentAttributes.setHtml("<p>Your <b>HTML</b> content here. {{ link }}</p>");

    transmission.setContentAttributes(contentAttributes);

    transmission.setContentAttributes(contentAttributes);

    // Send the Email
    RestConnection connection = new RestConnection(this.client, getEndPoint());
    Response response = ResourceTransmissions.create(connection, 0, transmission);

    System.out.println("Transmission Response: " + response);
}

Solution

  • In order for the SparkPost template engine to only personalize http(s)? urls, and not wrap things like mailto:a@b.com, the scheme (http:// or https://) needs to be in the template, as opposed to in the substitution data. Here's an example:

    Template: This is a link to <a href="http://{{{myurl}}}">somewhere awesome</a>!

    Substitution Data: substitutionData.put("myurl", "www.google.com?utm_campaign=test_campaign");

    There are actually three changes here - the second one is using triple curlies {{{ instead of {{ double curlies, to avoid html-escaping the contents of the substitution variable. And the third is to put the url inside an anchor tag, since SparkPost won't wrap bare links.