10 Replies Latest reply on Sep 20, 2009 1:59 PM by coltrane42

    Using Campaign Monitor API with a Custom Trigger

    coltrane42 Level 1

      Hello all,

       

      I'm trying to setup up a custom trigger to fire when someone posts a comment on my application that adds their email address to my Campaign Monitor newsletter list, but I'm having some trouble making my API info dynamic.

       

      The basic CM API code for adding a subscriber looks like this (after calling upon the CMBase.php file which contains the actual classes that is):

       

           $api_key = "xxxx001";
           $client_id = 'xxxx002';
           $campaign_id = null;
           $list_id = 'xxxx003';
           $cm = new CampaignMonitor( $api_key, $client_id, $campaign_id, $list_id );

          $result = $cm->subscriberAddRedundant('emailaddress', 'namegoeshere');

       

      I'm trying to make this basic functionality fire inside of a Custom Trigger after the user's comment is inserted into my comment database table.  So I basically just put this code inside of a custom trigger and converted the email and name data so that they draw from the transaction.  The trigger is designated as a "BEFORE" action in the list of registered triggers.

       

      function Trigger_UpdateCM(&$tNG) {

           $api_key = "xxxx001";
            $client_id = 'xxxx002';
            $campaign_id = null;
            $list_id = 'xxxx003';

           $cm = new CampaignMonitor( $api_key, $client_id, $campaign_id, $list_id );
           $result = $cm->subscriberAddRedundant($tNG->getColumnValue("email"), $tNG->getColumnValue("name"));
           return NULL;}

      (*I've simplied the error reporting for the purposes of this forum, the original code wouldn't actually return NULL, but would test for ...)

       

       

      All of this works fine to this point.  Now for my problem...

       

      I want to be able to change my API info dynamically, since this trigger will be appearing on several pages throughout my application... and since I'll probably end up reusing this for different projects.  Ideally, I want to store the API info in my MYSQL database along with the other site configuration settings....  I didn't anticipate a problem with this, since I can usually just get a recordset on the page and then call it using the InterAKT markup language, but that's not working since I'm not using InterAKT transactions here of course.  I also cannot figure out how to pass a variable into the transaction from the page without using a hidden field in the form itself (which I obviously don't want to do since I'm dealing with API keys).

       

      So how in the world do I get my API variables to draw from my database since I'm using a trigger?

      Am I going about this all wrong, or am I just missing something incredibly simple? 

       

      Thanks for any help you can give me... I'm really starting to pull my hair out!

       

      Michael

        • 1. Re: Using Campaign Monitor API with a Custom Trigger
          Albert S. Level 3

          Hi,

           

          You want to be able to change the API info dynamically, so at some point it is assigned to some kind of variable. Here is a link that shows the markup language that the transaction engine will support.

           

          http://help.adobe.com/en_US/Dreamweaver/10.0_ADDT/ch8_retrieve_dynamic_data_32.html#102902 7

           

           

          Also you want to be able to add it to a database, here are some examples of some custom transactions. One which includes and insert statement.

           

          http://help.adobe.com/en_US/Dreamweaver/10.0_ADDT/ch17_trans_engine3_33.html#1028781

           

           

           

          Here is a link to the ADDT manual.

           

          http://help.adobe.com/en_US/Dreamweaver/10.0_ADDT/

           

           

          Just for starters, hope that helps some.

          • 2. Re: Using Campaign Monitor API with a Custom Trigger
            coltrane42 Level 1

            Albert,

             

            Thanks so much for taking the time to help out...  although I'm not sure that solves this particular problem.

            I had already tried using the InterAKT markup language in the code to pull the API variables from my recordset like so:

             

            function Trigger_UpdateCM(&$tNG) {

                 $api_key = "{recordset.api_key}";
                  $client_id = '{recordset.client_id}';
                  $campaign_id = null;
                  $list_id = '{recordset.list_id}';

                 $cm = new CampaignMonitor( $api_key, $client_id, $campaign_id, $list_id );
                 $result = $cm->subscriberAddRedundant($tNG->getColumnValue("email"), $tNG->getColumnValue("name"));
                 return NULL;}

             

            Unfortunately this didn't work for me.  I suspect the reason for this is that my actual "$cm" class for exectuing the Campaign Monitor trigger is not a part of the InterAKT ADDT suite, so it doesn't recognize the InterAKT markup syntax.  But I'm not exactly sure about all of this.  It seems like the InterAKT markup should be able to pass the variables from the trigger into the Campain Monitor class just as it would with any of my usual Triggers, but it doesn't.  Maybe I'm missing something else here entirely? 

             

            Thanks again for the reply!

             

            Michael

            • 3. Re: Using Campaign Monitor API with a Custom Trigger
              Albert S. Level 3

              Hi,

               

              When trying to pass variable(s) thru a trigger it won't work when you do something like this...

               

              $api_key = "{recordset.api_key}";

               

              If api_key is a part of the original transaction you can do something like...

               

              $api_key = $tNG->getColumnValue("api_key");

               

              If api_key is a part of a seperate recordset that is not a part of the original transaction then you may need to add the column like...

               

              $tNG->addColumn("api_key", "STRING_TYPE", "VALUE", "{recordset.api_key}");

               

              You can do this is a BEFORE trigger to add the columns before the transaction happens.

               

              Are you updating the information to the same table or to a different table?

              • 4. Re: Using Campaign Monitor API with a Custom Trigger
                coltrane42 Level 1

                Albert,

                 

                Thanks again for the quick response... sorry to keep going back and forth on this, but it's a bit confusing for me and I want to make sure I'm understanding your suggestion fully

                 

                I'm not updating a separate table on my own application at all.  The trigger is making a call to Campaign Monitor's system.
                Let's say that a user submits a Contact Form through my system.  The following would occur (assuming they have checked the box to be added to the mailing list of course):

                 

                1- A new record is inserted into my ADDT application under the "contactus" table for the current transaction.
                2- Data is pulled from the current transaction and a Trigger is fired to add a new record to the Campaign Monitor DB using their API.

                 

                 

                In response to your last suggestion... I think I already tried adding additional columns for the API data directly to the current transaction.  I included three columns along with the fields needed to process the current transaction... but got an error when I tried to insert the new record into the "contactus" table since those API fields obviously don't exist in the database.

                 

                So are you suggesting that instead I can create an additional Trigger that fires before my Campaign Monitor Trigger?  And that within this new trigger I should be able to add the three additional columns needed for my API data (by calling upon the recordset I created on the page)?  I wonder why I couldn't just add the columns in that manner within the Campaign monitor trigger itself... something like this:

                 

                function Trigger_UpdateCM(&$tNG) {

                     $tNG->addColumn("api_key", "STRING_TYPE", "VALUE", "{recordset.api_key}");

                     $tNG->addColumn("client_id", "STRING_TYPE", "VALUE", "{recordset. client_id}");

                     $tNG->addColumn("list_id", "STRING_TYPE", "VALUE", "{recordset. list_id}");

                     $campaign_id = null;

                 

                     $cm = new CampaignMonitor( $tNG->getColumnValue("api_key"), $tNG->getColumnValue("client_id"), $campaign_id, $tNG->getColumnValue("list_id") );

                     $result = $cm->subscriberAddRedundant($tNG->getColumnValue("email"), $tNG->getColumnValue("name"));
                     return NULL;}

                 

                 

                Thanks,

                 

                Michael

                • 5. Re: Using Campaign Monitor API with a Custom Trigger
                  Albert S. Level 3

                  Hi,

                   

                  Ok without any trigger at all or ADDT what code would you use to update your Campaign Monitor API.

                  • 6. Re: Using Campaign Monitor API with a Custom Trigger
                    Albert S. Level 3

                    I guess what I am asking is if this had static values, does this work for you?

                     

                    $cm = new CampaignMonitor( $tNG->getColumnValue("api_key"), $tNG->getColumnValue("client_id"), $campaign_id, $tNG->getColumnValue("list_id") );
                    $result = $cm->subscriberAddRedundant($tNG->getColumnValue("email"), $tNG->getColumnValue("name"));

                     

                     

                    Ok I just re-read your initial question. Yes this does work if the values are static.

                    Sorry for asking the question.

                     

                    Message was edited by: Albert S.

                    • 7. Re: Using Campaign Monitor API with a Custom Trigger
                      coltrane42 Level 1

                      No worries!

                      Like I said, it's all a bit confusing to articulate

                      • 8. Re: Using Campaign Monitor API with a Custom Trigger
                        Albert S. Level 3

                        Hi,

                         

                        Give this a try... It calls the SQL statement for the API info within the trigger so you don't have to try to bring the values in from outside of the trigger.

                        I don't know the transaction engine that well but SQL statement values from a statement that is not within the original transaction while not using POST, SESSION or GET can be difficult or not even possible (not 100% sure). Markup that works within a trigger generally has to be tied to the original transaction in which its running.

                         

                        function Trigger_UpdateCM(&$tNG) {
                        $queryAPI = "SELECT STATEMENT THAT GETS API INFO";
                        $apiResults = $tNG->connection->execute($queryAPI);

                         

                        $api_key = $apiResults->Fields('api_key');
                        $client_id = $apiResults->Fields('client_id');
                        $campaign_id = null;
                        $list_id =  $apiResults->Fields('list_id');

                         

                        $cm = new CampaignMonitor( $api_key, $client_id, $campaign_id, $list_id );
                        $result = $cm->subscriberAddRedundant($tNG->getColumnValue("email"), $tNG->getColumnValue("name"));

                         

                        if (!apiResults) {
                        $errObj = new tNG_error('No API information found!', array(), array());
                        return $errObj;
                        } else {
                        return null;
                        }

                         

                        }

                         

                        Let me know if that works.

                        If you actually need to get muliple results (array) from the SQL statement you can roll thru using commands like.

                         

                        while(!apiResults->EOF) {

                          $apiResults->Fields('api_key');

                        $apiResults->MoveNext();

                        }

                        • 9. Re: Using Campaign Monitor API with a Custom Trigger
                          coltrane42 Level 1

                          Albert,

                           

                          That looks like a promising solution!  I'm on a deadline with a separate project at the moment, so it may take me a couple of days to try implementing this, but I'll definitely let you (and any other interested parties out there) know how it goes.  Thanks for the great energy you've put into this!

                           

                          Michael

                          • 10. Re: Using Campaign Monitor API with a Custom Trigger
                            coltrane42 Level 1

                            Albert,

                             

                            Sorry it's taking me so long to test this stuff out!  I seem to constantly have more and more work piled upon me, and it's been tough to get back to this issue since I came up with an acceptable workaround.  But still, I'm hoping to eventually create a more elegant solution.

                             

                            In the meantime, I ran across something that may hold the answer.  There is apparently a specific way to import global php variables into the trigger, without having to exectue the sql query all over again.  I've used this on some other functions, and it worked out great!  So, when I get the time, I think that all I'll need to do to fix the Campaign Monitor trigger will be to define the variables outside of the trigger, and then rewrite the trigger to this:

                             

                            $api_key = "xxxx001";
                            $client_id = 'xxxx002';
                            $campaign_id = null;
                            $list_id = 'xxxx003';

                             

                            function Trigger_UpdateCM(&$tNG) {

                                 global $api_key;

                                 global $client_id;

                                 global $campaign_id;

                                 global $list_id;

                                 $cm = new CampaignMonitor( $api_key, $client_id, $campaign_id, $list_id );
                                 $result = $cm->subscriberAddRedundant($tNG->getColumnValue("email"), $tNG->getColumnValue("name"));
                                 return NULL;}

                             

                            I'll let you know when I finally get some time to test this out, but I think it should work fine based on some other applications I've run across.  There's probably a way to import multiple global variables at once, but I haven't done the research yet.

                             

                            Michael