{"id":33907,"date":"2019-03-02T06:52:17","date_gmt":"2019-03-02T06:52:17","guid":{"rendered":"http:\/\/getshortcodes.com\/?post_type=docs&#038;p=33907"},"modified":"2019-05-15T11:59:03","modified_gmt":"2019-05-15T11:59:03","slug":"tracking-spoiler-opening-in-google-analytics","status":"publish","type":"docs","link":"https:\/\/getshortcodes.com\/docs\/tracking-spoiler-opening-in-google-analytics\/","title":{"rendered":"Tracking spoiler opening in Google Analytics"},"content":{"rendered":"\n<p>This article presents\u00a0the example\u00a0of JavaScript-code. The code should be placed in the main file of your\u00a0current\u00a0active theme scripts. If you don&#8217;t know where to paste JavaScript-code you can use a special\u00a0<a href=\"https:\/\/wordpress.org\/plugins\/custom-css-js\/\">plugin<\/a>.<\/p>\n\n\n\n<p>Don&#8217;t forget to connect Google Analytics to your website.<\/p>\n\n\n\n<p>It is recommended to add unique CSS-class to the shortcode you are going to track. You can make it in the following way:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><div class=\"su-spoiler su-spoiler-style-default su-spoiler-icon-plus my-custom-spoiler su-spoiler-closed\" data-scroll-offset=\"0\" data-anchor-in-url=\"no\"><div class=\"su-spoiler-title\" tabindex=\"0\" role=\"button\"><span class=\"su-spoiler-icon\"><\/span>Spoiler title<\/div><div class=\"su-spoiler-content su-u-clearfix su-u-trim\"> Content <\/div><\/div><\/pre>\n\n\n\n<p>Now you can use the following JavaScript code:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">jQuery(document).ready(function($) {<br>  \/\/ Handle spoiler title click<br>  $('.my-custom-spoiler').on('click', '.su-spoiler-title', function() {<br>    var $spoiler = $(this).parent('.su-spoiler');<br>    setTimeout(function() {<br>      \/\/ Check that spoiler is open<br>      if (!$spoiler.hasClass('su-spoiler-closed')) {<br>        \/\/ Track event in GA<br>        ga('send', {<br>          hitType: 'event',<br>          eventCategory: 'Spoilers',<br>          eventAction: 'open',<br>          eventLabel: 'Campaign Name'<br>        });<br>      }<br>    }, 100);<br>  });<br>});<\/pre>\n","protected":false},"featured_media":0,"template":"","docs_category":[23],"class_list":["post-33907","docs","type-docs","status-publish","hentry","docs_category-customization"],"_links":{"self":[{"href":"https:\/\/getshortcodes.com\/api\/wp\/v2\/docs\/33907","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/getshortcodes.com\/api\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/getshortcodes.com\/api\/wp\/v2\/types\/docs"}],"wp:attachment":[{"href":"https:\/\/getshortcodes.com\/api\/wp\/v2\/media?parent=33907"}],"wp:term":[{"taxonomy":"docs_category","embeddable":true,"href":"https:\/\/getshortcodes.com\/api\/wp\/v2\/docs_category?post=33907"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}