Support » Plugin: Jetpack by WordPress.com » Adding Google Analytics to Jetpack Carousel

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Jeremy Herve

    (@jeherve)

    Jetpack Mechanic 🚀

    I would suggest that you try what’s suggested here, that should do the trick!

    ga(‘send’, ‘pageview’) will have already been sent through the normal tracking code. Because carousel doesn’t load a new page, I don’t think it will be sent (at least I don’t see it watching live data on my site)

    I think what needs to be done is:

    ga(‘send’, ‘event’, ‘gallery’, ‘click’, image/path/filename);

    I have tried this with hard-coded generic data ‘image’ But how do I put the actual image/path/filename into a variable?

    Plugin Contributor Jeremy Herve

    (@jeherve)

    Jetpack Mechanic 🚀

    You could try to use something like this to grab the Carousel ID that’s added to the URL when switching between images. Would that work for you?

    Thank you for the help. The following is what I did and it is working. Note to others: You must replace “example.com” and use your own google analytics ID (UA-xxx…).

    The ga(‘create’, may be redundant because it’s probably in your page footer tracking code already. I’m not a Google Analytics expert, so I’m not sure if you need one for each click on a page.

    jQuery(document).on( 'jp_carousel.selectSlide', '.jp-carousel-wrap', function( event, slides ) {
    
    // pass value to google analytics
    ga('create', 'UA-xxxx-1', 'example.com');
    ga('send', 'pageview', { 'page': location.pathname + location.search + location.hash});	
    
    } );

    That puts the full path including the anchor (#) tag into GA. Something like:
    /dir/subdir/pagename/#jp-carousel-1744 — This way you can take a look at your most popular images, trace what people look at next, and even figure out what images they are skipping.

    Because I have a child theme (as most trying this will) I needed “get_stylesheet_directory_uri” in the following (a variation from many examples) to enqueue in my theme_functions.php (vantage_child is just the name of my theme). I used the browser console to debug path errors for the missing script.

    /**
     * enqueue script
     */
    function vantage_child_scripts() {
    	wp_enqueue_script( 'carousel-analytics',  get_stylesheet_directory_uri() . '/js/carousel-analytics.js', array(), '1.0.0', true );
    }
    
    add_action( 'wp_enqueue_scripts', 'vantage_child_scripts' );

    Hi eleganteye,

    nice. Thanks a lot.

    Enrique

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Adding Google Analytics to Jetpack Carousel’ is closed to new replies.