Google Analytics - Can I put script in footer? - php

I want to add google analytics to our website and have read some conflicting info about where to put the script tag.
Google says to put it before the closing </ head> tag: http://code.google.com/apis/analytics/docs/tracking/asyncTracking.html
The way our site is designed, this would mean making an edit to every page. It's not that big of a deal if I need to do this, however, our site also uses header and footer include files.
These header & footer files have html code in them and fall just inside the <body> and </body> tags once the page is loaded.
It would be so much easier to just add the script to the header or the footer file. I'd just paste it in there once and it would be serving up the code on every page.
My question is: Can I do this? Can I move the script snippet inside footer include file even though it's not before the </head> tag as google suggests?
Am I asking for problems if i do it this way?
Thanks!!

You can.
The only difference is that if you put in the "footer" (i.e. just before the </body> tag), the browser will first load the DOM (i.e. everything up to the script tag) and then start to load the script. If you put it in the header, it will try to load the script either before it loads the rest of the DOM or while it is loading it (depending on the browser).
You might feel a difference only if you have a huge page with a lot of elements, but the only difference really would be that the script would be loaded later, so if someone aborts a load of the page, the script might not get executed.
Thus, Google recommends to place it in the <head> and if you can, you should, however you also can put it somewhere else if that makes life easier.

I would recommend saving the analytics code in a file and including it in your footer or header - Example: . I've recently run into some issues with Firefox and IE regarding the page load and analytics. It loads the page to the point where the analytics code is present and then the page hangs indefinitely. I can refresh the page and it loads fine, but for some reason it's happening and there's very little info out there regarding solutions. So, in my case I've included the code in the Footer or of the page at the bottom. This way, in regards to your site visitors, they'll likely not even notice because the rest of your page will load and once they click on a link it shouldn't hang again.

I'd say you can - but easier than that. Just place it in the footer and check if the Live-tracking finds visitors if it does you have the answer.

It's possible and is in fact the way many plugins do it (especially most WordPress plugins I've seen). In fact, putting all your JS at the end of the html (just before < /body>) is recommended if those are not required during the load of the page.

I have seen people put it pretty much anywhere on the page. You can put script tags anywhere on the page; many prefer to keep them in the <head> tag, while others put them at the bottom to avoid using document.ready. The point is that putting scripts there still works. Therefore, I see no problem in putting it at the bottom of the page.

Yes, I believe that it would work - it's not recommended though.
f you have a header and footer file, why not just append your Analytics code to it? Unless every page has unique footer/header page, of course. Then I'd recommend you to take a look at MVC.

Observations have already been made here regarding the scripts placement which can be taken on a site by site basis. We run Google Analytics using Google Tag Manager and use the header code placement in the footer and have no problems with that. Google is currently rolling out Core Web Vitals which places website performance as a must have, and so optimising code delivery is really important. Just wanted to add this here as it was pertinant.

Related

Using PHP or JavaScript (client side) - How to inject a web element into a live webpage (for a demo..)?

Let's say, that I want to demonstrate a widget (or some HTML in a frame) that would be "injected" into another page.
For example: I want to show the people in Amazon.com that I can put let's say a ball image underneath every price tag they put on their web page. That is - I want to build a web server (or indeed a server less html web page) that would show their page and put some stuff of mine inside theirs. So it looks as if the client (Amazon.com here) has my software already installed on their server.
I am a web-dev total newbie, so if this is the simplest thing in the world please, ..
Thanks
There's TONS of special cases that can cause this to fail, but I'll present a simple way that will work for you on a decent amount of webpages(but not all).
save the webpages html source into a local html file.
edit the html source, adding a <base href="http://www.amazon.com/"> tag into the <head> element.
make any other modifcations to the page you want, such as adding new <script> tags to support your new functionality. Make sure your modifications use absolute urls.
If they navigate away from the page, your enhancements will obviously not carry onto the next page. ALso, you will have more success if you upload the file onto a web server. While a user can view the page by double clicking on the html file if they were to save it locally, differences in javascript security permissions will likely make some webpages not function correctly.
The reason you need to add the <base> tag is because the browser resolves relative urls by looking at the url in its address bar. So, if the amazon page had an image like this
<img src="logo.png">
and you saved the html and put it on you webserver at www.example.com, the browser would look for the image at www.example.com/logo.png, which clearly doesn't exist. The base tag tells it what base url to use.
If you need more automation, having them install a browser addon would be a good way to do this if your users are somewhat technical. Greasemonkey is a popular addon, and you can tell it to inject stuff into certain webpages. The benefit of an addon is that it can inject the new functionality into any page on the web, without you having to individually save and modify them. Also, it has the potential to work on all web pages, leaving their functionality perfectly in tact, opposed to the other suggestion. This is far more complicated though.

