GA Event

by mtenschert

JavaScript

/**
 * ALPINRESORTS.com Analytics Events for ALPINGUARANTY
 * for checkout and product page
 */

// create ga event
function create_ga_event(eventAction, eventLabel) {
  ga('send', {
    hitType: 'event',
    eventCategory: 'ALPINGUARANTY Test',
    eventAction: eventAction, // click
    eventLabel: eventLabel // where? e.g. ps/checkout
  });
}

// location pathname extract
var initial_totalPath = window.location.pathname;
var initial_paths = initial_totalPath.split("/"); // string to [en, ski-rental, france, ...]
var initial_page_ps_key = initial_paths[3]; // extract page name
var initial_page_ps = "undefined";
try {
  initial_page_ps = initial_page_ps_key.split("?"); // because of the product?xyz;
} catch (e) {
  console.log('DEINSCRIPNAME: could not found any ? in URL');
}

// render function
function renderClickInsuranceItems() {
  var insurance_items = $('.product__additional-offer-list-item.insurance');
  
  // clickfunction
  insurance_items.on("click", function() {

    var eventAction = 'click on pill';
    create_ga_event(eventAction, initial_page_ps);

  });
}