Track goal using PHP API after redirecting through external site


#1

Hi,

I am tracking a goal (actually, just the page view) in a PHP redirect (HTTP 302) script using the PHP API with the following code:


PiwikTracker::$URL = 'http://'.$_SERVER['HTTP_HOST'].'/piwik/';
$piwikTracker = new PiwikTracker(1);
$piwikTracker->setUrl($_SERVER['REQUEST_URI']);
$piwikTracker->doTrackPageView('<Page Title>');

This works fine so far. The Problem now is, that the users leave site for one page view before calling this PHP redirect page, like in the following diagram:


'Entrypage X, Piwik-enabled Site' -> 'Custom Page Y, Piwik-enabled Site' -> ... -> 'Custom Page Z, Piwik-enabled Site' -> 'External Page View' -> 'PHP Redirect Page, Piwik-enabled Site'

That means, that all the session information (referer, viewing time, …) from the piwik tracking is lost, and the users are recorded as a new visit coming from ‘External Page’. Is there any way to pass or re-attach the previous visiting session in the PHP redirect page?


(Matthieu Aubry) #2

the only way would be to pass as URL parameters the referrer and then read it in your script and set it as setUrlReferrer


#3

Unfortunately, this doesn’t seem to work.
If I set the request URL of the last accessed Piwik-enabled page in the PHP redirect script with setUrlReferrer(), the page views (and its goals) of the redirect script are recorded as direct entries.

Like this should be correct, right?

  1. Custom Page Z, Piwik-enabled Site:

/* add this site's URI as p_ref URL parameter to external page */
$href_link = url_of(External Page View).'&p_ref='.urlencode($_SERVER['SCRIPT_URI']);

  1. External page will forward p_ref URL parameter to PHP redirect page.

  2. PHP Redirect Page, Piwik-enabled Site:


PiwikTracker::$URL = 'http://'.$_SERVER['HTTP_HOST'].'/piwik/';
$piwikTracker = new PiwikTracker(1);
$piwikTracker->setUrlReferrer($_REQUEST['p_ref']);
$piwikTracker->setUrl($_SERVER['REQUEST_URI']);
$piwikTracker->doTrackPageView('<Page Title>');