2
votes

This is my 1st time I'm attempting to create a Joomla plugin and I need some help on getting this to work. The plugin is quite simple, I want to capture the HTTP_REFERER, check if the request was made from Google organic or paid results, pass the data to a session var and then submit it along with the values in a contact form. (there's a hidden field in my form and it gets the session var value). I use RSForms for creating my forms, just for the reference.

In the beginning, I hardcoded the following code into index.php at site root and it worked fine. Now, I'm trying to make a proper plugin but I can't get it to fire off when pages are loaded. I've tried all the system methods, still failing to get it to run.

This is my code:

defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.plugin.plugin' );

class plgSystemRsformsGoogleReferer extends JPlugin
{

    public function plgSystemRsformsGoogleReferer( &$subject, $config )
    {
        parent::__construct( $subject, $config );
    }


    function onAfterRender()
    {
        $session = & JFactory::getSession();
        if (!$session->get('referrer', $origref, 'extref')) //If does not exist
        {
          $origref = $_SERVER['HTTP_REFERER'];
          $session->set('referrer', $origref, 'extref');
          $q =  search_engine_query_string($session->get('referrer', $origref, 'extref'));

          if(stristr($origref, 'aclk')) { // if referer is a google adwords link as opposed to an organic link
              $type = ', paid link';
          } else {
              $type = ', organic result';
          }

        $ginfo = $q.$type;
        $session->set('referrer', $ginfo, 'extref');  

        }

        function search_engine_query_string($url = false) {
            if(!$url && !$url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : false) {
                return '';
            }

            $parts_url = parse_url($url);
            $query = isset($parts_url['query']) ? $parts_url['query'] : (isset($parts_url['fragment']) ? $parts_url['fragment'] : '');
            if(!$query) {
                return '';
            }
            parse_str($query, $parts_query);
            return isset($parts_query['q']) ? $parts_query['q'] : (isset($parts_query['p']) ? $parts_query['p'] : '');
        }
    }
}

And this is my manifest xml for the plugin installation (installation works fine):

<?xml version="1.0" encoding="utf-8"?>
<install version="1.5" type="plugin" group="system" method="upgrade">
    <name>RSForm Google Referer v1.1</name>
    <author>Me</author>
    <creationDate>July 2012</creationDate>
    <copyright>(C) 2004-2012 www.mysite.com</copyright>
    <license>Commercial</license>
    <authorEmail>[email protected]</authorEmail>
    <authorUrl>www.mysite.com</authorUrl>
    <version>1.1</version>
    <description><![CDATA[Track visitor's search terms and and attaches the information to the RSForm! Pro Forms emails when sent.]]></description>
    <files>
        <filename plugin="rsform_google_referer">rsform_google_referer.php</filename>
    </files>
</install>

I feel I'm close but I can't get it to run, any suggestions will be appreciated. Thanks!

1
First guess is that it is a mismatch between the internal name of your plugin - from the plugin="" parametever in the files->filename xml element and the name of your class. It could also be that it is the 'name' tag that is used but that you've got spaces and a version number in there. Can't remember the specifics but your classname - including the case of the letters has to correspond with the internal plugin name (however derived).Dean Marshall
@DeanMarshall Thanks Dean, really saved my day. I was looking for a solution why an old and unsupported joomla 1.5 plugin doesn't fire onAfter render. And the reason was class bane doesn't have underscores as in plugin internal name.Nesim Razon

1 Answers

4
votes

The name of the class is wrong. It needs to match the name of the plugin folder and that name of the plugin file. It should be:

class plgSystemRsform_Google_Referer extends JPlugin

That is Rsform not Rsforms and the underscores.