0

I have complex php tracker where it stores user clicks etc.I am giving sample code here for pixel.php

session_start();
header('Content-type: image/png');
echo gzinflate(base64_decode('6wzwc+flkuJiYGDg9fRwCQLSjCDMwQQkJ5QH3wNSbCVBfsEMYJC3jH0ikOLxdHEMqZiTnJCQAOSxMDB+E7cIBcl7uvq5rHNKaAIA'));

        if (!isset($_SESSION['track']) || $_SESSION['track'] == '')
        {
        $_SESSION['track'] = "New User";
    //code to insert new user
        }
        else
        {
        $_SESSION['track'] = "Old User";    
    //codes to update old user
        }

Now I want to fire pixel when user clicks on my sign up forms or some bottons.So I use below code

  <script>
  function firepixel(val) {
    var img = document.createElement("img");
    img.setAttribute("src", "https://www.stimulatemind.com/track/pixel.php?id="+val);
    /* set other attributes here */
    document.body.appendChild(img);
  }
</script>


<script>
 $(document).on('click', '[data-pixel]', function() {

 var val = $(this).data("pixel"); 

firepixel(val);


}) ;
</script>

This code is working perfectly fine in desktops and ios , but for android chrome and few other browsers.New Session is created every time when the pixel.php is called.

I tried jquery $.get $.getJSON , etc , but everytime new session is created.

Any alternative or crossbrowser method to do this ? How to Google analytics does this ?

Gracie williams
  • 1,287
  • 2
  • 16
  • 39

1 Answers1

0
 header('Content-type: image/png');
 echo gzinflate(base64_decode('6wzwc+flkuJiYGDg9fRwCQLSjCDMwQQkJ5QH3wNSbCVBfsEMYJC3jH0ikOLxdHEMqZiTnJCQAOSxMDB+E7cIBcl7uvq5rHNKaAIA'));
 session_start();

1) If its a PNG image storing it gzipped just inflates its size.

2) Why are you bothering to create an image when you could be doing this more resiliently using an AJAX or SJAX request and avoiding a repaint

3) you are starting your session after you have flushed the headers

4) your error logging is not working

symcbean
  • 47,736
  • 6
  • 59
  • 94
  • because YOU ARE STILL STARTING YOUR SESSION AFTER FLUSHING YOUR HEADERS – symcbean Apr 10 '18 at 08:38
  • Nope , I changed the code , session_start() is in beginning , Actually all these working in 95% of my visitors , for few android devices , its happening – Gracie williams Apr 10 '18 at 15:22
  • **YOU ARE STILL STARTING YOUR SESSION AFTER FLUSHING YOUR HEADERS** – symcbean Apr 10 '18 at 20:33
  • how do u tell , from my question ? I am telling I changed in my code , and yea now I changed in question too.. session_start() is in beginning now.. 3 to 5 % of visitors session is recreated – Gracie williams Apr 11 '18 at 03:09
  • Well If the user browsers are clearing the cookies, then they will appear as new users. You can try to track users based on their ips. But event that is not guaranteed to work 100%. User tracking is not fool proof. Google Analytics can also make mistakes – Nadir Latif Apr 11 '18 at 04:49