Learn business growth with Google Analytics 4 Forums Google Analytics 4 How to include website domain in GA4 Google Analytics? Reply To: How to include website domain in GA4 Google Analytics?

  • Owen

    Member
    28 February 2023 at 10:43 pm

    You’re on the right track. It seems like you already have the “host” as a parameter in your function, but you’re not using it in the properties that you’re setting for the gtag pageview event. To track the hostname, you can simply append the hostname to the “page_path” and “page_location” properties.

    For “page_location”, ideally, it should contain the complete URL such that it includes the protocol (http or https), hostname and path.

    For “page_path”, it encompasses the page’s path and other information that comes after the domain name in the URL, you can add the hostname too for your reference.

    Here’s how to do it:

    `javascript
    function pageViewGa(pagePath, pageUrl, host) {
    gtag(“event”, “page_view”, {
    “page_title”: pagePath,
    “page_location”: host + pageUrl,
    “page_path”: host + pagePath
    });
    }
    `
    Just replace “pageUrl” and “pagePath” with your “host + pageUrl” and “host + pagePath” respectively, this will contain your hostname.
    Remember to make sure that “host” contains the correct value. If it doesn’t, you can get it from “window.location.hostname” in JavaScript.