Is it possible to add the campaign monitor to a webform like you can add it to the user registration?

Comments

ssherriff’s picture

At the moment the module gives you the possibility to have a checkbox on the registration page and/or the contact page.

modulist’s picture

Both the contact page and the registration are pretty kludgey in their implementation. Webform allows you to change the URL, redirect page and a number of other features.

I was also under the impression that we could add various fields to match segments with this module.

rickvug’s picture

Category: support » feature

Since this functionality doesn't existing, I'm changing this to a feature request.

Michsk’s picture

i would also be intersted in this

dries arnolds’s picture

I'm also interested. It's a much requested feature that clients have.

Right now I'm filtering csv results from webform submissions and importing those manually in Campaign Monitor.

ssherriff’s picture

Assigned: Unassigned » ssherriff

Ok, as several people have asked for this, I will have a look at including this in the module.

ssherriff’s picture

With this feature request, could someone who has more experience using webforms help define the actual feature request a bit more. Did you just want the capability to add a newsletter or newsletter checkboxes to the bottom of the form, or did you want some other more complicated way of actually having a specific 'campaignmonitor' field that can be added to forms. I'll have to do a bit more research to get this one right, so any information someone might want to provide will definitely help.

Cheers

rickvug’s picture

@ssherriff I see a simple checkbox as being sufficient. The more challenging aspect of the integration will be selecting which fields contain the name and email address to be sent to Campaign Monitor. IMO, any further webform related feature requests should be filed as separate issues at a later date.

iancawthorne’s picture

The captcha module might be worth looking at as this adds itself to any form.

epersonae2’s picture

Checkbox(es) to indicate which list(s) to subscribe, and a way to indicate which fields should also be sent would be a good thing.

BTW, for those looking for a right now implementation, I figured out a (somewhat cludgy) way to do it with Additional Processing. Let me know if there's interest, and I'd be happy to share! :)

cnolle’s picture

Hey,

I would definitely be interested, been trying to work this out for the last couple of days with no luck.

-- Christian

rickvug’s picture

@epersonae2 You are welcome to share your solution, no matter how cludgy it is.

epersonae2’s picture

OK, here's what I've got. It's not on a production site yet, so I can't say if there's anything weird that might happen with more visitors.

This code, including the PHP tags, goes in Additional Processing. I used the Create a subscribe form feature in Campaign Monitor to get the value for the field names and the url to submit.

//get the email address and any additional fields
$content['email-field-name-from-campaignmonitor'] = $form['submitted']['your_email_field']['#value'];
$content['additional-field-name'] = $form['submitted']['your_additional_field']['#value'];

//send data to the form
$url = 'http://yourcampaignmonitorsubscribeformurl.com';
$headers = array('Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8');
$data = http_build_query($content, '', '&');
$req = drupal_http_request($url, $headers, 'POST', $data);

I hope this is helpful!

cnolle’s picture

@epersonae2 Thanks for that. Works wonderfully. Good stufF.

rjperry25’s picture

