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

    • In addition to the political nature of Elon musk aside. AI Chat bots still have issues that is still a bit of a black box they are still trying to figure and are still not trust worthy to do what they say at this time. This is an area of “go fast and break things” mantra with tech is a very bad idea.
    • Electron sucks. I was much happier when Teams moved off it.
    • iOS 27 will focus on features for Apple's first foldable phone coming 2026 by Taras Buria Foldable smartphones are hardly a novel thing anymore. The form factor has been around for a while, and Samsung, the pioneer of the idea, is already at the seventh generation. Now, with the concept and tech being plenty mature, Apple is joining the party with its first foldable, rumored to arrive in 2026. According to Mark Gurman from Bloomberg, Apple's next iOS 27 update will prioritize new features for the rumored foldable iPhone. The development of iOS 27 is starting soon, and Apple wants to put a lot of focus on the folding experience. With the first foldable iPhone, Apple is entering a well-developed market, and the company is not expected to bring major innovations, unlike the original Apple Vision Pro. Apple is reportedly focusing on a design similar to the Galaxy Z Fold7 with an inward-folding display. Still, the first iPhone is rumored to feature some notable improvements for the inner display crease and the hinge. Foldable phones are becoming increasingly popular in China, and Apple can no longer ignore the market. Offering an expensive foldable iPhone will help the company boost revenue and reignite interest in its main product. There are plenty of users who are interested in giving a book-like foldable a try, but having Android as the only platform choice is a big turn-off for many. With Apple joining the party, the form factor will become much more interesting to many users. Still, those users will have to make sure their pockets are plenty deep. The first foldable iPhone is not expected to cost less than $2,000, so the sticker shock of the original iPhone X from 2017 and Apple Vision Pro from 2023 is coming next year. Source: Bloomberg
    • Hmmm, my fiber still falls under CenturyLink and not Quantum. I am also in Tucson.
  • 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
      498
    2. 2
      Michael Scrip
      205
    3. 3
      ATLien_0
      201
    4. 4
      Xenon
      138
    5. 5
      +FloatingFatMan
      117
  • Tell a friend

    Love Neowin? Tell a friend!