Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tracking pixel : error 500 in mono language setups #44

Open
miragecraft opened this issue Jan 17, 2024 · 10 comments
Open

Tracking pixel : error 500 in mono language setups #44

miragecraft opened this issue Jan 17, 2024 · 10 comments

Comments

@miragecraft
Copy link

miragecraft commented Jan 17, 2024

Tried with starterkit v3.9.8 and v4.0.3

First I tried to set options like indicated in the docs:

// site/config/config.php

return [
   'debug' => true,
  'daandelange.simplestats.tracking.method' => \daandelange\SimpleStats\SimpleStatsTrackingMode::OnImage,
];

PHP promptly crashed...

I ended up changing the setting directly in src/config/options.php

Then I put <?= $page->simpleStatsImage() ?> right before the </body> tag, which generates the following:

<img alt="simplestats counter pixel" class="simplestats-image" height="1" loading="lazy" src="http://localhost:81/starterkit-4.0.3/notes/counter.png" style="position: absolute; right: 0; pointer-events: none; height: 1px; width: 1px; opacity: 0;" width="1">

The src URL is the page URL + /counter.png

However it fails to load and returns 500 error in the dev console, I'm assuming this is not the intended behavior and that tracking failed.

@Daandelange
Copy link
Owner

Hi, indeed, there's an issue : the plugin is not loaded when loading the config file : SimpleStatsTrackingMode is unknown ... no idea why this used to work correctly when I implemented it.
It was probably not a good idea to use a custom variable type in the config file.

As a temp workaround, you can add this in the top of your config file (correct the path if needed!) :
require_once("./site/plugins/kirby3-simplestats/src/models/SimpleStatsTrackingMode.php");

@miragecraft
Copy link
Author

As a temp workaround, you can add this in the top of your config file (correct the path if needed!) : require_once("./site/plugins/kirby3-simplestats/src/models/SimpleStatsTrackingMode.php");

Great, this allows me to change the setting in site/config/config.php instead of src/config/options.php.

Still getting 500 error for the actual tracking pixel though.

@Daandelange
Copy link
Owner

Are you trying in Kirby 3 ? On my side it's working in the starterkit. (4 is not yet supported)

@miragecraft
Copy link
Author

Yes, I'm testing it with v3.9.8

image

image

@Daandelange
Copy link
Owner

Weird, I'm using Starterkit 3.9.1, I hope that's not where the error lies.
Could you try using die('Something'); at different steps in these functions to find out where it goes wrong ?

[
'pattern' => '(:all)/counter.png',
'language' => '*',
'action' => function ($language, $id) {
return SimpleStats::trackPageAndServeImageResponse( page($id) );
},
],

