From 3e2b7f764ea9e62646b529f78c39084ef3fd4778 Mon Sep 17 00:00:00 2001
From: mattpiwik <matthieu.aubry@gmail.com>
Date: Mon, 13 Feb 2012 01:43:15 +0000
Subject: [PATCH] Fixes #2475  * Campaign will now be tracked when the campaign
 name/keywords parameters are found in the fragment / hash tag For example
 example.org/bla?hello=world#pk_campaign=CAMPAIGN&utm_term=KEYWORD will be
 tracked as visit from campaign CAMPAIGN and keyword KEYWORD  * Updated tests

git-svn-id: http://dev.piwik.org/svn/trunk@5825 59fd770c-687e-43c8-a1e3-f5a4ff64c105
---
 core/Tracker/Visit.php                        | 75 ++++++++++++-------
 plugins/CoreHome/templates/header.tpl         |  3 +-
 plugins/Live/templates/visitorLog.tpl         |  2 +-
 ...ns_ForceUsingVisitIdNotHeuristics.test.php |  4 +-
 4 files changed, 52 insertions(+), 32 deletions(-)

diff --git a/core/Tracker/Visit.php b/core/Tracker/Visit.php
index cb23baeb6f..0078ec3931 100644
--- a/core/Tracker/Visit.php
+++ b/core/Tracker/Visit.php
@@ -1427,45 +1427,66 @@ class Piwik_Tracker_Visit_Referer
 		return true;
 	}
 
