Keyword

Add gtag search event to module search

  • Uzi
  • Uzi's Avatar Topic Author
  • Offline
  • Premium Member
More
4 months 1 week ago #181358 by Uzi
Hello, 

I'd like to add the following code:
gtag("event", "search", { search_term: "t-shirts" });

Where can i do this?I want to add this to the search module so i can track what people search or try to search in my k2 component.

Please Log in or Create an account to join the conversation.

More
4 months 1 week ago #181361 by JoomlaWorks
Replied by JoomlaWorks on topic Add gtag search event to module search
You would add this in a script tag (along with the rest of your GA snippet) inside your index.php file targetted to ONLY load on the "search" task for the itemlist view in K2 as well as the com_search component.

Something like this...
<script>

    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());

    <?php
        $option = JRequest::getCmd('option');
        $view   = JRequest::getCmd('view');
        $task   = JRequest::getCmd('task');

        if (
            ($option == 'com_k2' && $view == 'itemlist' && $task == 'search') ||
            $option == 'com_search' ||
            $option == 'com_finder'
        ):
    ?>
    gtag("event", "search", { search_term: "t-shirts" });
    <?php endif; ?>

    gtag('config', 'YOUR_ID_HERE');

</script>

Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

Please Log in or Create an account to join the conversation.

  • Uzi
  • Uzi's Avatar Topic Author
  • Offline
  • Premium Member
More
4 months 1 week ago #181363 by Uzi
Replied by Uzi on topic Add gtag search event to module search
Hi, thanks for your reply. I already have my gtag config set up. so i can freely add these tags in my files. But I need to add the search term dynamucally. so i need to have the file from k2 where the ajax search functionalit yis defined so i can add this to the file.. I can also add my own script to check for the specific text input.. maybe that is better.

Please Log in or Create an account to join the conversation.

More
4 months 1 week ago #181365 by JoomlaWorks
Replied by JoomlaWorks on topic Add gtag search event to module search
It's not hard to grab the GET param for simple search result pages. However getting the value of a "live" search doesn't really make sense as the user is constantly typing. Even if you add a delay to grab whatever the finished search string is, the user may click on one of the suggested links and just visit another page. And don't forget that events to GA need to be sent async. So I doubt there's the needed time for all that in the case of live search.

Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

Please Log in or Create an account to join the conversation.


Powered by Kunena Forum