How do I preload .css and .js files, before the whole page (dynamic by PHP) is generated

My homepage (index.php) is generated dynamicly by PHP, so it takes about 1s till the the page is delivered to the user.
Because my css and js script is in the header of (index.php), after this 1s the css and js will start to download.
Is there some general way of preloading this js and css files?
My aproach would be to delete all content of index.php, include just <'head> with my css and js file, and then do some js to load the whole indexOriginal.php (with the dynamic content generated on my server by PHP), so this should enable the user to download the css, js in parallel of indexOriginal.php.
Is this the right aproach?
The approach you suggested could work. You also might consider altering your PHP script to hurriedly deliver the first part of the page (<html> down through <body>) before it starts getting the rest of the page together (see http://php.net/manual/en/function.flush.php).
Your page full generated by backend and sended to user only after php script end its work.
You could, for example, create page like.
<html><head><script></script></head><body></body></html>
In <script></script> load, for example, JQuery and do something like
$(document).ready($('body').load('/path/to/your/script.php'))
But I really thing this wouldn't be a good practice. Also I don't think that Google would parse your site content with this solution.
I think you should look better about your JS (compress, gzip, split to many files and upload them from different CDNs, move everything to $().ready, etc.)
My recommendation would be to have your initial PHP script JUST deliver the CSS and JS tags. Once those are loaded, use the JavaScript to make an AJAX call that gets the rest of the page then displays it. Or you can build the page in the JavaScript (which would allow you to use loading icons and the like to give a better indicator to the user that "the page is coming")

Make a URL Navigation load instantly

What has been known for a while, is that a "fast navigation" works easily for http://example.com/#1 --> http://example.com/#2.
However, there is a new technique out there. It enables fast navigation between http://example.com/1 --> http://example.com/2.
EXAMPLE: http://rageslide.com/
As you can see in the example, the navigation between http://rageslide.com/1 and http://rageslide.com/2 etc. via swiping apparantly DOES NOT FORCE THE ENTIRE SITE TO RELOAD.
I'd like to do the same for my site, but I have no idea how to do this. All pages served by my site are dynamic (via PHP and MYSQL).
I have this idea:
Cache the generated output of a page (http://example.com/2) for 60 seconds.
When the user is on http://example.com/1 preload (http://example.com/2) via Javascript.
The user navigates from http://example.com/1 to http://example.com/2. Since the content is preloaded and cached, the content will be served to the user instantly.
Different idea:
Somehow, http://example.com/1 is being interpreted as http://example.com/content.php#1 through a .htaccess. But I have no idea if this is possible or not.
Will this work? Or what would be the best way to solve this problem?
No, the url you see there is not used to load another page. There are AJAX requests in the javascript code contained in the website, that load the new content to display and update the URL bar.
You can read more about it in this article and in the following questions asked in the past:
Modify the URL without reloading the page
Updating address bar with new URL without hash or reloading the page
i can think of two possible thing you can try out.
first is simply use iframes to load the next and previous page of each page, and when someone swipes to the next page load the next page to a new iframe or a div with ajax or any other html element for that matter.
the other is to use the rel attribute, here is an explanation about it.
hope this helps you out
you can get pretty close without scripting anything or degrading the site by letting the browser cache the expected navigation point resources
for caching images, put dummies at the end of the body
<img .... height="0" width="0">
and for pages
<link rel=”prefetch” href=”url” /> there is also a rel attribute for next and previous for slide viewer type pages
Note: the url can be a javascript resource
Note2: the transition may be slightly less clean than dynamically populating from javascript especially on larger complex pages, but will still work with noscript or javascript turned off, so maybe a good fallback

Including a file based on clicked link

I am beginning to develop a website as a personal project, to discover a little bit web technologies. My question is more about "how to make the things clean" than a technical question.
Here is the thing:
Let's say that an index.php page will include an other menu.php, which will permit to navigate the website.
My index.php look like this, basically:
include("header");
include("menu");
include("DEPENDING ON WHICH LINK AS BEEN CLICKED FROM THE MENU");
include("bottom");
To avoid the POST method, and have a lot of stuff on my URL, I would like to do it an other way, but I do not know what is the best one.
I thought about declaring a variable like $page, when a link is clicked, I can do something like "if $page == home", then I include the home.php page... etc...
I do not know if my question is clear... I know that it will appear as a very easy and beginner question but I don't even know where to look...
Do you know if I can find any "open source website" so I can study the code and see the best practices about it?
P.S.: Sorry for my english which is probably not perfect at all, I am working on it.
You can have a menu like
Home
About
Then on your PHP code
include $_GET["view"] . ".php";
Note that I am not validating, so any parameter passed on the url would be able to include any file.
The $_GET returns the values passed to the page through the URL.
The $_POST returns values posted.
The $_REQUEST returns both $_GET and $_POST values.
A good place to study many languages is W3Schools, you could check there sometime.
Make a page which will be common redirect page.
Every post will come to that page and based on the page parameter it will redirect.
So action of every page is same, but based on page paramter redirect to which ever page you want
You can switch case and
use header to redirect
i think you want to avoid GET method and avoid lot stuff in url
For learning
I thinks this is the simple website for learner.
http://www.w3schools.com/php/default.asp
http://www.plus2net.com/php_tutorial/site_map.php
http://www.tizag.com/phpT/
actually most of all these websites are same.
i hope you know the basic website PHP.net
Then,,.. no one is low level ..every low level will be in a top level one day.. just like you am also trying :)
Don't do what you are trying to do. The whole point of having pages is to handle things with different files. That is, you will have some commonality between files (handled by auto prepend and include path, potentially) such as your header and footer. Each file should include this on its own and print it out directly.
That is, you should not handle everything on one page and then conditionally include a file. Just send users to a different page.
Finally, I recommend not splitting up the header/footer files at all. Instead create a decorator that wraps the main content and displays it all at once. Something like:
$page = <<<HTML
<html><head><title></title></head>
<body>
<div id="top nav"></div>
{CONTENT}
</body>
</html>
HTML;
Then you go through and build your page content. Then you add it to CONTENT in the decorator and print it. PHPTAL is a great way to have this handled externally.
Hi you should please ask one question at a time:
I think this basic tutorial will give you a good idea on how and what to use the include(); func.
http://www.w3schools.com/php/php_includes.asp
I started with:
http://www.solitude.dk/filethingie/
Very simple .php file administrator.
You should definetly check out sourceforge, giant colletion of open source projects just filter by php (search for literally anything).
Just wanted to mention that you can download the full code of more complex pages (that are based on php) like
wordpress (blogging platform) - very easy to install and configure
identi.ca (twitter open source alternave)
You can now download reddit´s source code - quite easy to.
Maybe you wont be able to modify them immeditelly but theyll help you to get the picture

Opening Javascript based popup ads on the same page

I own an image hosting site and would like to generate one popup per visitor per day. The easiest way for me to do this was to write a php script that called subdomains, like ads1.sitename.com
ads2.sitename.com
unfortunatly most of my advertisers want to give me a block of javascript code to use rather than a direct link, so I can't just make the individual subdomains header redirects.I'd rather use the subdomains that way I can manage multiple advertisers without changing any code on page, just code in my php admin page. Any ideas on how I can stick this jscript into the page so I don't need to worry about a blank ads1.sitename.com as well as the popup coming up?
I doubt you'll find much sympathy for help with pop-up ads.
How about appending a simple window.close() after the advertising code? That way their popup is displayed and your window closes neatly.
I'm not sure that I've ever had a browser complain that the window is being closed. This method has always worked for me. (IE, Firefox, etc.)
At the risk of helping someone who wants to deploy popup ads (which is bound to fail due to most popup blockers anyway), why can't you just have the subdomains load pages that load the block of Javascript the advertisers give you?
Hey, cut the guy some slack. Popups might not be very nice, but at least he's trying to reduce the amount of them. And popup blockers are going to fix most of it anyway. In any case, someone else might find this question with more altruistic goals (not sure how they'd fit that with popups, but hey-ho).
I don't quite follow your question, but here's some ideas:
Look into Server Side Includes (SSI) to easily add a block of javascript to each page (though you could also do it with a PHP include instead)
Do your advertiser choosing in your PHP script rather than calling the subdomains
Decipher the javascript to work out what it's doing and put a modified version in the subdomain page so it doesn't need an additional popup. Shouldn't be too hard.

Categories