-	/*
-	 * Campaign analysis
-	 */
-	protected function detectRefererCampaign()
+	protected function detectCampaignFromString($string)
 	{
-		if(isset($this->currentUrlParse['query']))
+		foreach($this->campaignNames as $campaignNameParameter)
 		{
-			$campaignParameters = Piwik_Common::getCampaignParameters();
-
-			$campaignNames = $campaignParameters[0];
-			foreach($campaignNames as $campaignNameParameter)
+			$campaignName = trim(urldecode(Piwik_Common::getParameterFromQueryString($string, $campaignNameParameter)));
+			if( !empty($campaignName))
 			{
-				$campaignName = trim(urldecode(Piwik_Common::getParameterFromQueryString($this->currentUrlParse['query'], $campaignNameParameter)));
-				if( !empty($campaignName))
-				{
-					break;
-				}
+				break;
 			}
+		}
 
-			if(!empty($campaignName))
-			{
-				$this->typeRefererAnalyzed = Piwik_Common::REFERER_TYPE_CAMPAIGN;
-				$this->nameRefererAnalyzed = $campaignName;
+		if(!empty($campaignName))
+		{
+			$this->typeRefererAnalyzed = Piwik_Common::REFERER_TYPE_CAMPAIGN;
+			$this->nameRefererAnalyzed = $campaignName;
 
-				$campaignKeywords = $campaignParameters[1];
-				foreach($campaignKeywords as $campaignKeywordParameter)
+			foreach($this->campaignKeywords as $campaignKeywordParameter)
+			{
+				$campaignKeyword = Piwik_Common::getParameterFromQueryString($string, $campaignKeywordParameter);
+				if( !empty($campaignKeyword))
 				{
-					$campaignKeyword = Piwik_Common::getParameterFromQueryString($this->currentUrlParse['query'], $campaignKeywordParameter);
-					if( !empty($campaignKeyword))
-					{
-						$this->keywordRefererAnalyzed = trim(urldecode($campaignKeyword));
-						break;
-					}
+					$this->keywordRefererAnalyzed = trim(urldecode($campaignKeyword));
+					break;
 				}
-				return true;
 			}
+			return true;
 		}
 		return false;
 	}
+	
+	/*
+	 * Campaign analysis
+	 */
+	protected function detectRefererCampaign()
+	{
+		if(!isset($this->currentUrlParse['query'])
+			&& !isset($this->currentUrlParse['fragment']))
+		{
+			return false;
+		}
+		$campaignParameters = Piwik_Common::getCampaignParameters();
+		$this->campaignNames = $campaignParameters[0];
+		$this->campaignKeywords = $campaignParameters[1];
+		
+		$found = false;
+		
+		// 1) Detect campaign from query string
+		if(isset($this->currentUrlParse['query']))
+		{
+			$found = $this->detectCampaignFromString($this->currentUrlParse['query']);
+		}
+
+		// 2) Detect from fragment #hash
+		if(!$found
+			&& isset($this->currentUrlParse['fragment']))
+		{
+			$found = $this->detectCampaignFromString($this->currentUrlParse['fragment']);
+		}
+		return $found;
+	}
 
 	/*
 	 * We have previously tried to detect the campaign variables in the URL
diff --git a/plugins/CoreHome/templates/header.tpl b/plugins/CoreHome/templates/header.tpl
index 239b956bc9..fd4fe450a5 100644
--- a/plugins/CoreHome/templates/header.tpl
+++ b/plugins/CoreHome/templates/header.tpl
@@ -1,5 +1,4 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
-   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <title>{if !$isCustomLogo}Piwik &rsaquo; {/if} {'CoreHome_WebAnalyticsReports'|translate} - {$siteName}</title>
diff --git a/plugins/Live/templates/visitorLog.tpl b/plugins/Live/templates/visitorLog.tpl
index 0ef46a0349..7e2b4d7b93 100644
--- a/plugins/Live/templates/visitorLog.tpl
+++ b/plugins/Live/templates/visitorLog.tpl
@@ -223,7 +223,7 @@
 				{else}
 				{* Goal conversion *}
 					<img src="{$action.icon}" /> 
-					<strong>{$action.goalName}</strong>
+					<strong>{$action.goalName|escape:'html'}</strong>
 					{if $action.revenue > 0}, {'Live_GoalRevenue'|translate}: <strong>{$action.revenue|money:$javascriptVariablesToSet.idSite}</strong>{/if}
 				{/if}
 				</li>
diff --git a/tests/integration/TrackCustomVariablesAndCampaigns_ForceUsingVisitIdNotHeuristics.test.php b/tests/integration/TrackCustomVariablesAndCampaigns_ForceUsingVisitIdNotHeuristics.test.php
index b1226a46fe..8d9fe5be9f 100755
--- a/tests/integration/TrackCustomVariablesAndCampaigns_ForceUsingVisitIdNotHeuristics.test.php
+++ b/tests/integration/TrackCustomVariablesAndCampaigns_ForceUsingVisitIdNotHeuristics.test.php
@@ -53,7 +53,7 @@ class Test_Piwik_Integration_TrackCustomVariablesAndCampaigns_ForceUsingVisitIdN
         $t = $this->getTracker($idSite, $dateTime, $defaultInit = true);
 
         // Record 1st page view
-        $t->setUrl( 'http://example.org/index.htm?utm_campaign=GA Campaign&piwik_kwd=Piwik kwd&utm_term=GA keyword SHOULD NOT DISPLAY' );
+        $t->setUrl( 'http://example.org/index.htm?utm_campaign=GA Campaign&piwik_kwd=Piwik kwd&utm_term=GA keyword SHOULD NOT DISPLAY#pk_campaign=NOT TRACKED!!&pk_kwd=NOT TRACKED!!' );
         $this->checkResponse($t->doTrackPageView( 'incredible title!'));
         
         $visitorId = $t->getVisitorId();
@@ -96,7 +96,7 @@ class Test_Piwik_Integration_TrackCustomVariablesAndCampaigns_ForceUsingVisitIdN
         $t3->setForceVisitDateTime(Piwik_Date::factory($dateTime)->addHour(1.3)->getDatetime());
         // fake a website ref cookie, the campaign should be credited for conversion, not referrer.example.com nor example.org 
         $t3->DEBUG_APPEND_URL = '&_ref=http%3A%2F%2Freferrer.example.com%2Fpage%2Fsub%3Fquery%3Dtest%26test2%3Dtest3';
-        $t3->setUrl( 'http://example.org/index.htm?pk_campaign=CREDITED TO GOAL PLEASE' );
+        $t3->setUrl( 'http://example.org/index.htm#pk_campaign=CREDITED TO GOAL PLEASE' );
         $this->checkResponse($t3->doTrackGoal($idGoal, 42));
 	}
 }
-- 
GitLab