Tracking and Email Alert

In this board you can talk about general questions about phpMyFAQ

Moderator: Thorsten

PinMan
Posts: 34
Joined: Mon May 15, 2006 2:59 pm

Tracking and Email Alert

Post by PinMan »

Hello guys :D

Is it possible to build in an email alert for user tracking (i.e. an email gets sent to the admin address) when someone uses the 'Send to a Friend' option in an FAQ answer?

I currently use this for user tracking on Search requests - as per a fix I found on one of the other forums.

Cheers

:D
matteo
Posts: 572
Joined: Sun Nov 20, 2005 6:53 pm
Location: Italy

Post by matteo »

Hi,
do you mean to send the same email also as BCC to the PMF server admin?

Regards,
Matteo
phpMyFAQ QA / Developer
Amazon.co.uk Wishlist
PinMan
Posts: 34
Joined: Mon May 15, 2006 2:59 pm

Post by PinMan »

Hi Matteo,

Yes that's it basically :D
matteo
Posts: 572
Joined: Sun Nov 20, 2005 6:53 pm
Location: Italy

Send a BCC copy to the PMF administrator

Post by matteo »

Hi,
locate, backup and open the [PATH_TO_PMFINSTALL]/mailsend2friend.php file and change this line below from:

Code: Select all

                mail($IDN->encode($mail), $PMF_LANG["msgS2FMailSubject"].$name, $PMF_CONF["send2friend_text"]."\n\n".$PMF_LANG["msgS2FText2"]."\n".$link."\n\n".$attached, "From: ".$IDN->encode($mailfrom));
to:

Code: Select all

                mail($IDN->encode($mail), $PMF_LANG["msgS2FMailSubject"].$name, $PMF_CONF["send2friend_text"]."\n\n".$PMF_LANG["msgS2FText2"]."\n".$link."\n\n".$attached, "From: ".$IDN->encode($mailfrom)."\r\nBcc: ".$IDN->encode($PMF_CONF['adminmail']));
Ciao,
Matteo
phpMyFAQ QA / Developer
Amazon.co.uk Wishlist
PinMan
Posts: 34
Joined: Mon May 15, 2006 2:59 pm

Post by PinMan »

Thanks Matteo (again) - it worked perfectly 8)
PinMan
Posts: 34
Joined: Mon May 15, 2006 2:59 pm

Post by PinMan »

Hello Matteo,

Taking this a step further, rather than generating an email - could the search string be automatically inserted into a standalone 'search query' database table (within the FAQ database)?

Cheers
PinMan
matteo
Posts: 572
Joined: Sun Nov 20, 2005 6:53 pm
Location: Italy

Post by matteo »

Hi PinMan,
have you already set up the new table or are you asking for an out-of-the box code snippet covering both PHP and SQL code for tracking search queries on DB?

Ciao,
Matteo
phpMyFAQ QA / Developer
Amazon.co.uk Wishlist
PinMan
Posts: 34
Joined: Mon May 15, 2006 2:59 pm

Post by PinMan »

Hi Matteo,

I was tempted to give it a try myself, but didn't want to break anything - so to answer your question, I'm after a code snippet please :D

Thank you
matteo
Posts: 572
Joined: Sun Nov 20, 2005 6:53 pm
Location: Italy

Post by matteo »

Hi PinMan,
I've been quite busy in these last weeks: I'll post it "ASAP", just wait some very few days ;)

Ciao,
Matteo
phpMyFAQ QA / Developer
Amazon.co.uk Wishlist
PinMan
Posts: 34
Joined: Mon May 15, 2006 2:59 pm

Post by PinMan »

Hi Matteo,

No problem - you take as long as you need, as I know you are a busy man :D

Thank you very much 8)
PinMan
Posts: 34
Joined: Mon May 15, 2006 2:59 pm

Post by PinMan »

Just a thought whilst we're at it - could we also include the CGI environment variables HTTP_REFERER and HTTP_USER_AGENT in the tracking as well?

Sorry :oops:
PinMan
Posts: 34
Joined: Mon May 15, 2006 2:59 pm

Post by PinMan »

Hi Matteo

Sorry to nag, but have you had any time to look into my request?

:D
matteo
Posts: 572
Joined: Sun Nov 20, 2005 6:53 pm
Location: Italy

Post by matteo »

Hi Pinman,
I've done just few line of code at the time of my first reply: I think I could close it within the first half of the next week.

Ciao,
Matteo
phpMyFAQ QA / Developer
Amazon.co.uk Wishlist
matteo
Posts: 572
Joined: Sun Nov 20, 2005 6:53 pm
Location: Italy

Post by matteo »

