Jump to content

Working with the Matomo Cookie


iwato

Recommended Posts

OBJECTIVE:  Suppress my site's splash panel for repeat visitors using a mechanism similar to the one that I use to suppress the panel for visitors whose viewports are too small to handle it properly.

BACKGROUND:  As I have instructed Matomo not to recognize me when I access the application I have no way of testing whether my code is working.  According to a friend who has experimented with his own computer it does not.  Although I am able to retrieve the cookie set by Matomo, I am now uncertain about how to handle it.

NOTE:  The cookie that Matomo appears to set is not set until the page has

The COOKIE Sent by Matomo

Array ( [_pk_id_1_28d8] => b1dccabc0123a611.1529214805.24.1530731060.1530731060. [_pk_ses_1_28d8] => * )

The PHP that Recovers the COOKIE and Assigns It to a Javascript Variable

if (isset($_COOKIE['_pk_id_1_28d8'])) {
    $cookie_set = 1;
}

The JAVASCRIPT on the Same Page as the PHP

var cookie_set = <?php echo $cookie_set;?>;

The JAVASCRIPT on a Loaded Script Page that Controls the Splash Panel

if ((!refer_type && !navtype && (viewportWidth > 360)) || !cookie_set) { [the code that sets the splash panel] }

QUESTION:  Can you see anything in the above logic or design that could be showing the splash panel when it is not desired?

Roddy

Edited by iwato
Link to comment
Share on other sites

If you didn't give $cookie_set a default value, trying to print it into Javascript is going to result in an error.  This is an error:

var cookie_set = ;

It's also worth noting that for people like myself who use various tracking and ad blockers, Matomo will not be loaded at all and so will never set a cookie.  It's also advisable to create an environment that is possible for you to test.

This seems like a perfect use case for localStorage, I'm not sure why you're trying to use a third-party service to control what happens on your site.

Link to comment
Share on other sites

Why is it that you believe that I did not give cookie_set a default value?

Are you suggesting with the following that I set two cookies:  one with Matomo and one directly with PHP?

Quote

It's also worth noting that for people like myself who use various tracking and ad blockers, Matomo will not be loaded at all and so will never set a cookie. 

For the moment,  a local test environment that would be all convenient is impossible.

Quote

I'm not sure why you're trying to use a third-party service to control what happens on your site

I agree with you; it is generally foolish to depend on a third party service to control my site, but in this case the third party service is on my host server.  It belongs to me.

Roddy

Link to comment
Share on other sites

Quote

Why is it that you believe that I did not give cookie_set a default value?

I didn't say that I thought you didn't set a default value, I'm just pointing out that, if you didn't, it would cause an error.  You didn't show that you set a value other than in that if statement.  Obviously, I can only comment on code that I'm being shown.  I'm not a mind reader.

 

Quote

Are you suggesting with the following that I set two cookies:  one with Matomo and one directly with PHP?

 

I wouldn't use cookies at all, I would use localStorage.  Since you are controlling the display of this with Javascript, I also wouldn't use PHP.

Quote

For the moment,  a local test environment that would be all convenient is impossible.

You've had Matomo ignore you in a way that is literally impossible to undo?

Quote

I agree with you; it is generally foolish to depend on a third party service to control my site, but in this case the third party service is on my host server.  It belongs to me.

I understand that.  I just think it's weird to use a third-party to determine whether or not someone has visited or otherwise interacted with your site when it's so trivial to do that yourself.  There is literally no reason to add that dependency to your code.  A few lines of Javascript can achieve this, you don't need to depend on any third parties or any other languages.  If you want to add additional complexity, that's up to you.  Just be aware that you're also introducing possible errors, like tracking code not being downloaded and ran, tracking cookies not being set, etc.  With all of that, it sounds like a lot of complexity just to figure out if someone has been to your site before or clicked on some button.

Link to comment
Share on other sites

Quote

I didn't say that I thought you didn't set a default value, I'm just pointing out that, if you didn't, it would cause an error.  You didn't show that you set a value other than in that if statement.  Obviously, I can only comment on code that I'm being shown.  I'm not a mind reader.

I believe we were talking past one another.  Please compare the following two pieces of code and explain why the variable refer_type works and the variable cookie_set fails.  Is it because the else-statement is creating a default value?

The PHP

if (isset($_GET['refer_type']) && filter_var($_GET['refer_type'], FILTER_VALIDATE_INT)) {
$refer_type = $_GET['refer_type'];
} else {
    $refer_type = 0;
}

if (isset($_COOKIE['_pk_id_1_28d8'])) {
    $cookie_set = 1;
}

The Javascript

var refer_type = <?php echo $refer_type;?>;
var cookie_set = <?php echo $cookie_set;?>;

Roddy

Link to comment
Share on other sites

When you say a variable fails, what specifically do you mean, how does a variable fail?  What is the output?  Look at the code in the browser and see what is being printed on that Javascript line.

If that's all the code you have, then obviously if that if statement is not true then $cookie_set will not be defined, and that's going to produce a PHP error when you try to print it.  If you have error messages enabled, it's going to print the error message in the middle of your Javascript code, which is obviously going to be a syntax error in Javascript.  If error messages are being ignored then it's going to not print anything and that's also a syntax error in Javascript.

Link to comment
Share on other sites

So, the following statement is true.  Is this not correct?

Quote

Is it because the else-statement is creating a default value?

Roddy

Link to comment
Share on other sites

It's not as simple as yes or no (and that's a question, not a statement), that's why I posted the response I did.  For example, I don't know what you mean when you say a variable "fails".  Variables only hold values, they do not succeed nor fail, so I don't know what you mean by that.  So, I would refer you to my previous response.

In particular, you need to look at the output of your PHP code that gets sent to the browser to figure out how the browser is going to respond to it.  If your Javascript code has an error, instead of looking at the PHP code that generates the Javascript, look at the output that the browser gets.  That's where to start.  Once you know what the problem is with the Javascript code then you figure out what PHP code you need to use to fix it, if you're going to use PHP to output Javascript.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...