# Google Analytics

**URL:** https://forum.pinegrow.com/t/google-analytics/1350
**Category:** Javascript
**Created:** [November 7, 2017, 11:16am UTC](https://forum.pinegrow.com/t/google-analytics/1350 "2017-11-07T11:16:27Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![trahald](https://avatars.discourse-cdn.com/v4/letter/t/b487fb/32.png) [@trahald](https://forum.pinegrow.com/u/trahald)
#### Post date: [November 7, 2017, 11:16am UTC](https://forum.pinegrow.com/t/google-analytics/1350/1 "2017-11-07T11:16:27Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![mandarin](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/mandarin/32/806_2.png) [@mandarin](https://forum.pinegrow.com/u/mandarin)
#### Post date: [November 7, 2017, 4:17pm UTC](https://forum.pinegrow.com/t/google-analytics/1350/2 "2017-11-07T16:17:52Z")

</div>

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

> **[Google Analytics Dashboard for WP by ExactMetrics (formerly GADWP)](https://en-gb.wordpress.org/plugins/google-analytics-dashboard-for-wp/)**
>
> Connects Google Analytics with your WordPress site. Displays stats to help you understand your users and site content on a whole new level!

---

<div class="post-metadata">

### Author: ![trahald](https://avatars.discourse-cdn.com/v4/letter/t/b487fb/32.png) [@trahald](https://forum.pinegrow.com/u/trahald)
#### Post date: [November 7, 2017, 4:36pm UTC](https://forum.pinegrow.com/t/google-analytics/1350/3 "2017-11-07T16:36:29Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![mandarin](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/mandarin/32/806_2.png) [@mandarin](https://forum.pinegrow.com/u/mandarin)
#### Post date: [November 8, 2017, 11:06am UTC](https://forum.pinegrow.com/t/google-analytics/1350/4 "2017-11-08T11:06:38Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![trahald](https://avatars.discourse-cdn.com/v4/letter/t/b487fb/32.png) [@trahald](https://forum.pinegrow.com/u/trahald)
#### Post date: [November 8, 2017, 11:24am UTC](https://forum.pinegrow.com/t/google-analytics/1350/5 "2017-11-08T11:24:43Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![mandarin](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/mandarin/32/806_2.png) [@mandarin](https://forum.pinegrow.com/u/mandarin)
#### Post date: [November 8, 2017, 12:06pm UTC](https://forum.pinegrow.com/t/google-analytics/1350/6 "2017-11-08T12:06:59Z")

</div>

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

---

<div class="post-metadata">

### Author: ![trahald](https://avatars.discourse-cdn.com/v4/letter/t/b487fb/32.png) [@trahald](https://forum.pinegrow.com/u/trahald)
#### Post date: [November 9, 2017, 10:21pm UTC](https://forum.pinegrow.com/t/google-analytics/1350/7 "2017-11-09T22:21:36Z")

</div>

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>
```

---

<div class="post-metadata">

### Author: ![mandarin](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/mandarin/32/806_2.png) [@mandarin](https://forum.pinegrow.com/u/mandarin)
#### Post date: [November 10, 2017, 8:08am UTC](https://forum.pinegrow.com/t/google-analytics/1350/8 "2017-11-10T08:08:14Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![abhirupbhabani](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@abhirupbhabani](https://forum.pinegrow.com/u/abhirupbhabani)
#### Post date: [December 5, 2019, 11:37pm UTC](https://forum.pinegrow.com/t/google-analytics/1350/9 "2019-12-05T23:37:17Z")

</div>

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

---

<div class="post-metadata">

### Author: ![RobM](https://avatars.discourse-cdn.com/v4/letter/r/278dde/32.png) [@RobM](https://forum.pinegrow.com/u/RobM)
#### Post date: [December 6, 2019, 12:30pm UTC](https://forum.pinegrow.com/t/google-analytics/1350/10 "2019-12-06T12:30:59Z")

</div>

> [@trahald](#):
>

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

````auto
 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.
````
