Shandisa jQuery Kuteerera uye Kupfuura Google Analytics Chiitiko Tracking Kune Chero Kudzvanya

jQuery Teerera Kudzvanya kuti Upfuure Google Analytics Chiitiko Tracking

Ndinoshamisika kuti mamwe masanganiswa uye masisitimu haasanganisire otomatiki Google Analytics Chiitiko Tracking mumapuratifomu avo. Yakawanda yenguva yangu yekushanda pamasaiti evatengi iri kugadzira kuteedzera kweZviitiko kupa mutengi ruzivo rwavanoda pane izvo mushandisi maitiro ari kushanda kana kusashanda pasaiti.

Nguva pfupi yadarika, ndakanyora nezve nzira yekutevera mailto clicks, tel clicks, uye Elementor mafomu ekutumira. Ini ndichaenderera mberi nekugovera mhinduro dzandiri kunyora netarisiro yekuti zvinokubatsira kuti uongorore zviri nani saiti yako kana webhu kushanda kwewebhu.

Uyu muenzaniso unopa nzira dzakareruka dzekubatanidza Google Analytics Chiitiko chekutevera mune chero anchor tag nekuwedzera chinhu che data chinosanganisira iyo Google Analytics Chiitiko Chikamu, Google Analytics Chiitiko Chiitiko, uye Google Analytics Chiitiko Label. Heino muenzaniso we link inosanganisa data element, inonzi gaevent:

<a href="#" data-gaevent="Category,Action,Label">Click Here</a>

Chinodiwa chesaiti yako zvinosanganisira jQuery mairi… iyo iyi script inofambiswa nayo. Kana peji rako ratakurwa, chinyorwa ichi chinowedzera muteereri kune peji rako kune chero munhu anodzvanya chinhu nacho gaevent data… yobva yatora uye kupatsanura chikamu, chiito, uye zita raunotsanangura mukati memunda.

<script>
  $(document).ready(function() {      
    $(document).on('click', '[data-gaevent]', function(e) {
      var $link = $(this);
      var csvEventData = $link.data('gaevent');
      var eventParams = csvEventData.split(',');
      if (!eventParams) { return; }
        eventCategory = eventParams[0]
        eventAction = eventParams[1]
        eventLabel = eventParams[2]
        gtag('event',eventAction,{'event_category': eventCategory,'event_label': eventLabel})
        //alert("The Google Analytics Event passed is Action: " + eventAction + ", Category: " + eventCategory + ", Label: " + eventLabel);
    });
  });
</script>

Chiziviso: Ndakasanganisira yambiro (yakataura) kuti iwe ugone kuyedza izvo chaizvo zvapasa.

Kana iwe uchimhanyisa jQuery paWordPress, iwe unozoda kugadzirisa iyo kodhi zvishoma sezvo WordPress isingakoshe iyo $ shortcut:

<script>
  jQuery(document).ready(function() {      
    jQuery(document).on('click', '[data-gaevent]', function(e) {
      var $link = jQuery(this);
      var csvEventData = $link.data('gaevent');
      var eventParams = csvEventData.split(',');
      if (!eventParams) { return; }
        eventCategory = eventParams[0]
        eventAction = eventParams[1]
        eventLabel = eventParams[2]
        gtag('event',eventAction,{'event_category': eventCategory,'event_label': eventLabel})
        //alert("The Google Analytics Event passed is Action: " + eventAction + ", Category: " + eventCategory + ", Label: " + eventLabel);
    });
  });
</script>

Haisi iyo yakanyanya kusimba script uye ungangoda kuita imwe yekuwedzera kuchenesa, asi inofanirwa kukuita kuti utange!