Forums / General Questions / How I Added Google Analytics Tracking Code instead of using a plugin

How I Added Google Analytics Tracking Code instead of using a plugin

Views: 17,399

Results 1 to 20 of 23
18 Jun 2012, 12:57 PM
#1
ardhill avatar

ardhill

New Zenner

Join Date:
Dec 2007
Location:
Belfast, N.Ireland
Posts:
99
Plugin Contributions:
0

How I Added Google Analytics Tracking Code instead of using a plugin

How I put the Google Analytics in my ZenCart without A plugin

The Short Answer

Get your Google Analytics Code, open this file on your Zen Cart site:
\includes\templates\MY_Custom_Template\common\tpl_main_page.php

Scroll to the bottom of the file and just before the final line that is:

</body>

Paste in your code from google. Save the file, upload to your live site and that's it.

The Longer Answer

I wanted to add the Google Analytics code (GA code) direct to my site and not to use a plugin for my own reasons. But when I got the tracking code from Google, their instructions were to:

Google:

  1. Paste this code on your site
    Copy the following code, then paste it onto every page you want to track immediately before the closing </head> tag.

So, I did the sensible thing and created a new google-analytics.js file, and uploaded it to:
*\includes\templates\MY_Custom_Template\jscript*

Then I couldn't figure out why it wasn't working.

I searched about on here, and found that some other had the same issue, and suggested adding it before the </body> tag instead. I was a bit concerned, because the post I read were from a while ago, and things change online so quickly.

But, I had a go at the method I mentioned above (see my short answer), and the new Real-Time beta reporting on GA showed results immediately :) So, all I know that this method works today 18 June 2012 on Zen Cart v 1.5.0

I wrote this to hopefully save others having to search through tons of posts in mixed threads to find the answer to this problem.

Should you want a Plugin instead, you could try this one: http://www.zen-cart.com/downloads.php?do=file&id=272
(I am not connected with this)

If you want a bit more help with the whole process of setting up a GA account, getting the code and integrating it, one of the contributors on here wrote an article on this: http://pro-webs.net/tutorials/setting-up-google-analytics-w-conversion-tracking-basic/

I don't know if the way that I have outlined here is the 'proper' or 'recommended' way of doing this, but it appears to work.

I hope this helps to save some some time for some people.

Paul

18 Jun 2012, 1:32 PM
#2
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: How I Added Google Analytics Tracking Code instead of using a plugin

So, I did the sensible thing and created a new google-analytics.js file, and uploaded it to:
\includes\templates\MY_Custom_Template\jscript\

Then I couldn't figure out why it wasn't working. If you named the file like that, it would not work in any case. You need to start filenames in /jscript/ folders with jscript_, so your file could be like jscript_google-analytics.js.

18 Jun 2012, 3:19 PM
#3
ardhill avatar

ardhill

New Zenner

Join Date:
Dec 2007
Location:
Belfast, N.Ireland
Posts:
99
Plugin Contributions:
0

Re: How I Added Google Analytics Tracking Code instead of using a plugin

gjh42:

If you named the file like that, it would not work in any case. You need to start filenames in /jscript/ folders with jscript_, so your file could be like jscript_google-analytics.js.

Sorry, yes you are quite right - my bad.
I was more concerned about writing the post, and by the time I had written it, I had done it the other way on Zen Cart and deleted the .js file.

Thanks for clearing up any confusion.

Paul

3 Oct 2012, 4:30 PM
#4
jaspalx avatar

jaspalx

New Zenner

Join Date:
Oct 2012
Posts:
1
Plugin Contributions:
0

Re: How I Added Google Analytics Tracking Code instead of using a plugin

Hi ardhill, may be a silly question but when you created the jscript_google-analytics.js file, did you remove the <script></script> tags? I just tried it and it seems to work fine. FWIW, I used jscript_ga.js

4 Oct 2012, 8:03 AM
#5
ardhill avatar

ardhill

New Zenner

Join Date:
Dec 2007
Location:
Belfast, N.Ireland
Posts:
99
Plugin Contributions:
0

Re: How I Added Google Analytics Tracking Code instead of using a plugin

JaspalX:

Hi ardhill, may be a silly question but when you created the jscript_google-analytics.js file, did you remove the <script></script> tags? I just tried it and it seems to work fine. FWIW, I used jscript_ga.js

Yes, you are correct.

These .js files are loaded and run as Javascript by Zen Cart, so you don't need to add the <script></script> tags.

In fact the ZenCart wiki states:

ZenCart wiki:

NOTE: jscript_*.js files must not contain any <script...> .... </script> tags. They should contain ONLY the contents that are to go "between" those tags.
http://www.zen-cart.com/wiki/index.php/Customisation_-_Templates_-_Javascript#JavaScript_Override_How-To

7 Oct 2012, 10:15 AM
#6
stickleback avatar

stickleback

New Zenner

Join Date:
Oct 2012
Posts:
3
Plugin Contributions:
0

