简体   繁体   中英

Google Tag Manager Virtual Page View

I have a little problem setting up a virtualPageView which should override the URL which is sent to google when no result is present.

Heres what I have as JavaScript code:

function returnNoSearchResultsGoogleTagManagerCode($searchterm){
    if ($searchterm == "") return "";

    $requestUri = $_SERVER['REQUEST_URI'] . "&no_result=".$searchterm;


    $js = "<script>
        $(document).ready(function(){
             dataLayer.push({
                 'event':'empty_result',
                 'virtualPageURL':'".$requestUri."'
                });
            });


    </script>";

    return $js;
}

As you can see, I want to use an event trigger (empty_result).

In google, I use a Trigger to determine if the Page is a no result Page. First i created a custom Variable with custom JS

    function(){
       if (document.getElementsByClassName('ga-no-result').length > 0){
            return true;
       }else{
            return false
       }
   }

The class is set, if the SearchEngine can't find a result. So far so good. I also created a dataLayer variable to hold the virtualPageURL

dataLayer

Now I need an event which is triggered if the variable is true.

事件SearchEmptyResult

Finally I created a Tag with type PageView which is fired when the event occurs:

谷歌标签

Until now it seems okay, the Tag is properly configured (i guess) but if I do a search which has no result, the Page URL is not overridden

在此处输入图片说明

The Tag is properly fired and the variables are filled. The overview of the dataLayer shows a correct dataLayer event. But the PageURL is not overridden... Even if I wait a whole day, the category isn't sent to google.

What am I doing wrong? I would be very thankful if someone would have an idea or even a solution :)

Thanks in advance

exa.byte

UPDATE:

Hey, I forgot to mention, that I want to use the new page variable as the string which google should use to determine the searchterm and the searchcategory In Google Analytics I configuered the search as the "q" parameter and the "no_result" as the category. Is it even possible to change the string which google will parse in the end?

To send a virtual pageview to Google Analytics, the field you need to change is page not {{Page Url}} , also the title field is often used.

That's really the only two things you need to do to send a simple virtual pageview.

Extra: I always start my pagepath with /virtual/ to be able to recognize which ones are virtual pageviews easily in GA

For virtual page view you have to change Field "page" - in your GTM-OnSearchEmptyResult you are changing "{{Page URL}}" - I don't think that's correct way to send virutal pageview. Also if you need to change hostname use Fieldname "hostname".

In preview mode you will not see Page URL changed in Variables Tab, you have to go to the actual GA tag that is fired and check it's values. You can either do this in GTM's preview tool or you can use standard developer tools - Network Tab and see what values are being sent to GA:

在此处输入图片说明

You can see "dl" parameter is the current page, if you set up virtual page you should also see parameter called "dp" this is going to be the new value of page in your GA.

If you want to setup virtual pageview you have to use page instead of {{Page URL}} in your fieldname and for Document title use title in you fieldname. for more field reference of google analytics follow below link https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#hitType .

If you don't want to mess around with custom Tag Manager events it's still possible to use the good old ga method, even if you don't include the Analytics code on the page. You just need to fetch the right tracker dynamically, as explained by Simo Ahava in this thread .

if (typeof ga === "function") {
  ga.getAll().forEach((tracker) => {
    tracker.set('page', '/my/path'); // <- change here
    tracker.send('pageview');
  });  
}

I also put it in a gist here .

thanks for your help. I think I got rid of the Problem and have solved it now. I will describe my solution below:

The solution is quite simple.

  1. I had an error/ spelling error @ google backend. I set the search_category parameter to "no_results", but used "no_result" for implementation... Pretty dumb, but sometimes you just won't see the wood for the trees...

  2. I created a new Trigger as helper "HelperDomReady" to trigger the only if DOM is ready and the variable "isEmptySearch" equals "(bool)true"

Now I can see the searchterms which have no result in google backend in the "sitesearch categories" summary. Since I won't set the parameter at all, if the search had at least one hit, the site-search category shows "not-set" for successful results. Therfore the category-section will only show searches without a hit. Problem solved :)

Disadvantage: The searchterm is also listed in the normal list. But I think this is negligible

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM