Gallery2:How to embed counter code at the page bottom - Gallery Codex
Personal tools

Gallery2:How to embed counter code at the page bottom

From Gallery Codex

Netstat/OneStat

You may want to add the [Nedstat]/[Onestat] etc. type of code to the bottom of your pages in order to track visitors to your album. Here is how to do it:

  • copy themes/<theme>/templates/theme.tpl to themes/<theme>/templates/local/theme.tpl
  • in the local version of the file, find the line with {g->trailer}
  • just before that line, add the following code:
{g->addToTrailer}
{php}
$countercode = <<<END

<the code provided to you by the counter site guys>

END;
echo $countercode;
{/php}
{/g->addToTrailer}   
  • save the file and go!

BBClone

---UPDATED FOR G2.1 - See History for G2.0 version---

Install bbclone under your gallery2 installation.

Create a file called gallery2.php and put it in the root of your gallery2. Put the following text in the gallery2.php file.

<?php
require_once(dirname(__FILE__) . "/main.php");

if (!defined('GALLERY_CONFIG_DIR')){
	define('GALLERY_CONFIG_DIR', dirname(__FILE__));
}

if (!defined("_BBCLONE_DIR")){
	define("_BBCLONE_DIR", GALLERY_CONFIG_DIR . "/bbclone/");
}

$Title = "";
$Title = pathway($_GET['g2_itemId']);
if (strlen($Title) > 0){
	define("_BBC_PAGE_NAME", $Title);
	define("COUNTER", _BBCLONE_DIR."mark_page.php");
	if (is_readable(COUNTER)) require (COUNTER);
}

function pathway($g2_itemId){
	//first get parent albums
	if (empty($g2_itemId)){
		//return "Gallery Root";
		$g2_itemId = 7;
	}
	if (!class_exists('GalleryCoreApi')){
		return "";
	}
	list ($ret, $parents) = GalleryCoreApi::fetchParentSequence($g2_itemId);
	if ($ret) { $parents = array(); }
	$parents[count($parents)+1]=$g2_itemId;
 	$g2_s=0;
	//gogogo
	$path = "";
	$extend = "";
	$seperator = " -> ";
	foreach($parents as $items){
		list ($ret,	$item) = GalleryCoreApi::loadEntitiesById($items);
		if (GalleryUtilities::isA($item, 'GalleryItem')){
			$title = "";		
			if (!$ret && $item){
				if ($item->getTitle()){
					$title = $item->getTitle();
				}else{
					$title = $item->getPathComponent();
				}
			}
			$titles[$item->getId()] = preg_replace('/\r\n/', ' ', $title);
	 
			//let's switch again
			if($g2_s == 0 AND count($parents) ==1){ //first and last		
				$path .= $extend.$title;
				$extend = $seperator;
			} elseif($g2_s == 0 AND count($parents) !=1) {// first but not last
				$path .= $extend.$title;
				$extend = $seperator;
			} elseif($g2_s == count($parents)-1 AND $g2_s == 1){// second and last	
				$path .=$extend.$title;
				$extend = $seperator;
			} elseif($g2_s == count($parents)-1){ // not second but it is last
				$path .=$extend.$title;
				$extend = $seperator;
			} elseif($g2_s == 1) {// everything in between			
				$path .=$extend.$title;
				$extend = $seperator;
			} else {
				$path .=$extend.$title;
				$extend = $seperator;
			}
		}
		$g2_s++;
	}
	if (!empty($_GET['g2_view']))
		if ($_GET['g2_view'] == "imageframe.CSS" ||
			$_GET['g2_view'] == "core.DownloadItem" ||
			$_GET['g2_view'] == "rewrite.FileNotFound")
			return "";
		else
 		    if ($_GET['g2_view'] != "core.ShowItem")
			$path .= " * " . $_GET['g2_view'];
	if (!empty($_GET['g2_subView']))
		$path .= " ~ " . $_GET['g2_subView'];
	return $path;		
}//end pathway function
?>

and then in your config.php include this line

define('GALLERY_MAIN_PHP', 'gallery2.php');

This will then record your hits no matter what theme is used. If you don't want to use bbclone, you can replace the bbclone specific code with another hit counter.

phpTrafficA

Follow the same directions as above for BBClone.

Install phpTrafficA and follow all of it's setup instructions.

Modify the write_logs.php file from phpTrafficA.

Change

if ($sites[$sid]['trim']) {
    $To = $_SERVER["PHP_SELF"];
} else {
    $To = $_SERVER["REQUEST_URI"];
}

to


if ($PageName != null &&
    strlen($PageName) > 0)
{
    $To = $PageName;
}else{
    if ($sites[$sid]['trim']) {
        $To = $_SERVER["PHP_SELF"];
    } else {
        $To = $_SERVER["REQUEST_URI"];
    }
}

and in gallery2.php instead of

$Title = "";
$Title = pathway($_GET['g2_itemId']);
if (strlen($Title) > 0){
    define("_BBC_PAGE_NAME", $Title);
    define("COUNTER", _BBCLONE_DIR."mark_page.php");
    if (is_readable(COUNTER)) require (COUNTER);
}

put in

$PageName = "";
$PageName = pathway($_GET['g2_itemId']);
if (strlen($PageName) > 0){
   $sid="12345";
   require("/phpTrafficA/write_logs.php"); 
}

Be sure in this last part of code to put the correct sid for your phpTrafficA site and that the path to write_logs.php is correct.

advertisements