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

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

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;

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

This topic is now closed to further replies.
  • Posts

    • JetBrains is working to cut false positives in RustRover 2026.2 by David Uzondu Recently, JetBrains released the fifth EAP build of its dedicated IDE, RustRover 2026.2, bringing improvements like a Run gutter icon for criterion_main! macro benchmarking and a feature that alerts you when there are unused traits in your current scope. Now, the company is out with a blog post addressing one of the "most common" complaints from users: false positives. In RustRover, a false positive occurs when the editor incorrectly highlights something as an error even though the project compiles and runs successfully. This mismatch flags a gap between the IDE's internal intelligence and the actual compiler. When the editor flashes red warnings over perfectly valid code, developers lose trust in the tool, which stalls momentum. Traditionally, RustRover runs cargo check to detect compiler errors and warnings, but it also relies on its own code analysis engine to power real-time features. To provide quick feedback, this engine parses your source code into a syntax tree while inferring types and resolving names as you type. Because this engine must work on broken, half-written code and react instantly, its logic sometimes diverges from the compiler's, producing false positives that do not exist in the compiler's eyes. JetBrains said that it has a "dedicated task force" focused specifically on identifying and fixing false positives by analyzing user reports and examining large-scale open-source projects. To speed up this process, the team built an internal system modeled after Crater, the famous Rust project that compiles and runs tests for every single crate published on crates.io. This automated pipeline compares the diagnostics from RustRover's analysis with actual compiler output to catch discrepancies before they reach users, ensuring smoother workflows. RustRover, for those who're unaware, is a dedicated IDE designed specifically for Rust developers. It's been around for a couple of years now, providing features like built-in debugging via LLDB, seamless cargo integration, advanced macro expansion, and HTML support. JetBrains distributes the app under two licensing models: a paid commercial subscription and a free option for non-commercial use.
    • Last year I bought the 2TB variant for $114 on Amazon. That's crazy that the 1TB is now 67% more expensive for half the storage, even with the newer T9 already on the market. And that's considered a good deal.
    • You can disable all non needed features from Brave. There is also Brave Origin which removes them entirely and it is free for Linux.
    • I wish I could use Brave but the tab suspension feature is horrible. It doesn't suspend them like Edge does. Even after 2h open with 70+ tabs (same as Edge), it has 2GB more consumption than Edge for no reason.
    • TeamViewer 15.78.4.0 by Razvan Serea TeamViewer is the fast, simple and friendly solution for remote access over the Internet - all applications in one single, very affordable module. Remote control of computers over the Internet, Instantly take control over a computer anywhere on the Internet, even through firewalls. No installation required, just use it fast and secure. Training, sales and teamwork, TeamViewer can also be used to present your desktop to a partner on the Internet. Show and share your software, PowerPoint presentations etc. File transfer, chat and more, Share your files, chat, switch the direction during a teamwork session, and a lot more is included in TeamViewer. TeamViewer key features: Cross-platform remote access (Windows, macOS, Linux, Android, iOS, IoT) Attended and unattended remote control Secure file transfer between devices Remote printing to local printers Multi-monitor support with easy switching Wake-on-LAN for sleeping devices Session links for quick connections (no password sharing) Web client access (no installation needed) End-to-end encryption (AES-256) Two-factor authentication and access controls AI-powered session insights and reporting Mass deployment and device management tools Customizable allow/block lists for security Command line and script execution remotely Performance monitoring and analytics dashboards TeamViewer 15.78.4.0 changelog: Improvements Permissions inheritance has been improved, increasing reliability when permissions are assigned to user group managers. Bugfixes Fixed a bug where 'Show details' button was not showing up on command bar upon selection of a device group. Fixed a bug which was causing the legacy groups to disappear when applying hide offline filter in basic view. Fixed a bug where devices were loading infinitely after login. Fixed a bug which was causing crash in application. Download: TeamViewer 15.78.4.0 | 32-bit | Portable | Mac | ~70.0 MB (Free for personal use) View: TeamViewer Home Page | Release Notes | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • One Year In
      Primer1st earned a badge
      One Year In
    • Experienced
      JayZJay went up a rank
      Experienced
    • Reacting Well
      Sir_Timbit earned a badge
      Reacting Well
    • Week One Done
      rubentuben8 earned a badge
      Week One Done
    • Week One Done
      ARaclen earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      524
    2. 2
      PsYcHoKiLLa
      231
    3. 3
      Edouard
      135
    4. 4
      ATLien_0
      88
    5. 5
      Steven P.
      82
  • Tell a friend

    Love Neowin? Tell a friend!