nterry
December 26, 2011, 1:27am
1
I am trying to set up Piwik tracking on a site that is already being tracked by Google Analytics. The site was designed to work with Campaign Parameters passed preceded by #. For example:
http://www.example.com/subdir/?param1=x¶m2=z #utm_source=source&utm_medium=medium&utm_campaign=campaign&utm_content=content
This works in GA because the tracker has _setAllowAnchor set to true. See docs here
Is it possible to configure Piwik to recognise these Google Campaigns?
matthieu
(Matthieu Aubry)
December 26, 2011, 8:45am
2
nterry
December 26, 2011, 6:13pm
3
In the meantime I have a work around. I grab the google code from the URL with split("#")[2]
and then process it to the pk_ form, then reassemble the URL and use setCustomUrl to push this to Piwik.
Seems to work fine, but a more general version would be better for sure.
Thanks for the prompt response. I’m sorry I can’t work out how to format this to not insert a smiley above, replace with )
matthieu
(Matthieu Aubry)
December 27, 2011, 3:25am
4
nterry it would be appreciated if you can post your code using setCustomUrl() in the ticket for this feature! thanks
nterry
December 27, 2011, 8:49pm
5
Here is my code. It’s not pretty and it’s not optimized, but it works:
Replace the smileys with right-parenthases
var googleTracking = document.location.hash.substring(1).split("&") ; // Map the Google Tracking
paramChar = (document.URL.indexOf("?") == -1) ? "/?" : "&" ; // There may be a ?param we need to preserve
var x = 0,utmSource,utmMedium,utmCampaign,utmTerm,pkCampaign = "",pkTerm = "" ;
while (x < googleTracking.length)
{
switch (googleTracking[x].split("=")[0])
{
case "utm_source": pkCampaign = googleTracking[x].split("=")[1]; break ;
case "utm_medium": pkCampaign = googleTracking[x].split("=")[1]; break ;
case "utm_campaign": pkCampaign = googleTracking[x].split("=")[1]; break ;
case "utm_term": pkTerm = googleTracking[x].split("=")[1]; break ;
default:
}
x++ ;
}
if (pkCampaign != "")
{
pkCampaign = paramChar + "pk_campaign=" + pkCampaign ;
if (pkTerm != "") pkTerm = "&pk_term=" + pkTerm ;
} else
{
if (pkTerm != "") pkTerm = paramChar + "pk_term=" + pkTerm ;
}
var customUrl = document.URL.split("#")[0] + pkCampaign + pkTerm ;
....
And later in the code
....
var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", PiwikID) ;
piwikTracker.setCustomUrl( customUrl ) ;
piwikTracker.trackPageView() ;