Extending the API

Discussion in 'Extensions' started by Ignacioport, Dec 29, 2015.

  1. Ignacioport

    Ignacioport Member

    Joined:
    Dec 24, 2015
    Messages:
    33
    Likes Received:
    4
    S.E:
    Expired
    L.T:
    Regular
    L.C:
    3
    Hello Twisted1919 and fellow Mailwizz users,
    how are you?

    I am looking to extend the Mailwizz API to have enough functionality to create a proper reporting platform. Is there any kind hook to allow me to do such thing?

    At the moment I am kind of new to YII, so I am studying the framework as a mean to extend Mailwizz. Do someone have any reference or recommendations regarding this?

    A am also looking for a reference of the available hooks within Mailwizz, where could I check such thing?

    Thanks!
     
  2. twisted1919

    twisted1919 Administrator Staff Member

    Joined:
    Dec 27, 2014
    Messages:
    10,450
    Likes Received:
    2,432
    @Ignacioport - If you want to add reporting abilities via the api, then you can create one/more controllers in the /apps/api/controllers folder and put everything you need there. Make sure you give them a name with a prefix to avoid name collisions in future.
    Then after your endpoints are done, simply create new classes in the php sdk to handle them, that's the easiest part.

    Related to the hooks list, you can find such list by running this command from command line:
    Code:
    grep -rnw 'apps/' -e "applyFilters(" && grep -rnw 'apps/' -e "doAction("
    
    The above will show the file/line where they are defined and the params they make available.
     
  3. Ignacioport

    Ignacioport Member

    Joined:
    Dec 24, 2015
    Messages:
    33
    Likes Received:
    4
    S.E:
    Expired
    L.T:
    Regular
    L.C:
    3
    Thanks a lot for your answer Twisted!

    I created a ReportsController and started to work on this implementation.

    Should I add something like:

    array('reports/view', 'pattern' => 'reports/<campaign_uid:([a-z0-9]+)>', 'verb' => 'GET'),

    To the urlManager from
    //apps/api/config/main.php ?

    I am asking due to this comment on such file

    * This file should not be altered in any way!

    If this is not the proper approach, what do you recommend?

    Regards.
     
  4. twisted1919

    twisted1919 Administrator Staff Member

    Joined:
    Dec 27, 2014
    Messages:
    10,450
    Likes Received:
    2,432
    @Ignacioport - Good things you read and take the comments into consideration.
    Instead of altering the main.php file, create a file called main-custom.php and place the code in it, this way, at upgrades, your changes won't be affected ;)
    The main-custom.php file should contain:
    PHP:
    <?php defined('MW_PATH') || exit('No direct script access allowed');

    return array(
        
    'components' => array(
            
    'urlManager' => array(
                
    'rules' => array(
                    array(
    'reports/view''pattern' => 'reports/<campaign_uid:([a-z0-9]+)>''verb' => 'GET'), 
                ),
            ),
        ),
    );
    Keep in mind that this main-custom.php file will be merged with the main.php one ;)

    Hope it makes sense.
     
  5. Ignacioport

    Ignacioport Member

    Joined:
    Dec 24, 2015
    Messages:
    33
    Likes Received:
    4
    S.E:
    Expired
    L.T:
    Regular
    L.C:
    3
    It makes a lot of sense.

    Thanks man!! You offer awesome support.

    I already managed to retrieve the info a needed using the API. All thanks to your great suggestions.

    Cheers!
     
    twisted1919 likes this.
  6. Jose Vega

    Jose Vega Member

    Joined:
    May 19, 2015
    Messages:
    59
    Likes Received:
    9
    S.E:
    Expired
    L.T:
    Regular
    L.C:
    1
    @Ignacioport Would you be willing to share your code so we can use the reporting endpoints?
     
  7. Ignacioport

    Ignacioport Member

    Joined:
    Dec 24, 2015
    Messages:
    33
    Likes Received:
    4
    S.E:
    Expired
    L.T:
    Regular
    L.C:
    3
    @Jose Vega That is actually a though one, as that is not work that I did for myself. I did it for the agency I work at. If that was not the case I would have no problem sharing the code.

    Yet it is not a very though implementation and acording to some posts I saw you seem to be a developer so I could give you some hints on how you can easily accomplish this.

    Create your ReportsController in the api controllers folder (of course)
    Check closely CampaignsController. You will find plenty of useful methods there.

    As a starting point you can copy the actionView from CampaignsController to your newly created controller.

    add this to such method:

    PHP:
    $campaign->attachBehavior('stats', array(
                
    'class' => 'customer.components.behaviors.CampaignStatsProcessorBehavior',
                ));
             
                
    $record['open_unique']   = $campaign->stats->getUniqueOpensCount(false);
                
    $record['unique_open_rate']   = $campaign->stats->getUniqueOpensRate(true);
                
    $record['open']   = $campaign->stats->getOpensCount(false);
                
    $record['open_rate']   = $campaign->stats->getOpensRate(true);
                
    $record['click_trhough_rate']   = $campaign->stats->getClicksThroughRate(true);
                
    $record['clicks_unique']   = $campaign->stats->getUniqueClicksCount(false);
                
    $record['unique_click_rate']   = $campaign->stats->getUniqueClicksRate(true);
                
    $record['clicks']   = $campaign->stats->getClicksCount(false);
                
    $record['click_rate']   = $campaign->stats->getClicksRate(true);
                
    $record['bounces']   = $campaign->stats->getBouncesCount(false);
                
    $record['unsubscribes']   = $campaign->stats->getUnsubscribesCount(false);         
                
    $record['recipients']   = $campaign->stats->getDeliverySuccessCount(false);         
                
    $record['processed_mails']   = $campaign->stats->getProcessedCount(false);
                
    /* - /ESTO  -*/
             
                
    $data['record'][] = $record;
            
    With that you should have reporting capabilities in a per campaign basis and a good idea on how you can extend this further ;)
     
    Last edited: Feb 23, 2016
    Mattias and twisted1919 like this.
  8. Jose Vega

    Jose Vega Member

    Joined:
    May 19, 2015
    Messages:
    59
    Likes Received:
    9
    S.E:
    Expired
    L.T:
    Regular
    L.C:
    1
    Thank you , yes , I am a developer and it shouldn´t be hard to implement this...
     

Share This Page