Book Image

Instant Galleria How-to

By : Nathan Van Gheem
Book Image

Instant Galleria How-to

By: Nathan Van Gheem

Overview of this book

Providing beautiful and usable galleries is an important aspect of the Web today. A gallery solution needs to integrate into your web application easily and seamlessly. Users expect mobile sites that function on par with their desktop counterparts‚Äîespecially for image galleries. In order to accomplish these tasks, you need to use a JavaScript (not Flash) that is extensible and scales to mobile devices. "Instant Galleria How-to" will teach you how to use Galleria in advanced scenarios to become an expert and integrate Galleria into your next project using themes and plugins to accomplish any task. This book teaches you how to use and create themes and plugins,  using the Galleria API through a series of recipes that include a plethora of code examples. You'll be taken through detailed instructions on the usage of JavaScript to customize Galleria. You will create your own theme (mobile friendly), plugin, and learn all the configuration options of Galleria. You'll learn how to customize Galleria by using its extensive API, optimize Galleria, integrate with Google Analytics, create tests for your customization, and integrate into your web application. You'll become an expert user of the Galleria framework, which will enable you to deploy beautiful, mobile friendly galleries for your next web project.
Table of Contents (7 chapters)

Recording image views with Google Analytics (Medium)


This recipe discusses how to track which images are viewed with Google Analytics.

Getting ready

To get us started, we create a new ga-example.html file inside our development directory. Include a couple of sample images so that we can inspect the Google Analytics tracking requests being sent.

The basic HTML structure will look like the following code snippet with the normal Google Analytics code at the bottom of the file:

<!DOCTYPE html>
<html lang="en">
  <head>
    <script src="http://code.jquery.com/jquery.min.js">
    </script>
    <script src="../galleria-1.2.8.min.js"></script>
    <script>
      /* Galleria initialization */
    </script>
  </head>
  <body>
    <!-- Galleria HTML definition -->
    <div id="galleria" style="height: 500px;">
      <a href="images/image1-1600px.jpg">
        <img data-title="Image 1"
             data-description="Image 1 description"
             src="images/image1-200px.jpg"
             data-link="images/image1-1600px.jpg" />
      </a>
      <a href="images/image2-1600px.jpg">
        <img data-title="Image 2"
             data-description="Image 2 description"
             src="images/image2-200px.jpg"
             data-link="images/image2-1600px.jpg" />
      </a>
    </div>
    <script>
  /* Standard analytic JavaScript source. Please replace 
     "<CODE>" with your GA code */
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', '<CODE>']);
  _gaq.push(['_trackPageview']);
  (function() {
    var ga = document.createElement('script');
    ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 
                  'https://ssl' : 'http://www') + 
              '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0];  
    s.parentNode.insertBefore(ga, s);
  })();
    </script>
  </body>
</html>

How to do it...

The Google Analytics JavaScript includes an API to push page tracking manually to Google. In our case, we'll hook into a Galleria image view event and send out a Google Analytics page tracker request. The Google Analytics JavaScript API uses AJAX to send out the tracker request so a new page load isn't required.

<script>
  $(document).ready(function(){
    Galleria.loadTheme(
      '../themes/classic/galleria.classic.min.js');
    Galleria.run('#galleria');
    Galleria.ready(function(options) {
      var galleria = this;
      galleria.bind('loadstart', function(e) {
        var url = e.imageTarget.src;
        var host = window.location.hostname;
        _gaq.push(['_setCustomVar', 1,
                   "galleryView", "yes", 1]);
        _gaq.push(['_trackPageview', 
                  url.substring(url.indexOf(host) + 
                                host.length)]);
      });
    });
  });
</script>

The code pattern here follows much of what has been done throughout the book. We're registering an event handler that'll fire when a new image is shown in the gallery. Then, we're sending the tracker request to the Google Analytics API. We make sure to only get the full path of the image to track in Google Analytics. The host name is not allowed.

How it works...

The Google Analytics API provides an API to push page tracking to it manually in JavaScript. We're just utilizing that API in this example with basic Galleria API programming.

There's more...

Google Analytics has a robust JavaScript API if there is a need to do something more fancy. Check out their documentation available at https://developers.google.com/analytics/devguides/collection/gajs/methods/.