• 0

Using PHP to include analytics.js file?


Question

Hey guys, I'm not very familiar with Analytics (or JavaScript, for that matter). I'm much more familiar with HTML/CSS and PHP.

Normally I just copy/paste the tracking code in <script> before the closing </head> tag like the Analytics page says, instead can I just do it this way:

In an index.html or index.php could I just use <?php include 'analytics.js'; ?> before the head tag

And the .js file will have the following code:


var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-XX']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
[/CODE]

Or will Analytics somehow not work with this sort of setup?

Link to comment
Share on other sites

10 answers to this question

Recommended Posts

  • 0

Hm... I tried this earlier and realized that when you simply include the regular tracking script in a .js file via PHP it shows the script at the top of the page in plain text.

I can't remember/find the code for it to start actually utilizing the code as JS instead of plain text.

And even if I get it, will Analytics read it and track the code properly?

Thanks in advance.

Link to comment
Share on other sites

  • 0

if you want to dump the contents of the .js file into the html, use the php include codethat you wrote above , but it needs to dump it within a set of html script tags, within the html hewd section...

Link to comment
Share on other sites

  • 0

Ohh, I understand. The php has to include it within the script/javascript tags even if it's an external file?

Alright that worked for me.

Thanks again. :D

Link to comment
Share on other sites

  • 0

Can't you just use


<script type="text/javascript">
Javascript code goes here.
</script>
[/CODE]

or does it have to be an external file ?

Link to comment
Share on other sites

  • 0

Can't you just use


<script type="text/javascript">
Javascript code goes here.
</script>
[/CODE]

or does it have to be an external file ?

Eh, I could.

Well, I'm not using this for one thing- I want to use it in a .php file, where I could also technically use it like that. But I want the code to look a bit neater and more organized by reading from an external file, and additionally I am trying to expand my understanding Javascript better. As mentioned, I mainly only know HTML, CSS and PHP. I know the most minuscule amount of JS.

Link to comment
Share on other sites

  • 0

Also, excuse the double post, but can't I just include a file using JS in the script tags of the index to include an external .js file?

I looked in to doing it "non-asynchronously" but I got no good results on that. :/

Link to comment
Share on other sites

  • 0

Also, excuse the double post, but can't I just include a file using JS in the script tags of the index to include an external .js file?

I looked in to doing it "non-asynchronously" but I got no good results on that. :/

Sure, just use the src attribute of the script tag in your html to include the js file...

&lt;script src="/path/to/analytics.js"&gt;&lt;/script&gt;

Link to comment
Share on other sites

  • 0

Make sure you close the php tags before you use that script.

Unless you are trying to echo it;

&lt;?
echo "&lt;script src=\"/path/to/analytics.js\"&gt;&lt;/script&gt;";
?&gt;

Link to comment
Share on other sites

  • 0

I do believe the way you want it done while is a correct use is just dragged out ? I dont understand why you would not simply include the .JS file it should work fine. (assuming one has used the correct tags)

Link to comment
Share on other sites

  • 0

Sorry, it has been a while since I have been on... and thank you all for the replies.

I do believe the way you want it done while is a correct use is just dragged out ? I dont understand why you would not simply include the .JS file it should work fine. (assuming one has used the correct tags)

Well, no. That is what I am trying to do, find the most clean and organized way to use the JS, which is to simply include it. I was having problems doing that via PHP though.

I realized even if you include it with php, it has to be in the <script> tags (simple mistake I hadn't realized at the time). I think I will try it with the src attribute of the script tag, as mentioned.

Seems like the cleanest and easiest. I just needed guidance because I have not learned JS yet. ;)

Link to comment
Share on other sites

This topic is now closed to further replies.