2

I am trying to enable site search tracking on my intranet, but because the query parameter is a fragment identifier, I get no result. Here is the URL of a search result for the word "paper": https://intranet.example.com/en/search/intranet#k=paper

I use Google Tag Manager. I know there is a way with the help of a custom JavaScript macro in Tag Manager, but I'm not a programmer so I cannot figure it out.

1 Answer 1

1

The aim is to replace the fragments with parameters.

Function 1, {{Remove Pii from queryText}}: Here is an example: Change the page field in the Pageview event

Notice that function is called Remove Pii. Since GA does not allow Pii info and users can enter it in via the site search. Thus while process the query text it can a good opportunity to remove the Pii.

So that function filters the personal info:

function(){
  try {
  var searchTerm = {{queryTest}};
  searchTerm = searchTerm.replace(/[\w-\.]+@([\w-]+\.)+[\w-]{2,4}/, "[Email]"); #remove email
  searchTerm = searchTerm.replace(/[+]*([(]{0,1}[0-9]{1,4}[)]{0,1}){2,5}[-\s\.\/0-9]{4,}/, "[Number]"); #remove phone number and the like
  return searchTerm
  } catch (err) {
    //console.log(err.message);
    return {{queryTest}};
  } 
}

function 2 {{queryTest}}:

Using this link as a foundation: https://community.swiftype.com/t/how-to-add-query-parameter-before-stq-fragment/468

function(){
  try {
    var Frag = {{Page Path}};
    var qText = new RegExp("\#k=(.+)");
    return Frag.match(qText);
  } catch (err) {
        //console.log(err.message);
        return undefined;
     } 
  }

Note: I have not tested this last function. As my situation had a different source for the queryText.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.