Add script in all pages without src=

Hello guys,

I need to add a specific script on <head>:

<script type="text/javascript">
        dataLayer = [{
            'TyperUser': '" + UserType + "',
            'Screen': '" + PageName + "'
        }];
</script>

but, without src=.

How i need get the page name, i'm trying doing at OnBeginWebRequest, to create a script in my all pages:

but without success.


I need the result in the html <head> to be the same as:


if someone could help me, i would be very grateful.

Tnks.


Hi 

You can add script in Layout (Preparation)to access in all the pages using AddJavaScript Tag

Hope this helps

Hi Agno,

I am assuming you are using a traditional web application.  Also it might be worth providing a little more information on what you are trying to achieve to help community members give you a better answer.

To get the page name, are you looking for the filename or the title?

For the filename, try in javascript

  location.pathname.match(/[^\/]+$/)[0]

For the title, try in javascript

  document.querySelector('title').innerText

There are a few ways you might add the script.

One is to modify your page layout, and put the script at the top.  However, this will still put the script in the <body> and you wanted this script in <head>

Another way would be to use HTTPRequestHandler and use AddPostProcessingFilter with matchingRegexp = "$" (to put it at the end of the head section) then in the replacement add your script as a string.  Be sure to use two double quotes in the string when you want the javascript to include a doublequote.

AddJavaScriptTag will not add the script to <head> either.

I hope this helps!

Kind regards,

Stuart

Thank you Salman and Stuart,

But, I will try to explain better what I need.

I already have two variables that I use (TyperUser and Screen), this is not a problem.

What I need is to insert the script at the beginning <head>, NOT in the end of <head> or <body>:

"<script type="text/javascript">
    window.dataLayer = window.dataLayer || [];
    window.dataLayer.push({
            'TyperUser': 'PJ',
            'Screen': 'MySpecificPage'
        });
</script>"


using only the OnBeginWebRequest, without having to insert the script on every page I have, and

without  src= in the script.


Example:


Thank you.

Solution

Hi Agno,

Using AddPostProcessingFilter with matchingRegexp = "^" should put it at the beginning of <head>.

Try it in OnBeginWebRequest if you like, but I haven't used it there.  I can only confirm it will work from the page layout.

Kind regards,

Stuart

Solution

Stuart Harris wrote:

Hi Agno,

Using AddPostProcessingFilter with matchingRegexp = "^" should put it at the beginning of <head>.

Try it in OnBeginWebRequest if you like, but I haven't used it there.  I can only confirm it will work from the page layout.

Kind regards,

Stuart

Thanks, but I never used this action,
have any example of how to use?

Thanks Stuart,
it worked perfectly on OnBeginWebRequest.


Result:

Great, thank you Agno, I have learned something today too!