Failing to add tracking code of Matomo

Hi,

I’m trying to add the tracking code of Matomo to my Backlight5 site.

I followed the Using PHPlugins | Backlight guided with no success…

this is my php file:

———————/backlight/custom/phplugins/matomo.php————————

<?php

function head(){
   echo "
<!-- Matomo -->
<script>
  var _paq = window._paq = window._paq || [];
  /* tracker methods like "setCustomDimension" should be called before "trackPageView" */
  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);
  (function() {
    var u="//mystats.domain.com/„;
    _paq.push(['setTrackerUrl', u+'matomo.php']);
    _paq.push(['setSiteId', '2']);
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
  })();
</script>
<!-- End Matomo Code -->
  ";
}

?>

—————————end of php——————————

Either I use „ or ‚ next to the echo command it fails:

Something went wrong
Unexpected error: syntax error, unexpected ‘_paq’ (T_STRING), expecting ‘;’ or ‘,’ in matomo.php on line 19
Please report error at https://discourse.theturninggate.net

so here I am :slight_smile:

Has anyone managed to successfully integrate the code?

Somehow I am just spinning in circles. Other posts here in the forum have not brought me further

Any help is welcome
dr.watson

You need to use the phplugins-pangolin-sample.php file as a starting point. it’s found at /backlight/modules/custom-resources/phplugins/

There’s some code at the beginning of that file that’s necessary to make it all work. So just put your code below the section that reads: // SET USER FUNCTIONS BELOW (around line 227)

this code looks like it has an error with the double commas at the end below rather than a double quote above the text.

Thanks Rod - this was missing…

The page now loads without errors and in the source code I see the matomo code - but no data is collected…

I also removed the double commas - no change either…

At least I am a big step further and will now look what is wrong with the matomo code

Thanks for your help
Dr.Watson

A link to your site would be helpful.

It might just that

    var u="//mystats.domain.com/„;

should be

    var u=\"//mystats.domain.com/\";

And //mystats.domain.com/ might need to replaced with the link to your custom tracker page. Check the documentation for this tracker.

Hi Daniel,

thank you for your contribution.

I had not specified the correct URL to get the tracking to run undisturbed. :blush:

The strange character at the end of the URL was created here in the forum when pasting the code into the post. So all good…

After about 24 hours the tracking just started to work as if by magic - as it is in IT.

I’m not sure if I should insert the backslashes at the URL. The code also seems to work with single quotes. So according to - never touch a running system - so I let it be this way.

Best regards
Dr.Watson

The backslashes are “escape” characters.

This would break:

echo "A "thing" I want to echo."

This would not:

echo 'A "thing" I want to echo.'

But, if for some reason, I want to use double-quotes throughout, I can “escape” them using backslashes:

echo "A \"thing\" I want to echo."

In truth, this is a safer approach and probably what I should do for all examples, except I didn’t in the first place:

echo <<<SCRIPT
	<script>
	$("ul.menu a[href='" + window.location.pathname + "']")
		.parentsUntil('.menu', 'li')
		.addClass("current_page_item")
		;
	</script>
SCRIPT;

As you can see, this allows me to use both double-quotes and single-quotes with impunity.

I don’t know why this works, and I don’t know what to call it so that I can search for an explanation using terms. Ben can probably tell us more.

It has many names. I know it as heredoc and I always forget about it! Here document - Wikipedia

1 Like

Nice! Thanks, Daniel!

1 Like