Tracking Google Campaigns with # Anchor


#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&param2=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 Aubry) #2

Thanks for the report, indeed this is an issue we will fix in the next few weeks, see the ticket: Campaign tracking parameters should be also detected in the URL hash tag (fragment) · Issue #2475 · matomo-org/matomo · GitHub


#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 Aubry) #4

nterry it would be appreciated if you can post your code using setCustomUrl() in the ticket for this feature! thanks


#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() ;