Welcome Guest! To access all forums & features, please register an account or sign-in. → Why register?



Using PHP to include analytics.js file?


10 replies to this topic - - - - -

#1 PALMERx64

    Neowinian

  • 49 posts
  • Joined: 11-July 12
  • Location: United States
  • OS: GNU/Linux
  • Phone: N/A

Posted 12 July 2012 - 01:58

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);
  })();

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


#2 OP PALMERx64

    Neowinian

  • 49 posts
  • Joined: 11-July 12
  • Location: United States
  • OS: GNU/Linux
  • Phone: N/A

Posted 12 July 2012 - 04:14

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.

#3 +theblazingangel

    AutoPatcher Veteran

  • 3,176 posts
  • Joined: 25-March 04
  • Location: England, UK

Posted 12 July 2012 - 11:53

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

#4 OP PALMERx64

    Neowinian

  • 49 posts
  • Joined: 11-July 12
  • Location: United States
  • OS: GNU/Linux
  • Phone: N/A

Posted 12 July 2012 - 17:41

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

#5 Freelancer1111

    Neowinian²

  • 204 posts
  • Joined: 11-December 10
  • Location: Germany

Posted 12 July 2012 - 17:53

Can't you just use



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



or does it have to be an external file ?

#6 OP PALMERx64

    Neowinian

  • 49 posts
  • Joined: 11-July 12
  • Location: United States
  • OS: GNU/Linux
  • Phone: N/A

Posted 19 July 2012 - 05:09

View PostFreelancer1111, on 12 July 2012 - 17:53, said:

Can't you just use



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



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.

#7 OP PALMERx64

    Neowinian

  • 49 posts
  • Joined: 11-July 12
  • Location: United States
  • OS: GNU/Linux
  • Phone: N/A

Posted 19 July 2012 - 05:16

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. :/

#8 CrispCreations

    Neowinian³

  • 468 posts
  • Joined: 19-April 04
  • Location: North West, UK

Posted 19 July 2012 - 08:03

View PostPALMERx64, on 19 July 2012 - 05:16, said:

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

<script src="/path/to/analytics.js"></script>


#9 +Kyle

    Nerdin' it up!

  • 704 posts
  • Joined: 12-February 06
  • Location: Michigan

Posted 26 July 2012 - 13:36

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

Unless you are trying to echo it;

<?
echo "<script src=\"/path/to/analytics.js\"></script>";
?>


#10 SPEhosting

    C++ n00b

  • 2,416 posts
  • Joined: 15-July 08
  • Location: my room
  • OS: windows 7, backtrack 5, OSx 10.6

Posted 06 August 2012 - 12:23

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)

#11 OP PALMERx64

    Neowinian

  • 49 posts
  • Joined: 11-July 12
  • Location: United States
  • OS: GNU/Linux
  • Phone: N/A

Posted 06 September 2012 - 18:50

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

View PostSPEhosting, on 06 August 2012 - 12:23, said:

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. ;)