Hi PinMan,
here is the code.
  1. Create a file called searchstats.php in the [PATH_TO_PMF_INSTALL]/inc folder pasting the code below:

    Code: Select all

    <?php
    /**
     * removeNoiseWords()
     *
     * Remove some noise words, usefull if you cannot modify
     * your MySQL conf in order to manage the options:
     *
     *   [mysqld]
     *   ft_stopword_file = /path/to/list_of_noise_words.txt
     *   ft_min_word_len = 3
     *
     * @param    string
     * @param    string
     * @param    integer
     * @return   string
     * @access   public
     * @author   Matteo Scaramuccia <matteo@scaramuccia.com>
     * @since    2006-08-28
     */
    function removeNoiseWords($text, $filename = 'inc/noisewords.txt', $minLen = 3)
    {
        if (isset($minLen)) {
            $words = explode(' ', $text);
            $gtMinLenWords = array();
            foreach ($words as $word) {
                if (strlen($word) > $minLen) {
                    $gtMinLenWords[] = $word;
                }
            }
            $text = implode(' ', $gtMinLenWords);
        }
         // Case sensitive noise words array
        $noiseWords = @file($filename);
        if (is_array($noiseWords)) {
            $text = str_replace($noiseWords, '', $text);
        }
    
        return $text;
    }
    
    /**
     * getNoSearchTrackingIPList()
     *
     * Return an array of IP addresses that are not tracked
     * for search patterns statistics.
     *
     * @access   public
     * @author   Matteo Scaramuccia <matteo@scaramuccia.com>
     * @since    2006-12-07
     */
    function getNoSearchTrackingIPList()
    {
        $_ips = array();
        // E.g.:
        // $_ips[] = '127.0.0.1';
    
        return $_ips;
    }
    
    /**
     * createSearchStatsTables()
     *
     * Create the 2 extra SQL tables: faqsearch_patterns and faqsearch_words
     *
     * @access   public
     * @author   Matteo Scaramuccia <matteo@scaramuccia.com>
     * @since    2006-12-07
     */
    function createSearchStatsTables()
    {
        global $DB, $db;
    
        switch($DB['type']) {
            default:
                $query[] = '
                            CREATE TABLE '.SQLPREFIX.'faqsearch_patterns (
                            id INT(11) NOT NULL,
                            pattern TEXT NOT NULL,
                            browser VARCHAR(100) NOT NULL,
                            browser_version VARCHAR(64) NOT NULL,
                            os VARCHAR(100) NOT NULL,
                            os_version VARCHAR(64) NOT NULL,
                            referer TEXT NOT NULL,
                            user_agent VARCHAR(255) NOT NULL,
                            ip VARCHAR(15) NOT NULL,
                            time INT(11) NOT NULL,
                            PRIMARY KEY (id)
                            ) TYPE = MyISAM';
                $query[] = '
                            CREATE TABLE '.SQLPREFIX.'faqsearch_words (
                            word VARCHAR(255) NOT NULL,
                            counter INT(11) NOT NULL,
                            time INT(11) NOT NULL,
                            PRIMARY KEY (word)
                            ) TYPE = MyISAM';
                break;
        }
    
        while ($each_query = each($query)) {
            $result = $db->query($each_query[1]);
        }
    }
    
    /**
     * saveSearchEngineStats(()
     *
     * Save the search pattern, both whole and each word.
     * Manage also a counter to track the patterns/words usage.
     *
     * @param    string
     * @return   boolean
     * @access   public
     * @author   Matteo Scaramuccia <matteo@scaramuccia.com>
     * @since    2006-12-07
     */
    function saveSearchEngineStats($search)
    {
        global $db;
    
        $_pattern   = $db->escape_string($search);
        $_referer   = (isset($_SERVER['HTTP_REFERER']) ? $db->escape_string($_SERVER['HTTP_REFERER']) : '');
        $_userAgent = (isset($_SERVER['HTTP_USER_AGENT']) ? $db->escape_string($_SERVER['HTTP_USER_AGENT']) : '');
        $_ipAddress = (isset($_SERVER['REMOTE_ADDR']) ? $db->escape_string($_SERVER['REMOTE_ADDR']) : '');
        $_now       = time();
    
        // Do not track any search pattern coming from a well-known IP addresses list
        if (in_array($_ipAddress, getNoSearchTrackingIPList()))
        {
            return true;
        }
    
        // Let's try to discover some browser info using one of these browser capabilities detection scripts:
        // a. PHP native get_browser();
        // b. User Agent Info (BrowserInfo class);
        // c. Browser Capabilities PHP Project (Browscap class):
        //    the same capabilities of get_browser() BUT without its contraints i.e. definition file update, perfomance, need of php.ini access.
        $_browser           = '';
        $_browserVersion    = '';
        $_os                = '';
        $_osVersion         = '';
    
        // PHP native get_browser()
        // http://www.php.net/manual/en/function.get-browser.php
        $browser = @get_browser();
        if ('object' == gettype($browser)) {
            $_browser           = $browser->browser;
            $_browserVersion    = $browser->version;
            $_os                = $browser->platform;
        } else if (@file_exists('inc/Browscap/Browscap.php')) {
            // Browser Capabilities PHP Project (Browscap class)
            // http://garetjax.info/projects/browscap/
            require('inc/Browscap/Browscap.php');
            // WARNING: be sure that the 'cache' folder has write right for the WebServer user
            $bc = new Browscap('inc/Browscap/cache');
            $browser = $bc->getBrowser();
            $_browser           = $browser->Browser;
            $_browserVersion    = $browser->Version;
            $_os                = $browser->Platform;
        } else if (@file_exists('inc/class.browser_info.php')) {
            // User Agent Info (BrowserInfo class)
            // http://www.zend.com/codex.php?id=1290&single=1
            require('inc/class.browser_info.php');
            $browser = new BrowserInfo($_userAgent);
            $_browser           = $browser->Browser;
            $_browserVersion    = $browser->Browser_Version;
            $_os                = $browser->OS;
            $_osVersion         = $browser->OS_Version;
        }
    
        // Insert the whole text pattern
        $query = sprintf("
            INSERT INTO
                %sfaqsearch_patterns
                (id, pattern, browser, browser_version, os, os_version, referer, user_agent, ip, time)
            VALUES
                (%d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d)",
            SQLPREFIX,
            @$db->nextID(SQLPREFIX.'faqsearch_patterns', 'id'),
            $_pattern,
            $_browser,
            $_browserVersion,
            $_os,
            $_osVersion,
            $_referer,
            $_userAgent,
            $_ipAddress,
            $_now
            );
        $result = @$db->query($query);
    
        if ($result === false) {
            createSearchStatsTables();
            return false;
        }
    
        // Update/Insert each word from the search pattern
        // Remove noise words
        $_patternCleaned = strtolower(removeNoiseWords($_pattern));
        // Update/Insert each word
        $words = explode(' ', $_patternCleaned);
        foreach ($words as $word) {
            $query = sprintf("
                SELECT
                    time
                FROM
                    %sfaqsearch_words
                WHERE
                    word = '%s'",
                SQLPREFIX,
                $word
                );
            $result = $db->query($query);
            if (1 == $db->num_rows($result)) {
                $query = sprintf("
                    UPDATE
                        %sfaqsearch_words
                    SET
                        counter = counter + 1,
                        time    = %d
                    WHERE
                        word    = '%s'",
                    SQLPREFIX,
                    time(),
                    $word
                    );
                $result = $db->query($query);
            } else {
                $query = sprintf("
                    INSERT INTO
                        %sfaqsearch_words
                        (word, counter, time)
                    VALUES
                        ('%s', %d, %d)",
                    SQLPREFIX,
                    $word,
                    1,
                    $_now
                    );
                $result = $db->query($query);
            }
        }
    
        return true;
    }
    ?>
    
  2. Add any entry, if you don't want to track requests for some IPs, in the code above:

    Code: Select all

    ...
    function getNoSearchTrackingIPList()
    {
        $_ips = array();
        // E.g.:
        // $_ips[] = '127.0.0.1';
    
        return $_ips;
    }
    ...
  3. Choose one of the 3 methods, quoted in the code, for extracting some browser info.
    My suggestion is to use:
    // User Agent Info (BrowserInfo class)
    // http://www.zend.com/codex.php?id=1290&single=1
    creating a file called class.browser_info.php in the inc folder pasting the code on the url above.
  4. This previous hack I coded for you must be moved to:

    Code: Select all

    ...
    function searchEngine($begriff, $category = '%')
    {
        global $db, $sids, $tree, $PMF_LANG, $PMF_CONF;
    
        require_once('inc/searchstats.php');
        saveSearchEngineStats($begriff);
        $begriff     = removeNoiseWords($begriff);
        $_begriff    = $begriff;
    ...
That's all ;)

Ciao,
Matteo
phpMyFAQ QA / Developer
Amazon.co.uk Wishlist
PinMan
Posts: 34
Joined: Mon May 15, 2006 2:59 pm

Post by PinMan »

Thanks Matteo :D

I removed the 'removenoisewords' function from searchstats.php (as it had already been declared in functions.php) and it worked like a dream.

You are a star man
:mrgreen:
Post Reply