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?

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.

  • 0
  On 12/07/2012 at 17:53, Freelancer1111 said:

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.

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

  • 0
  On 19/07/2012 at 05:16, PALMERx64 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...

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

  • 0

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

  On 06/08/2012 at 12:23, SPEhosting 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. ;)

This topic is now closed to further replies.
  • Posts

    • Oh no... here we go again. Tha same sh*t that happaned to Mail & Calendar...
    • I too have worked with pro gear for decades and so I found the interface to be unintuitive from both the pro and casual side (a rare "accomplishment" by what are obviously otherwise skilled coders, hehe). I eventually got it to work, thanks for offering, but I found other compatibility issues with my own use case, so I just dropped it entirely. Right now, I just use an analog line out/line in approach which works as expected across all usage scenarios. With both machines on the same power block/outlet, I'm not getting any analog hum or hiss. My next step will be to try the updated Multiplicity 4 when a bug, addressing this very issue unfortunately, is resolved. Knowing Stardock, that could be tomorrow or five years from now, so I check back every few months to see it it's fixed and I want to upgrade. Again, thanks for offering to help.
    • We recognize that performance can use some improvements, and we continue to work on improving it. However, it's worth noting that massive performance improvements don't happen overnight, they take a lot of work and effort, and in most cases, the improvements are more noticeable when you compare across several updates. That said, Files is open-source and everyone is invited to help with these efforts 🙂
    • A lot of effort has gone into improving stability and Files Preview now has a 99% crash free rate. These improvements will make their way to Files Stable when v4 is released later this year. If you're still experiencing issues, please report them on GitHub or Discord so we can track them properly.
  • Recent Achievements

    • Rookie
      Snake Doc went up a rank
      Rookie
    • First Post
      nobody9 earned a badge
      First Post
    • One Month Later
      Ricky Chan earned a badge
      One Month Later
    • First Post
      leoniDAM earned a badge
      First Post
    • Reacting Well
      Ian_ earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      495
    2. 2
      Michael Scrip
      203
    3. 3
      ATLien_0
      197
    4. 4
      Xenon
      137
    5. 5
      +FloatingFatMan
      115
  • Tell a friend

    Love Neowin? Tell a friend!