Learn business growth with Google Analytics 4 Forums Google Analytics 4 Understanding gtag parameters and addressing issues with page URL and button text

  • Understanding gtag parameters and addressing issues with page URL and button text

    Posted by Roman on 9 September 2022 at 5:02 am

    Hey buddy! Need a little help here. So, I was messing around with the gtag on my site and, you know, it’s like this now:

    gtag('event', 'CTA Click', {
        'page_url' : 'page_url', // What's supposed to go here? 
        'button_text' : 'button_text', // This's right, right?
        'event_label' : 'CTA Clicks'
      });
    

    I’ve been going through the docs like crazy but still don’t quite get what should really go into those parameters – it’s been a mixed bag for me.

    If I test it using GTM’s preview mode, the parameters show up just as they should in my realtime report. But, when I try clicking those same links outside of preview mode—it’s weird—it’s showing “page_url” instead of the actual URLs for the pages. Two heads are better than one, right? What do you think?

    Amelia replied 12 months ago 3 Members · 2 Replies
  • 2 Replies
  • Oliver

    Member
    29 March 2023 at 10:46 pm

    In the place of ‘page_url’, you need to pass the current page URL in the form of a string. Right now, what it seems like you’re doing is passing a string literal ‘page_url’ instead of the actual page URL. You could use ‘location.href’ to get the URL of the current page. It would look something like this: ‘page_url’ : location.href.

    As for the ‘button_text’, this should be the text that the button users click on is displaying. If you’re placing ‘button_text’ as a string, it will only display as just that – ‘button_text’. However, you likely want it to reflect the actual text. So you need to configure it to grab that, like this: ‘button_text’ : document.getElementById(“button-id”).innerText, assuming you have a button with the id of “button-id”.

    Therefore, your gtag should look something like this:

    `
    gtag(‘event’, ‘CTA Click’, {
    ‘page_url’ : location.href,
    ‘button_text’ : document.getElementById(“button-id”).innerText,
    ‘event_label’ : ‘CTA Clicks’
    });
    `
    This should solve your issue. The ‘page_url’ will then contain the actual URL of the page where the event is happening, and ‘button_text’ will show the actual text that exists on the button. ‘event_label’ can remain as ‘CTA Clicks’. Be sure to replace “button-id” with your actual button’s ID. If the text isn’t changing, make sure that the ID correctly corresponds to the button that’s being clicked.

  • Amelia

    Member
    29 April 2023 at 3:00 am

    For the ‘page_url’, you’re supposed to input the actual URL of the page where the event happened. If you just type ‘page_url’, then, unfortunately, that’s exactly what it will log. For ‘button_text’, stick in the text that appears on the specific button that’s been clicked on your site. That way, you can track specifically which button is getting all the love. Keep in mind that this isn’t the default setup, so you may need a bit of JavaScript to make it happen.

Log in to reply.