Re: How I Added Google Analytics Tracking Code instead of using a plugin

Hi there,

I'm trying to find a way to add Google -remarketing code to my pages - this seemed like a simple way to "hardwire" it into the page code - I tried, but it simply doesn't show up...

I'm a little lost - any help would be most appreciated....

Thanks

8 Oct 2012, 2:13 PM
#7
ardhill avatar

ardhill

New Zenner

Join Date:
Dec 2007
Location:
Belfast, N.Ireland
Posts:
99
Plugin Contributions:
0

Re: How I Added Google Analytics Tracking Code instead of using a plugin

stickleback:

I'm trying to find a way to add Google -remarketing code to my pages

Personally, I had to google - "Google remarketing code" to see what you were talking about :blush:

Sorry, but right now I personally just don't have time to read it up and play with the code. Maybe you could start a new thread on this as there aren't that may people involved in this thread. Others may be able to help that won't read this thread. There is bound to be other people who have done what you want, or are working on it and read the forums here.

BTW, do you have a short desription of what Google remarketing is?

Regards,

Paul

14 Aug 2013, 10:27 PM
#8
marton_1 avatar

marton_1

Totally Zenned

Join Date:
Apr 2013
Location:
eglisau switzerland
Posts:
568
Plugin Contributions:
0

Re: How I Added Google Analytics Tracking Code instead of using a plugin

Thanks for this; Google Analytics works now.
Reports are strange. Google analytics shows one visitor per day & google adwords show 30/40?

24 Sep 2013, 11:08 PM
#9
painted_hills_nb avatar

painted_hills_nb

New Zenner

Join Date:
Apr 2012
Location:
Fossil, OR
Posts:
35
Plugin Contributions:
0

Re: How I Added Google Analytics Tracking Code instead of using a plugin

ardhill:

How I put the Google Analytics in my ZenCart without A plugin

The Short Answer

Get your Google Analytics Code, open this file on your Zen Cart site:
\includes\templates\MY_Custom_Template\common\tpl_main_page.php

Scroll to the bottom of the file and just before the final line that is:

</body>

Paste in your code from google. Save the file, upload to your live site and that's it.

Paul

Thanks Paul. Worked perfectly! :clap:

Christine
"I believe the most important single thing, beyond discipline and creativity is daring to dare." ~Maya Angelou

4 Oct 2013, 8:40 PM
#10
aestheticdesign avatar

aestheticdesign

New Zenner

Join Date:
Aug 2011
Posts:
29
Plugin Contributions:
0

Re: How I Added Google Analytics Tracking Code instead of using a plugin

I was previously referencing an external file and I can't figure out where I have the code that's referencing it. It stopped working because Google changed the code. I added the new script to the html_header.php file so it would be right before the </head> tag. But I need to delete the old script reference and I can't figure out where it's coded.

Here's a snippet of /includes/templates/mytemplate/common/html_header.php

<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-43155911-1', 'oldsummit.com');
  ga('send', 'pageview');

</script>
</head>

But the view source shows up like this:

<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-43155911-1', 'oldsummit.com');
  ga('send', 'pageview');

</script>
[B]<script src='/google_analytics_auto.js'></script>[/B]</head>

I can't find how the google_analytics_auto.js is being inserted. Searching for it using the Developers Tool Kit, or using BBEdit on a local version of the files comes up empty.

Any ideas what could be inserting it? The javascript file itself has been deleted so it's not autoloading it and anyways it is in the root directory, not one of the special javascript directories. It has be referenced somewhere and I can't find it. Using v. 1.5

17 Mar 2014, 5:20 AM
#11
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: How I Added Google Analytics Tracking Code instead of using a plugin

In general, using the autoload feature of the jscript_whatever files would avoid any edits to existing files, thus saving you time and effort during an upgrade. There might possibly be some advantage to having the script show up at the bottom of the page source instead of in the head; an expert on Google & javascript would know.

26 Apr 2017, 1:33 PM
#12
idtags avatar

idtags

Zen Follower

Join Date:
Aug 2005
Posts:
315
Plugin Contributions:
0

Re: How I Added Google Analytics Tracking Code instead of using a plugin

Hello, I am using 1.5.1.

I am not programming savvy and trying to find a good instruction of how and where to add Google conversion tag (code).

Google says:

"Install your tag:
Copy the tag in the box below and paste it between the <body></body> tags of the page you'd like to track. You can then use Google Tag Assistant plugin on Chrome to make sure your tag is correctly placed."

I see suggestion to paste it to: \includes\templates\MY_Custom_Template\common\tpl_main_page.php>

  1. Is this still correct in 2017?

  2. Is there anything else to do? If yes, is there a link with step by step instruction?

Thank you for your answer in advance.

26 Apr 2017, 5:53 PM
#13
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,515
Plugin Contributions:
176

Re: How I Added Google Analytics Tracking Code instead of using a plugin

Simplest way, which doesn't affect any other files, is to create a new file:

