Alexa Plugin

Discussion in 'Tutorials' started by softdevsalon, Apr 9, 2019.

  1. softdevsalon
    • Licensed

    softdevsalon Member

    Hi sir,
    Just want to ask if how can I make a plugin and automatically updating the value on it?
    I want to get the data and using this api of alexa

    <?php
    $url="http://site.com/";
    $xml = simplexml_load_file('http://data.alexa.com/data?cli=10&dat=snbamz&url='.$url);
    $rank=isset($xml->SD[1]->POPULARITY)?$xml->SD[1]->POPULARITY->attributes()->TEXT:0;
    $web=(string)$xml->SD[0]->attributes()->HOST;
    echo $web." has Alexa Rank ".$rank;
    ?>

    Add a custom field on it in order to use or variable to use the alexa value.
    Every site listed will need to updated teh value of alexa when there is a changes.
  2. Basti
    • Staff

    Basti Administrator Staff Member

    plugins/YourPluginName/info.php
    Code:
    <?php
    $pluginname = 'My Amazing Plugin';
    $author = 'Me';
    $email = 'me@mydomain.com';
    $url = '#';
    $version = '1.0';
    $install = 0;
    ?>
    plugins/YourPluginName/rankings_compile_stats.php
    Code:
    $alexa_url = urlencode(htmlspecialchars_decode($TMPL['url'], ENT_QUOTES));
    $alexa_xml = simplexml_load_file('http://data.alexa.com/data?cli=10&dat=snbamz&url='.$alexa_url);
    $alexa_rank = isset($alexa_xml->SD[1]->POPULARITY) ? $alexa_xml->SD[1]->POPULARITY->attributes()->TEXT : 0;
    $alexa_web = (string)$alexa_xml->SD[0]->attributes()->HOST;
    
    $TMPL['alexa_string'] = $alexa_web." has Alexa Rank ".$alexa_rank;
    You notice I prepended alexa_ before your variables and secured the url a bit. The prepending is to avoid issues with other plugins which might use the same variable names. Always try to make them unique.
    Now simply use {$alexa_string} in your template files
    softdevsalon likes this.
  3. softdevsalon
    • Licensed

    softdevsalon Member


    I forgot to include, can I use the
    $alexa_rank value in ranking?

    Because, in our visiolist site if we dont have data for total page visit count and user didn't attach
    a link code to their site. The value and data for sorting is must be the value of alexa rank.
    So is it possible to use the alexa rank for ranking?

    Can we make an option for rank by page visits and rank by alexa?
  4. Basti
    • Staff

    Basti Administrator Staff Member

    That is a bit more complicated. And if a case statement does not work, I think there is nothing you can do there...

    For once you need to make a new INT field in VL_stats. alexa_rank with a default value of 0 ( zero )

    Then you need to actually store your alexa rank.
    While not optimal, for the case of testing, we do it in the file where you call the alexa rank.

    Once all works, you should make a cron file, which checks xx users every xx minutes and updates their database alexa rank.
    Code:
    $alexa_url = urlencode(htmlspecialchars_decode($TMPL['url'], ENT_QUOTES));
    $alexa_xml = simplexml_load_file('http://data.alexa.com/data?cli=10&dat=snbamz&url='.$alexa_url);
    $alexa_rank = isset($alexa_xml->SD[1]->POPULARITY) ? $alexa_xml->SD[1]->POPULARITY->attributes()->TEXT : 0;
    $alexa_web = (string)$alexa_xml->SD[0]->attributes()->HOST;
    
    $TMPL['alexa_string'] = $alexa_web." has Alexa Rank ".$alexa_rank;
    
    // Now first check if the database stored value is different than the above. If not, no need to update
    if ($alexa_rank != $TMPL['alexa_rank'])
    {
         $alexa_sql = (int)$alexa_rank;
         $alexa_username = $DB->escape($TMPL['username'], 1);
         $DB->query("UPDATE {$CONF['sql_prefix']}_stats SET `alexa_rank` = {$alexa_sql} WHERE `username` = '{$alexa_username}'", __FILE__, __LINE__);
    }
    
    Now you need in same plugin file called rankings_order_by.php
    Code:
    $order_by = "(CASE ". $this->rank_by($ranking_method)." WHEN 0 THEN alexa_rank ELSE ". $this->rank_by($ranking_method)." END) DESC, unq_{$ranking_method}_overall DESC";
    
    Again, im not 100% sure such a thing is even possible, high chance it isn't
    And when you test please deactivate your other ranking plugin. I saw in another thread that you rank by total something.
    just rename that other rankings_order_by.php while testing. Later on if this works, you need to combine these 2
    Mark likes this.
  5. softdevsalon
    • Licensed

    softdevsalon Member


    Ok thank you will test and do this. Will update the thread.
  6. softdevsalon
    • Licensed

    softdevsalon Member

    @Basti
    Done testing, it works!

    What is this code do?
    $order_by = "(CASE ". $this->rank_by($ranking_method)." WHEN 0 THEN alexa_rank ELSE ". $this->rank_by($ranking_method)." END) DESC, unq_{$ranking_method}_overall DESC";
    I did change DESC to ASC.. to this
    $order_by = "(CASE ". $this->rank_by($ranking_method)." WHEN 0 THEN alexa_rank ELSE ". $this->rank_by($ranking_method)." END) ASC, unq_{$ranking_method}_overall DESC";
    is this correct? because in alexa rank, the lowest is the highest.
    The problem is that what if the site doesn't have alexa data or 0 record. Site with 0 data will always show in number one or in top because the value is 0.
    (Temporary solution is that not to display them but how? Second temporary solution is to make inactive if detects 0 data/record but how?)



    How to do cron job? as you suggested?
    Last edited: Apr 15, 2019
  7. Basti
    • Staff

    Basti Administrator Staff Member

    That code basically means
    "When unq_pv_0_monthly is zero, it changes the pv value to the alexa column value and use that for the current member in loop order"

    Oh right alexa lower is better, I forgot that. But you cant simply make it ascending, cause also pageviews count would be affected by that. You need alexa in ascending and pv in descending. So we need to put up a few more cases.

    Try this, we have 3 order conditions this time instead of making an if, else.
    - if pv zero and alexa bigger zero; rank by that in ascending order as primary order
    - if that condition is false, it will be NULL ( order by NULL is equal to ignoring the hole line )
    - after that coming the default 2 vl orderings

    Code:
    $order_by = "
        (CASE WHEN ". $this->rank_by($ranking_method) ." = 0 AND alexa_rank > 0 THEN alexa_rank END) ASC,
        ". $this->rank_by($ranking_method) ." DESC,
        unq_{$ranking_method}_overall DESC
    ";
    
  8. softdevsalon
    • Licensed

    softdevsalon Member


    will try this one, is this also applicable to category?

    and how to do a cron job for just checking the alexa rank?

    can i make an option to sort by pv and alexa? how is it?

    last one, can i use alexa_rank (one created in the bm_stats column) to use in the template? because I think using $alexa_string this will always load and check the api instead of the database .
    Last edited: Apr 15, 2019
  9. Basti
    • Staff

    Basti Administrator Staff Member

    Yes it would work for category and stuff, but only if we combine your plugins. I don't know what else you all have as plugins, so it would be best if I can log into ftp for that.

    And yes, the cron would basicly handle the alexa checks, and you can simply use {$alexa_rank} then in templates ( any database column you can output in templates like that ). Or better set up an conditional with a plugin template. But ye it works.

    But before all that, the base thing should be tested for working and only then continue from there.
  10. softdevsalon
    • Licensed

    softdevsalon Member

    Let me know what to do first.
    -Combine the plugins
    -cron jobs
  11. Basti
    • Staff

    Basti Administrator Staff Member

    If the new order works, combine it with your other plugins who modify ranking ordering/methods
    cron is really the last on the list and no real issue if you have working code
  12. softdevsalon
    • Licensed

    softdevsalon Member

    Yes it works. But theres only problem in rank/sorting.
    it does not sort/rank properly.

    Let me give or take this situation

    - Priority is rank by unq pv.
    - If no pv then get the or use value or sort by a alexa_rank
    - If no alexa_rank data, disable or dont include in the rank


    So how to modify and combine it?
    Last edited: Apr 17, 2019
  13. Basti
    • Staff

    Basti Administrator Staff Member

    Then it does not works ;)

    Did you made sure you disabled your other ranking plugins? Lets say you plugin "a" which is this one, and plugin "b" which also modify sorting. "b" would overwrite "a"
  14. softdevsalon
    • Licensed

    softdevsalon Member

    Yes I did disabled the other plugins for ranking.

    UPDATE: Just solve the rank/sorting by using this query

    $order_by = "tot_{$ranking_method}_overall DESC,alexa_rank";

    PROBLEM: It cant skip using pv = 0 and alexa_rank=0, still the site in the list above and top above because of 0 value. I wanted the site to be in the last if both 0 .

    CAN YOU PROVIDE TO MAKE A PLUGIN THAT IF SITE GOT PV=0 AND ALEXA=0 WILL AUTOMATICALLY BE DELETED OR INACTIVE THEN EMAIL THE USER.?
    Last edited: Apr 18, 2019
  15. Basti
    • Staff

    Basti Administrator Staff Member

    Skip on ranking if both is zero: in your plugin - rankings_extend_where.php
    Code:
      $where_extra  .= " AND tot_{$ranking_method}_overall > 0 AND alexa_rank > 0";
    
    For this to work, you need to have the admin setting disabled. Make it 0
    Now your cron for alexa: unzip attached file and upload in your plugin
    it gets 10 random members each run and gets alexa.

    To setup the cron, login to your cpanel -> cron jobs
    make it run every minute, or every 5 minutes depending on the size of your list
    If you have recent cpanel, it will tell you on that page the command to input, just replace the path to your uploaded cron.php

    As for email, how do you planned that? You know every member starts of with 0 of everything, you cant simply email them.
    I'd say forget that email idea. Instead make a note on the signup form saying, sites with no pageviews and alexa rank are removed from public view or something

    Attached Files:

    • cron.zip
      File size:
      961 bytes
      Views:
      1
  16. softdevsalon
    • Licensed

    softdevsalon Member

    Code:
      $where_extra  .= " AND tot_{$ranking_method}_overall > 0 AND alexa_rank > 0";
    
    - It stops the sorting when pv is 0 even alexa_rank is not 0, sites are not in the list anymore

    I got your point. Thanks.
    On this matter
    "For this to work, you need to have the admin setting disabled. Make it 0"
    I don't want member that will be auto inactive when they are not active for several days.
    Given this example.. Site A is registered and has a number of tot_pv_overall but during some number of days site a is not active. I dont want to remove this..
    I only want to remove 0 pv/in/out and alexa_rank.

    On email part. I want them just to remind auto that they have no pv and rank. Thats why I need an auto email.
    Last edited: Apr 18, 2019
  17. Basti
    • Staff

    Basti Administrator Staff Member

    Oh I see, I guess you can keep that on then. Don't know how it will react to be honest though, so got to try out I guess. Was just a quick guess on my end.

    Oh right its a bit wrong code, try this
    Code:
    $where_extra  .= " AND (tot_{$ranking_method}_overall > 0 OR alexa_rank > 0)";
    
  18. Basti
    • Staff

    Basti Administrator Staff Member

    For the email part, hmm. You might could do this then.

    1) you need a new field in VL_sites
    - alexa_emailed, make it a tinyint field with default value of 0 ( zero )

    2) Now, so we don't email them each and every day, we have set this new field to 1 once they have been emailed.
    - then you could reset that alexa_email to zero once a week or month, so basicly would have a mail once a week/month to not spam them
    Lets see, first of the email reset. Make a file called new_month.php ( use new_week.php if you want to reset weekly instead
    Code:
    $DB->query("UPDATE {$CONF['sql_prefix']}_sites SET alexa_emailed = 0 WHERE alexa_emailed = 1", __FILE__, __LINE__);
    
    3) OK and for the actual email sending, we do with new file called new_day.php
    - We grab users with overall_pv and alexa is 0, right?
    - In the code below, change "MyPlugin" to your actual plugin name
    - I just grabbed username, if you need more info in the email template later, you need to add the required columns in the query "select"
    Code:
    $result = $DB->query("SELECT sites.username, sites.email FROM {$CONF['sql_prefix']}_sites sites, {$CONF['sql_prefix']}_stats stats WHERE sites.alexa_emailed = 0 AND stats.tot_pv_overall = 0 AND stats.alexa_rank = 0", __FILE__, __LINE__);
    while ($row = $DB->fetch_array($result)) {
    
        $TMPL = array_merge($TMPL, $row);
    
        // Here Send Alert E-Mail to User for his info 
        $alexa_email = new skin('alexa_email', "{$CONF['path']}/plugins/MyPlugin");
        $alexa_email->send_email($row['email']);
    
        // Email sent, set marker so it not repeats next day
        $DB->query("UPDATE {$CONF['sql_prefix']}_sites SET alexa_emailed = 1 WHERE username = '{$row['username']}'", __FILE__, __LINE__);
    }
    
    4) In same plugin again, make the email template, alexa_email.html
    - you can use any column you have in the "select" of step #3, for this purpose can only use {$username} and {$email} in the html file
  19. softdevsalon
    • Licensed

    softdevsalon Member

    It works in proper way now. Thanks.
  20. softdevsalon
    • Licensed

    softdevsalon Member

    Will try.. Thank you on this.

Share This Page