public static function trackPageAndServeImageResponse(Page $page){
// Correct tracking method ?
if( SimpleStatsTrackingMode::OnImage === option('daandelange.simplestats.tracking.method', SimpleStatsTrackingMode::OnLoad) ){
// Any tracking feature is enabled ?
if(
true===option('daandelange.simplestats.tracking.enableDevices' , true) ||
true===option('daandelange.simplestats.tracking.enableVisits' , true) ||
true===option('daandelange.simplestats.tracking.enableReferers', true) ||
true===option('daandelange.simplestats.tracking.enableVisitLanguages', true)
){
// Does the page exist ?
if( $page && $page->exists() && $page->isPublished() ){
SimpleStats::safeTrack( $page->id() );
//return var_dump(SimpleStats::safeTrack( $page->id() ));
return new Response(base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVQYV2NgYAAAAAMAAWgmWQ0AAAAASUVORK5CYII='), 'image/png', 200);
//header('Content-Type: image/png');
//header("Content-type: image/png");
//echo base64_decode('image/png;base64,'); // Smallest transparent PNG
//echo base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVQYV2NgYAAAAAMAAWgmWQ0AAAAASUVORK5CYII='); // Smallest transparent PNG
//header("Content-type: image/gif");
//echo base64_decode('R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=='); // Smallest transparent GIF
//echo base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'); // Smallest transparent GIF
//exit;
}
}
// Unknown kirby page or nothing to track, return 404
return new \Kirby\Exception\ErrorPageException(['httpCode'=>410]); // Gone / Removed (with error page)
}
return new \Kirby\Exception\ErrorPageException(['httpCode'=>404]); // Not found (with error page)
//return new Response(null, null, 410); // Gone / Removed (no page is served)
//return false; // generates error page
}

@miragecraft
Copy link
Author

Same issue with v3.9.1

image

Can't see anything after I put in the die statements:

<?php

namespace daandelange\SimpleStats;

//return (false===option('daandelange.simplestats.tracking.onLoad', true))?[]:[
//return (SimpleStatsTrackingMode::OnImage===option('daandelange.simplestats.tracking.method', SimpleStatsTrackingMode::OnLoad))?[]:[
//var_dump(option('daandelange.simplestats.tracking.method')); die();
return [
    // Intercept counter pixel on home page
    [
        'pattern' => 'counter.png',
        'language' => '*',
        'action' => function ($language) {
            die('route intercept before');
            return SimpleStats::trackPageAndServeImageResponse( site()->homePage() );
            die('route intercept after');
        },
    ],
    // On all other pages
    [
        'pattern' => '(:all)/counter.png',
        'language' => '*',
        'action' => function ($language, $id) {
            die('route intercept all before');
            return SimpleStats::trackPageAndServeImageResponse( page($id) );
            die('route intercept all after');
        },
    ],
];
    // Generates a router response for serving the tracker image
    public static function trackPageAndServeImageResponse(Page $page){

        die('trackPageAndServeImageResponse');

        // Correct tracking method ?
        if( SimpleStatsTrackingMode::OnImage === option('daandelange.simplestats.tracking.method', SimpleStatsTrackingMode::OnLoad) ){

            // Any tracking feature is enabled ?
            if(
                true===option('daandelange.simplestats.tracking.enableDevices' , true) ||
                true===option('daandelange.simplestats.tracking.enableVisits'  , true) ||
                true===option('daandelange.simplestats.tracking.enableReferers', true) ||
                true===option('daandelange.simplestats.tracking.enableVisitLanguages', true)
            ){
  
                // Does the page exist ?
                if( $page && $page->exists() && $page->isPublished() ){
                    SimpleStats::safeTrack( $page->id() );
                    //return var_dump(SimpleStats::safeTrack( $page->id() ));

                    die('image generation before');


                    return new Response(base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVQYV2NgYAAAAAMAAWgmWQ0AAAAASUVORK5CYII='), 'image/png', 200);

                    //header('Content-Type: image/png');
                    //header("Content-type: image/png");
                    //echo  base64_decode('image/png;base64,'); // Smallest transparent PNG
                    //echo  base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVQYV2NgYAAAAAMAAWgmWQ0AAAAASUVORK5CYII='); // Smallest transparent PNG

                    //header("Content-type: image/gif");
                    //echo  base64_decode('R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=='); // Smallest transparent GIF
                    //echo  base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'); // Smallest transparent GIF
                    //exit;

                    die('image generation after');

                }
            }
            // Unknown kirby page  or nothing to track, return 404
            return new \Kirby\Exception\ErrorPageException(['httpCode'=>410]); // Gone / Removed (with error page)
        }
        return new \Kirby\Exception\ErrorPageException(['httpCode'=>404]); // Not found (with error page)
        //return new Response(null, null, 410); // Gone / Removed (no page is served)
        //return false; // generates error page
    }

@miragecraft
Copy link
Author

Btw. are you scrolling down the page?

The lazy loading means the browser won't try to load the image until you scroll down some ways.

@Daandelange
Copy link
Owner

Daandelange commented Jan 17, 2024

Yes, my image gets loaded without scrolling, I see a 1x1 image here for example : http://kirby-maegazine/en/photography/sky/counter.png.

But wait, I think I haven't tested in mono-language starterkit (mine is multilang), and there is a language argument in routes.php, so that's probably crashing your kirby.

Edit:
It's not clear to me how to route both single and multilanguage requests in terms of Kirby-coding-style...

// Single language
    [
        'pattern' => 'counter.png',
        'action' => function () {
            return SimpleStats::trackPageAndServeImageResponse( site()->homePage() );
        },
    ],
    [
        'pattern' => '(:all)/counter.png',
        'action' => function ($id) {
            return SimpleStats::trackPageAndServeImageResponse( page($id) );
        },
    ],

This (above) is the single language alternative you can use as a quickfix replacement, but Im not sure how to define a combined one; adding both multi+single routes breaks multilang. Not sure how to fix this the intended kirby way.

This topic is close and might provide 2 solutions.

@miragecraft
Copy link
Author

Great! It's working now with the single language code snippet.

Let me know if you want me to close the issue or leave it open.

@Daandelange
Copy link
Owner

I'll leave it open as this needs to be fixed, thanks for your reports :)

@Daandelange Daandelange changed the title Tracking pixel causes 500 error? Tracking pixel : error 500 in mono language setups Jan 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants