#1 2024-12-23 16:25:44

dogen
Member
2024-12-23
5

Image view counts and photoswipe

Hey!

I've set up bootstrap darkroom to open photoswipe directly from the thumbnails and after that the view counts doesnt increase swiping through pictures (since its just showing an overlay and not reloading any pages). Is there any easy way to solve this or any suggestion to other theme or plugins that would make it possible to full screen swipe between pictures and have the hit counter increase?

One idea I had was to use ajax and send a request to ws.php (if there is a method for increasing the hit counter) in the js-file for photoswipe, but I dont think I'm good enough to start messing with that. Is there any url you can send with image id to ws.php to increase the counter?

Any suggestions are welcome!

Offline

 

#2 2024-12-27 00:15:13

dogen
Member
2024-12-23
5

Re: Image view counts and photoswipe

After looking into this further and getting a better understanding about... everything, I have come to the conclusion that the most bullet proof way of logging visits would be to create a log parser that uses the nginx access log and updating the database that way. Seems like a pretty big thing for a piano tuner to do. I'm hoping that I could direct people to using a basic theme without lightbox or using the official app for browsing. My best bet (or laziest that is) is to get the app developer to have visit hits through it. Otherwise a log parser will be my next little project.

Am I missing something here? I've never thought about the complexity combining a highly customizable photo gallery with registering picture visits.

Offline

 

#3 2025-02-18 20:44:18

dogen
Member
2024-12-23
5

Re: Image view counts and photoswipe

I finally got it!

I added a php-script to increase the hits on picture with ID.

Called that script from photoswipe-afterchange and grabbed the image id from url photoswipe.curriten.href. Then fetched the script with the correct ID added. Then I added a photoSwipe afterinit and put the same thing there so it updates the hits both when swiping and when you click a picture from the gallery. Phew, that was hard for a piano tuner.  I wonder why this wasnt implemented with the theme (modus theme)

Offline

 

#4 2025-02-18 21:50:59

OpenPicture
Member
2023-12-19
57

Re: Image view counts and photoswipe

Hello,

Does this also work with Bootstrap Darkroom?

If so, could you explain in more detail what you did?

Offline

 

#5 2025-02-19 11:46:44

dogen
Member
2024-12-23
5

Re: Image view counts and photoswipe

Yes, absolutely! I'm glad if this is of use to anyone else. Since I also like bootstrap darkroom that was the theme I made this mod to. I'll set a reminder to get back to this thread tonight. What kind of installation do you use? Docker?

Offline

 

#6 2025-02-19 19:59:16

dogen
Member
2024-12-23
5

Re: Image view counts and photoswipe

Remeber to change paths!

1. Start off by taking a backup of: piwigo\config\www\themes\bootstrap_darkroom\template\_photoswipe_js.tpl

2.Make a script called update_view_count.php that contains:

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

define('PHPWG_ROOT_PATH', '/app/www/public/');  // Path to your Piwigo installation
include_once(PHPWG_ROOT_PATH.'include/common.inc.php');

// Check if image_id is provided
if (isset($_GET['image_id'])) {
    $image_id = intval($_GET['image_id']);  // Get image_id from the URL and sanitize it

    // Update the view count for the image
    $query = 'UPDATE piwigo_images SET hit = hit + 1 WHERE id = ' . $image_id;
    pwg_query($query);

    // Optional: Display a success message
    echo "View count updated for image ID: " . $image_id;
} else {
    echo "No image ID provided.";
}
?>

3. Put update_view_count.php in the www\local\config folder (where the file config.inc.php is located).

4. Open piwigo\config\www\themes\bootstrap_darkroom\template\_photoswipe_js.tpl and replace the part with photoSwipe.listen('afterChange', function() with:

        photoSwipe.listen('afterChange', function() {

            let url = photoSwipe.currItem.href;

            let match = url.match(/picture\.php\?\/(\d+)\//);
            if (match) {
            fetch("local/update_view_count.php?image_id=" + match[1], { method: "GET" });
            console.log(match[1] + " swipe")
            }

            detectVideo(photoSwipe);
            $('.pswp__button--details').off().on('click touchstart', function() {
                location.href = photoSwipe.currItem.href
            });
        });

5. Add this new section (above photoSwipe.init()):

photoSwipe.listen('afterInit', function() {
    let url = photoSwipe.currItem.href;
    let match = url.match(/picture\.php\?\/(\d+)\//);
    if (match) {
        fetch("local/update_view_count.php?image_id=" + match[1], { method: "GET" });
        console.log(match[1] + " initial load");
    }
});


Thats all. Enable photoswipe by default so it starts up when you click an image (on all platforms). Start off by testing the script in a browser http://url/local/update_view_count.php?image_id=NUMBER to confirm its working. After that you can test clicking and swiping and check the console log in chrome (F12) to make sure it is triggered.

Last edited by dogen (2025-02-19 20:03:43)

Offline

 

#7 2025-02-20 01:52:53

OpenPicture
Member
2023-12-19
57

Re: Image view counts and photoswipe

That's great, I'll try it as soon as I have a moment tomorrow. Thank you so much for sharing.

My first installation of Piwigo was in 2016 with Softaculus in cPanel. I was just curious and wanted to try it out. Since then I've been tinkering with it everywhere because it's just fun and Piwigo offers an incredible number of possibilities.

Offline

 

Board footer

Powered by FluxBB