/includes/templates/YOUR_TEMPLATE_FOLDERNAME_HERE/jscript/jscript_google_analytics.js

and paste your google snippet in there.

All of the **/includes/templates/YOUR_TEMPLATE_FOLDERNAME_HERE/jscript/jscript_****.js **files will automatically be inserted before the </body> tag.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

26 Apr 2017, 10:04 PM
#14
idtags avatar

idtags

Zen Follower

Join Date:
Aug 2005
Posts:
315
Plugin Contributions:
0

Re: How I Added Google Analytics Tracking Code instead of using a plugin

Thank you for reply.

So, I will create a folder /jscript/ as I do not have one there.

Where can I borrow good jscript_google_analytics.js file to paste Google snippet?

Sorry for this silly question.

27 Apr 2017, 3:42 AM
#15
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,515
Plugin Contributions:
176

Re: How I Added Google Analytics Tracking Code instead of using a plugin

Google gives you their javascript snippet, which is all you need to put into the jscript_google_analytics.js file

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

5 May 2017, 6:06 AM
#16
kcin avatar

kcin

New Zenner

Join Date:
Jan 2014
Location:
Florida
Posts:
39
Plugin Contributions:
0

Re: How I Added Google Analytics Tracking Code instead of using a plugin

Will using the my_template/jscript method listed above allow for advanced features such as conversion tracking and enhanced ecommerce or is a full blown module necessary to get the most out of google analytics?

14 May 2017, 8:12 PM
#17
ezorb avatar

ezorb

Zen Follower

Join Date:
Apr 2006
Location:
Midland TX
Posts:
426
Plugin Contributions:
0

Re: How I Added Google Analytics Tracking Code instead of using a plugin

I use Bing ads and Google ads.
All I want to know is when customer click on Submit Order button is the sale from Bing or Google.
After reading all of this I am confused. No one mention Bing ads. I find it hard to believe I am the only one using Bing.

So could someone please tell me in simple details where to paste both of these codes?

13 Sep 2021, 2:45 AM
#18
rlexyd avatar

rlexyd

Zen Follower

Join Date:
Nov 2006
Location:
Sydney, Australia
Posts:
490
Plugin Contributions:
0

Re: How I Added Google Analytics Tracking Code instead of using a plugin

DrByte:

Simplest way, which doesn't affect any other files, is to create a new file:
**/includes/templates/YOUR_TEMPLATE_FOLDERNAME_HERE/jscript/jscript_google_analytics.js **and paste your google snippet in there.
All of the **/includes/templates/YOUR_TEMPLATE_FOLDERNAME_HERE/jscript/jscript_****.js **files will automatically be inserted before the </body> tag.

Hi DrByte,
I have a new 1.5.7c install using Responsive_Classic.
I installed my google analytics code as you suggested by creating the jscript_google_analytics.js and adding the code to it in /includes/templates/responsive_classic/jscript/.
The google analytics wasn't reading the conversions correctly and after speaking with their tech support, they advised me to paste the code in /includes/templates/responsive_classic/common/html_header.php just ABOVE the </head> tag, before line 215.
After doing so, they advised they can now see it working.

I asked them if I should remove the google .js file I created but they said to leave it.

Was their suggestion of where to input the code correct?
Also, is there really a point of leaving the .js file to run on every page?

*Zen Cart eCommerce Solution - Putting the Dream of Owning an Online Business within reach of anyone!

13 Sep 2021, 12:15 PM
#19
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,119
Plugin Contributions:
56

Re: How I Added Google Analytics Tracking Code instead of using a plugin

rlexyd:

Hi DrByte,
I have a new 1.5.7c install using Responsive_Classic.
I installed my google analytics code as you suggested by creating the jscript_google_analytics.js and adding the code to it in /includes/templates/responsive_classic/jscript/.
The google analytics wasn't reading the conversions correctly and after speaking with their tech support, they advised me to paste the code in /includes/templates/responsive_classic/common/html_header.php just ABOVE the </head> tag, before line 215.
After doing so, they advised they can now see it working.

I asked them if I should remove the google .js file I created but they said to leave it.

Was their suggestion of where to input the code correct?
Also, is there really a point of leaving the .js file to run on every page?
If you've already modified your template's /common/html_header.php to include the Google Analytics code directly, you should delete that .js file since that would cause the analytics to be included twice on each page.

13 Sep 2021, 9:15 PM
#20
nonprofit avatar

nonprofit

New Zenner

Join Date:
Oct 2012
Location:
Philadelphia, PA, USA
Posts:
34
Plugin Contributions:
0

Re: How I Added Google Analytics Tracking Code instead of using a plugin

I, too, have a 1.5.7c site - https://sepialiterature.info.
I, too, was unable to get analytics working per Dr.Byte's directions. I too added the Google analytics snippet before the /public_html/includes/templates/sepia/common/html_header.php. It now seems to be working. As lat9 suggested, I did delete the snippet from the jscript_google_analytics.js file, too.