Google Analytics

Hello!

I’m doing a Wordpress theme and I want to incorporate the following script in the head:

<! - Global site tag (gtag.js) - Google Analytics ->
<script async src = "https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXX-1"> </ script>
<script>
   window.dataLayer = window.dataLayer || [];
   function gtag () {dataLayer.push (arguments);}
   gtag ('js', new Date ());

   gtag ('config', 'UA-XXXXXXXX-1');
</ script>

Automatically Pinegrow places the two scripts separately, but when I save and export the theme, then in the code only the second script appears and Google Analytics does not receive information.

HTML shows only this, the first part is missing:

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

  gtag('config', 'UA-XXXXXXXX-1');
</script>

Any idea or how to incorporate the code?

Thanks in advance!

I use this plugin on all my WP sites, does a perfect job.

2 Likes

Thank you for your answer!

I don’t know the reason why I can not incorporate a script manually, it has no sense…
I think it is not necessary to use a plugin just for that (I always try to avoid plugins).

Does anyone know any other solution?
I’ve entered the code from the Library option (left column) using the “insert code” option.
Then I drag it in the head and it’s when appears two script.
At the time of export the first script does not appear.

Never used the Library option but you could try putting both bits of the Analytics script into a <div. …

Pinegrow might treat everything in the div as just one piece of code.

1 Like

the Analytics code must be inside the <head>, and placing a <div> in it is a bit weird…

If you do not use the library how do you do to incorporate scripts?

Put in in div just after opening tag or even before closing tag. It will work just fine.

1 Like

Thank you Mandarin for your answers, I really appreciate it.

I can’t place a div inside the head element and have valid code – only metadata is allowed and Google Analyts must be there.

I have also tried to put the Google Tag Manager code and it does not work either.

Could you show me how your built-in?

I want this code:

<head>
<meta charset="<?php bloginfo( 'charset' ); ?>">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap core CSS -->
<link href="bootstrap/css/bootstrap.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="design.css" rel="stylesheet" type="text/css">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
  <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Roboto:300,300italic,400,900,900italic,400italic">
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXX-1"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-XXXXXXXX-1');
</script>
<link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>">
<?php wp_head(); ?>
</head>

But after save and export and upload to FTP shows this:

 <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <!-- Bootstrap core CSS -->
        <!-- Custom styles for this template -->
        <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
        <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
        <div>
            <!-- Global site tag (gtag.js) - Google Analytics -->
            <script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-XXXXXXXX-1');
</script>
        </div>
        <link rel="pingback" href="http://www.website.com/xmlrpc.php">
        <title>XXXXXXXX</title>
<link rel="alternate" hreflang="es" href="http://www.website.com/" />
(etc...)
</head>

All I can really suggest is to add Analytics code in a div elsewhere as suggested where it will run fine or to use the plug in.

Personally I find the plug in to be excellent so have never experienced the issue you are having.

Sorry I cannot help more.

1 Like

Well i m here for the same…Hopefully someone will post an accurate solution here.

Check your functions.php file. I think PG strips those scripts and puts them there.

 wp_deregister_script( 'script-1' );
    wp_enqueue_script( 'script-1', 'https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXX-1', false, null, false);

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

  gtag(\'config\', \'UA-XXXXXXXX-1\');');```
I'm not sure if there is a way to force them to be kept in the head section.