From a marketeers perspective (I'm not a coder) but I use Drupal for my clients.

I use the webforms for capturing visitors data. If I could send the following data

name, email, tel - the usual stuff

plus and this is what I really could do with - is a menu list that can be used to segment the user in to a subscibers list within Campaign Monitor. You can identify each subscriber list by using the API Subscriber List ID (found in the admin area of CM)

Not sure how it can be done but you'll need to replicate the subscribers list in Campaign Monitor with the list in webform.

This would be very interesting and I could possibly fund some development time. If anyone is interested.

modulist’s picture

This would imply being able to pass the value of webform or CCK fields to Campaign Monitor.

I agree, it's a really importanrt way of adding subscribers to the proper segments.

nickorr’s picture

This is something I'm looking for as well.

All I'd want is an option to be able to add them to the existing webforms. In terms of UI, I'd have the same UI you've already got for Contact and Registration checkboxes, but add a list of existing webforms to those 2 items. So you'd just turn it on or off there. Make sense?

Thanks,
Nick

ssherriff’s picture

Yeah, I think it is harder then that though because people will probably have form elements on their webform that they want to use to fill custom fields (including the Name field). So really I'd have to possibly get the available webforms, plus the fields available and enable mapping. That might be complicated.

Any hints/tips by people who have experience with webforms are greatly appreciated at this point.

Cheers,
Stephanie

Michsk’s picture

Maybe team up with http://drupal.org/project/emf there's some great mapping options in there.

deanloh’s picture

Nice work on this! I used Webform to create a contact form and added this code to Additional Processing field. Problem is, every time someone submitted the form, his email address is added to Campaign Monitor. So I added a checkbox to let user to check if they want to subscribe to mailing list. The field name of the checkbox is "subscribe" and value is "opt_in", therefore I modified the code this way:

<?php
if ($form['submitted']['subscribe']['#value'] = "opt_in") {
//get the email address and any additional fields
$content['cm-xxxxxx-xxxxxx'] = $form['submitted']['email']['#value'];
$content['cm-xxxx'] = $form['submitted']['name']['#value'];

//send data to the form
$url = 'http://xxxxxx.xxx/t/r/s/itijyd/';
$headers = array('Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8');
$data = http_build_query($content, '', '&');
$req = drupal_http_request($url, $headers, 'POST', $data);
}
?>

Somehow this didn't seem to work, the form still sent all data to CM regardless the checkbox is checked or not. What do you think I have missed here?

deanloh’s picture

I had eventually found the answer myself, instead of:
if ($form['submitted']['subscribe']['#value'] = "opt_in") {
I changed it to:
if ($form['submitted']['subscribe']['#value']) {
and problem solved!

So the full code is:

<?php
if ($form['submitted']['subscribe']['#value']) { //replace subscribe with your checkbox field name
//get the email address and any additional fields
$content['cm-xxxxxx-xxxxxx'] = $form['submitted']['email']['#value']; //replace cm-xxxxxx-xxxxxx with your Campaign Monitor email field name
$content['cm-xxxx'] = $form['submitted']['name']['#value']; //replace cm-xxxx with your additional Campaign Monitor field name, e.g. cm-name

//send data to the form
$url = 'http://xxxxxx.xxx/'; //replace xxxxxx.xxx with your Campaign Monitor form URL, usually look like xxxxxx.xxx/x/x/x/xxxxxx/
$headers = array('Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8');
$data = http_build_query($content, '', '&');
$req = drupal_http_request($url, $headers, 'POST', $data);
}
?>

I hope this is helpful for anyone else who was looking for the same. Feel free to enhance this if you will. Thanks again to epersonae2 who originated this tip!

baronmunchowsen’s picture

Code in #21 works awesome for me. Thanks to all involved putting this together. I added a message after the drupal_http_request to let the user know that they've been subscribed:

<?php
if ($form['submitted']['subscribe']['#value']) {

  ... as above ...

  $req = drupal_http_request($url, $headers, 'POST', $data);

  //send a confirmation message to the user
  drupal_set_message(t('You have been successfully subscribed to our newsletter.'));
}
?>
james-oppenheim’s picture

Thanks for that, the code above worked a treat. Super easy!

quasi’s picture

Hi, I can't get this code to work. The data just doesn't get to my CM account.
Anything I should be careful about?

Does this approach even need the Campaign Monitor module?

quasi’s picture

I had my fields in a fieldset so
$form['submitted']['email']['#value']
becomes:
$form['submitted']['fieldset']['email']['#value']

And the answer to my second question is no.

Thank you for this wonderful solution!

nosro’s picture

Using the technique from #21, I found that you can also subscribe via the campaign monitor module if you want to:

if ($form['submitted']['subscribe']['#value']) { //replace subscribe with your checkbox field name
//get the email address and any additional fields
$email = $form['submitted']['email']['#value'];
$name = $form['submitted']['name']['#value'];

$api_key = variable_get(CM_API_KEY, '');
$list_id = 'xxx'; // enter your campaign id;
_campaignmonitor_add_subscriber($api_key, $list_id, $name, $email); // requires campaign monitor module of course
}
rocketfuel’s picture

Thanks to everyone who contributed to this thread, you saved me a lot of heartache. Have a great weekend!

rimian’s picture

Yes, this is how I did it (Webform > Config > Advanced Settings > Additional Processing). My code is like this:

$api_key = variable_get('campaignmonitor_api_key', '');
$list_id = variable_get('campaignmonitor_list_id', '');
$submitted = $form_state['values']['submitted_tree'];
if(!empty($submitted['join_our_mailing_list'])) {
  $name = $submitted['your_name'];
  $email = $submitted['your_email'];
  _campaignmonitor_add_subscriber(
    $api_key,
    $list_id,
    $name,
    $email
  );
}

I've got a few suggestions for this:

I'd refactor _campaignmonitor_add_subscriber to campaignmonitor_add_subscriber. I don't see why this function can't be used as an interface for other modules like webform. This module only has the formAPI as an interface. Fix that and half your problems go away.

Should the $api_key be a mandatory argument for this function? Why not have something like:

function campaignmonitor_add_subscriber($list_id, $name, $email, $api_key = FALSE) {
  if(!$api_key) {
    $api_key = _campaignmonitor_get_api_key();
  }
  //do the rest of the stuff
}
hessam61’s picture

Thanks deanloh. Post #21 worked for me and I was able to send the information from webform to the Campaign Monitor. The only problem is I get a white screen after submitting the form. Am I missing something?

summit’s picture

Subscribing, greetings, Martijn

sarhansg’s picture

This is something i've been looking for. Read an alternative method on another site which you can find at http://houseoflaudanum.com/navigate/howtos/add-campaign-monitor-opt-in-u... . Not sure if I did something wrong but it simply didn't work for me.

I've installed the following modules:

  • webform-6.x-3.9
  • campaignmonitor-6.x-2.4

Problem is, based on the link above, Webform 3.x has done away with the Additional/Advance Processing required by most of the techniques offered in this thread. Unfortunately, I cannot revert to an older version of Webform.

Should anybody have a solution I'd be more than happy to try.

gambry’s picture

From: http://drupal.org/project/webform
- The Additional Processing and Validation code options have moved to a separate project. Webform PHP.

Could it help?

bdiddymc’s picture

I have tested this on a basic webform for a site I need to set up, and it seemed to work so far. (Thanks for the link!)

Did you:

- Generate a subscribe form in the Campaing monitor system?
- make sure to set the url for the campaign monitor form in the module (line 21).
- Replace the hyphens with underscores for the webform Field Keys (and have them match the names in the CM generated form).
- Did you set up a check box with "cm-opt-in|I would like to sign up" option

Guessing one of those has been missed.

(this is the reply to #31 by the way)

sarhansg’s picture

Thanks bdiddymc. Looking at your suggestions I realised I didn't include the cm-opt-in option in my webform. I did try adding it but to no avail. I even tried changing it to cm_opt_in but it still didn't work. Any other ideas?

seancorrales’s picture

Just an update for all your campaign monitor users: it looks like the method a lot of people are recommending (create a form URL and POST the values to it) won't work. Campaign Monitor has started requiring users to fill out a CAPTCHA and confirm their email address before it'll accept the submission.

sarhansg’s picture

Thanks for the information droidenator. That brings us all to square one. Any solutions people?

alexreinhart’s picture

I've been working on a complete solution to this problem by using Campaign Monitor's API to submit the webform data directly. It also allows for users to edit their Campaign Monitor preferences (and unsubscribe and such) through a custom webform. Would this module's maintainers be interested in collaborating on getting this included?

cableman0408’s picture

Hi Alex
If you have a solution to this, I will be happy to help in getting this included into the module. But you should be aware that I have added some new functionality into the module and re-factored parts of the code the last 2 weeks. So you should checkout the newest version from 6.x-3.x-dev

alexreinhart’s picture

Hm. One problem is that I'm using the latest Campaign Monitor PHP wrapper, which is substantially different from the version your module seems to use. Are you planning to update to the new version at any point? I've found it relatively easy to work with.

Also, Webform provides capabilities which essentially duplicate this module's features -- it can provide forms in blocks or in nodes, so a simple "subscribe" block can be easily created without rebuilding Webform's extensive features. I'm not sure what approach you'd want to take to handling this duplication.

cableman0408’s picture

I have just started on the D7 version and think that we should use the latest Campaign Monitor PHP wrapper for it. Based on the "issues" with this I'll look into getting the 6.x-3.x branch updated to use it as well.

I don't think that the Campaign Monitor module should be dependent on Webforms as many sites do not use the webforms module. So I do not see this as a duplication of features, but more an extension of the module to easily integrate with webforms.

alexreinhart’s picture

Hm, that sounds good. The Webform integration code shouldn't care much about Drupal version, so when your D7 version moves to the new API I could make a patch to integrate the Webform features with it. Later, it could be backported to 6.x relatively easily. The Webform stuff could be hidden behind a configuration switch for those users who don't have or want Webform on their sites.

One aside, though: My development is sponsored by a Drupal-based web design company, and their support is contingent upon getting a little note of sponsorship, like we've done with our other module. I hope that's fine with you.

alexreinhart’s picture

Alternately I suppose the Webform integration could be put in as a submodule, so it could be enabled or disabled through the ordinary module interface. That would provide a nice logical separation of the code, while still allowing sharing of core features.

sarhansg’s picture

Looking forward to this development. Good luck!

cableman0408’s picture

I think that it should be a sub-module, but you should wait until I have complete the new API layer.

In regards to the sponsorship link, I will have to ask ssherriff

alexreinhart’s picture

Okay, that sounds good. Just ping me when the new API layer is ready and I can start porting over to it.

artis’s picture

cableman0408, I'm helping alexrienhart with this module (just ported it to D7) and was wondering if you've completed your changes on the API layer so we can get the submodule finished and contributed?

Thanks.

cableman0408’s picture

The D7 version is using the Campaign monitor v3 API, which I have create a wrapper class around in http://drupalcode.org/project/campaignmonitor.git/blob/refs/heads/7.x-1.... to create a Drupal abstraction layer.

I'm not planing any changes to this layer before a finale 1.0 release :-) I have however not found the time to back port the new layer to the 6.x-3.x branch.

Hope this helps :-)

albert volkman’s picture

Today I needed to add campaign monitor to a webform. Following the concept of #26 but using the object found in the campaignmonitor module, I did-

<?php

/**
 * Implements hook_form_alter().
 */
function webformcm_form_alter(&$form, &$form_state, $form_id) {
  switch ($form_id) {
    case '<form id>':
      // Add a submit handler
      $form['#submit'][] = 'webformcm_submit';
      break;
  }
}

function webformcm_submit($form, &$form_state) {
  // If the user has opted in
  if(isset($form_state['values']['<newsletter checkbox>'])) {
    // Grab connecter from CM
    $cm      = CampaignMonitor::getConnector();

    // Set list id, email and name
    $list_id = '<list id>';
    $email   = $form_state['values']['<email>'];
    $name    = $form_state['values']['<name>'];

    // Submit data to CM
    if (!$cm->subscribe($list_id, $email, $name)) {
      form_set_error('', t('You where not subscribed to the list, please try again.'));
      return FALSE;
    }   

    drupal_set_message(t('You are now subscribed to the newsletter.'), 'status');
  }
}

Works great! Replace the <values> per your webform. Remember to duplicate nested values in your webform as multilevel arrays.

jorgegc’s picture

Hey guys, I have created a sandbox that does exactly what you are after. The only problem is that it hasn't been ported to D6 yet but I am happy to do it if you guys help me get it to Drupal.org.

Check it out and let me know your thoughts! Cheers

http://drupal.org/sandbox/jorgegc/1530816

summit’s picture

Hi @jorgegc; this looks great! Yes please for porting to D6!
Greetings, Martijn

jorgegc’s picture

Hey Martijn, are you able to help out and review the D7 version? Cheers

summit’s picture

Hi @jorgegc. I am not using campaign monitor on my first Drupal 7 site. I will see if this is a good thing to install and then see if webform helps out. I can not promise this in the short term.
I use campaign monitor on a D6 site, but that doesn't help you I think.
greetings, Martijn

jorgegc’s picture

Hi Martijn,

A bit of good news for you... I have ported the module to Drupal 6. If you could give that a go that would be great! Any feedback is muchly appreciated :-)

http://drupal.org/node/1530816/git-instructions/6.x-1.x

Cheers,

Jorge

symphonia’s picture

@deanloh: thank you for the code... but I just can't find the Campaign Monitor form URL... could you tell me please where I can find it?

Edit: I found it.

vladimiraus’s picture

Issue summary: View changes
Status: Active » Closed (won't fix)

Closing 6.x issues