Sunday, 15 March 2015

Google Analytics

Install the following modules

  1. Google Analytics 7.x-2.1
  2. Google Analytics Event Tracking 7.x-1.2
  3. Enable 'Google Analytics' and 'Google Analytics Event Tracking'

Install chrome extensions for debug

  1. install and configure 'Google Tag Assistant'

Set up Google Analytics account 

  1. Go to google analytics website and sign up or sign in an account : http://www.google.com/analytics/
  2. Follow the website step and go to the 'Admin/Property Settings'. The configuration is supposed to be similar as below:

    Note:  The value of 'Default URL' can be a website URL on your local computer, but it must respect the URL format, so URLs like 'http://localhost' is not allowed. In my case, I'm using 'http://drupal.localhost.com' and configure it in /etc/hosts.

Configure D7 'Google Analytics' module

  1. enter admin/config/system/googleanalytics, paster the 'Property ID' where you get from 'google analytics' on this page.
  2. enable 'internal search' tracking in the page entered by clicking 'Search and Advertising' tab on the left.
  3. toggle debug mode of this module which will output some debug information in 'chrome console' if it's on. And tick 'locally cache tracking code file' in case of downloading analytics.js from google every time visiting the website.
  4. click some links in the website and check the result on 'Google Analytics' website by accessing 'Overview' of 'Real Time' on 'Reporting' page.

    Note: 'Real-Time' is for instant tracking which is quite useful for developing and 'Audience' is for the actual website report.
  5. Pay attention that D7 'Google Analytics' is using analytics.js which is a more recent way of tracking with it's guide on the page: https://developers.google.com/analytics/devguides/collection/analyticsjs/,  while the old ga.js is still bing used by numerous developers.

Try 'Google Analytics Event Tracking' Module

  1. Enable 'Google Analytics Event Tracking Example' module which is a sample module of event tracking residing in 'Google Analytics Event Tracking'  module
  2. Open with your favourite editor the file: sites/all/modules/google_analytics_et/google_analytics_et_example/google_analytics_et_example.module
  3. Find the variable '$selectors' in the function '_google_analytics_et_example_set_selectors_array' where you put the JQuery selector and event to configure the event of the element being tracked. Below is a standard array indicating how to add them.
     array(
          'event' => 'mousedown',      //a JQuery event name
          'selector' => 'a#logo',      //a JQuery selector to figure out the specific element which will be tracked
          'category' => 'Home Link',   //Typically the object that was interacted with (e.g. button)
          'action' => 'click',         //The type of interaction (e.g. click)
          'label' => 'Logo',           //Useful for categorizing events (e.g. nav buttons)
          'value' => 0,                //Values must be non-negative. Useful to pass counts (e.g. 4 times)
          'noninteraction' => TRUE,
        ),
    // An explanation of label parameter: !textThis is what would be returned from the jQuery .text() method.!hrefThe value of the href attribute (handy with anchor //tags).!currentPageThe URL of the current page (this is taken with the code window.location.href
    //There is one more bit that is handy when configuring the module.
    //Passing !test will send an alert to the page when the event is triggered and not send the event to Google Analytics.

  4. Commentate the value of the variable $selectors and make yours
  5. Trigger the event you set up in array $selectors and enter the 'Event' tab of Goolge Analytics Real-Time page to watch the result

Tracking vimeo video in 'Google Analytics Event Tracking Example'

  1. Download vimeo.ga.js from the website: http://www.sanderheilbron.nl/vimeo.ga.js/
  2. Make sure the 'Javascript API' of 'Media: Vimeo' module is enabled referring to another wiki page: Media Vimeo
  3. Modify the file: google_analytics_et_example.module and add vimeo.ga.js in the drupal loaded Javascript list
    /**
     * Implements hook_page_alter().
     */
    function google_analytics_et_example_page_alter(&$page) {
        drupal_add_js(drupal_get_path('module', 'google_analytics_et_example') . '/js/vimeo.ga.js', array('scope' => 'footer'));
    }

  4. Open the content page containing vimeo videos and watching the 'Real-Time' event in Google Analytics.

















No comments:

Post a Comment