1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to use 'include' correctly?

Discussion in 'PHP' started by Mr.Dog, May 6, 2015.

  1. #1
    Hi,

    I have an external .php file for my Analytics code and I am using it for my Google Analytics code.

    I have the <DOCTYPE>, <HTML> and <BODY> in the file, but I am not sure I need it. However, I lack the <?php and ?> for PHP.

    All you see below is in that .php file for Google Analytics.
    I am worried I am not using it correctly and not tracking all visitors.

    Is it correct?

    <!DOCTYPE html>
    <html lang="en">

    <body>

    <script>

    [my code is here]

    </script>

    </body>

    </html>
     
    Mr.Dog, May 6, 2015 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Okay... you have an include-file for the Google Analytics javascript - there shouldn't be any PHP in there at all? IS there PHP in there? You don't show us how this file is included in other pages - but if this is JUST an include-file, all you need is the javascript - nothing else is needed - you can also just include it in your header / index-file with a regular javascript include, again if the file doesn't contain anything else but the javascript (you would have to rename the file to a .js-extension, of course).
     
    PoPSiCLe, May 6, 2015 IP
  3. Mr.Dog

    Mr.Dog Active Member

    Messages:
    912
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    60
    #3
    The Analytics code is between <script></script> as a Javascript. And that whole thing I mentioned is inside a .php file, which I include onto my hundreds of pages.

    Google says I should put this right in the beginning of <BODY>, but I have it before the end of <HEAD>.

    I have a feeling it's not tracking correctly,... not sure why.
     
    Mr.Dog, May 7, 2015 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    And, I repeat, why in the world is it inside a PHP-file, why do you have other code, and why isn't it just a .js-file with the script in it (without the <script>-tags)?
     
    PoPSiCLe, May 7, 2015 IP
  5. Mr.Dog

    Mr.Dog Active Member

    Messages:
    912
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    60
    #5
    Because, in Google Analytics there is an option for placing it into .PHP pages.

    Go to the Google Analytics account and it says exactly this:

    PHP ImplementationOPTIONAL
    Create a PHP file named "analyticstracking.php" with the code above and include it on each PHP template page. Then, add the following line to each template page immediately after the opening <body> tag:
    <?php include_once("analyticstracking.php") ?>
     
    Mr.Dog, May 7, 2015 IP
  6. Mr.Dog

    Mr.Dog Active Member

    Messages:
    912
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    60
    #6
    ...so, I did as required, just that mine is before </HEAD>
     
    Mr.Dog, May 7, 2015 IP
  7. Spiner

    Spiner Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #7
    Use this expretion

    <? include("File.php") ?> this can be included inside html tags
     
    Spiner, May 7, 2015 IP
  8. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #8
    I'm just saying that you don't NEED to do it that way, and if Google recommends it, they're retarded. That being said - previously, having the Analytics code present at the beginning of the page was a no-no - it should be posted at the END of the page (before the </body>-tag), so that it doesn't affect loading of the page itself, and can monitor the complete, rendered page. Why don't you just test - have it in <head>, have it after <body> and have it before </body> (one at the time, of course), and do some testing, having say 5-10 friends visit, do some stuff, and see if everything is being tracked, and if the results differ between the different placements. I doubt there is any real difference.
     
    PoPSiCLe, May 7, 2015 IP
  9. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #9
    An opinion I am increasingly becoming convinced of. It was bad enough the analytics SCAM that makes you waste code on your site and slow your site down for a service that replicates stuff you should be pulling from your own server logs with tools like analog or webalizer, but since "page speed insights" is now ranking 1 minute pageload sites better than sites that load in 1 second just because the faster site isn't using a CDN or ineptly written... Google's webmaster stuff is very quickly becoming VERY SPECIAL.

    In the same way some Olympics are "special".

    You'd almost think that CDN's were high paying advertisers or that they now are trying to dupe people into using their bloated scripttard "service" that actually makes fast properly written sites slower.

    Hell just look at the mouth-breathing stupidity of their constantly telling you to dick with cache-control headers; given how browser makers brag about speed, if there was something wrong with the browser defaults don't you think they'd fix that on their end? Much less if you visit more than three websites the cache should be flushed on space concerns ALONE -- especially with the multi-megabyte monstrosities people are vomiting up and have the giant set of donkey brass to call a website now!

    My advice? **** Google Analytics for anything not provided by your server logs is just time wasted dicking around with unimportant crap; time likely better spent focusing on important things like content generation and active promotion of that content! It's pointless bloated bullshit and just more bullshit "statistics" that can be used to come up with lame excuses for not doing things properly or to let the various jackasses who call themselves SEO "experts" to bold faced lie to you!

    See the "I don't get major traffic from mobile" lie; are you not getting major traffic because people on mobile don't want your content, or are you not getting traffic because your site doesn't WORK on mobile? It's VERY easy to let your statistics convince you not to bother fixing things.

    Statistical lies and the ways people lie to themselves with it is just mind-blowing since it's the old propaganda standby of "card stacking". Hell, just look at the market share "lie" - one of the CLASSIC bits of propaganda across every industry. Let's use IE as an example:

    In 2004 IE reached a whopping 92% of the market (depending on who's numbers you use)... today they have anywhere from 24.6% (statcounter) to 59% (netApplications) -- automatically presented with JUST percentage, you'll constantly hear people deluding themselves into thinking that means there are less people using IE than were in 2003.

    ... and that is a delusion and complete 100% work of fiction. WHY? How can I possibly say that?

    In 2003 there were 750 million Internet users. The most recent count of Internet users is almost 3.2 BILLION. Now, I'm no mathematical genius, but...

    92% of 750 million is 690 million.
    24.6% of 3.2 billion is 787 million.

    So while IE "lost" close to 67% market share, they GAINED almost 100 million users; meaning they haven't lost a blasted thing!.

    Then you have places like Wikimedia which game their numbers by listing requests and not unique visits, which gives an unfair advantage to FF and Chrome... what the sudden drop in IE as a percentage of requests corresponds to FF adding it's little "prefetch" to the browser? No, really? :/

    You'll see the same thing from the anti-vaxxer nutjobs where they'll talk number of infected without saying out of how many people -- it's the same lie in REVERSE. Omitting "out of how many" is the biggest lie be it in percentage or actual number impacted. There's a reason "card stacking" is one of the 7 core propaganda techniques.

    Having statistics is one thing, knowing what they mean and what they don't is something else -- so my advice, use the numbers off your server and stop letting Google lie to you!

    ... and they are outright lying to people now; either that or as PoPSiCLe suggests, not all their dogs are barking. One brick short of having any bricks. The old antenna doesn't get every station. One midget short of a wrestling match. Mad as a monkey on a trike.

    Makes me think that burst I've been predicting for six years that Mark Cuban and Elon Musk now agree with me on is rushing towards us even faster now; as when Google is pulling desparate scam artist BS trickery and bold-faced lying to the public... well, anybody else remember the bullshit leading up to March 2000? We just passed the value of the previous bubble, and with a total lack of liquidity and 99% of this bubble funded by credit, this burst is gonna be far, far worse. Though honestly a few of the so called SEO "experts" and artsy types with the cojones to call themselves "designers" throwing themselves out of windows might actually be an improvement.

    That they can't even bother to tell you to use valid code or think ANYONE who has ANY business adding something like Analytics to a site needs a PHP include to do it... Wow, that's stupid! They are now handing out advice WORSE than the crap at W3Schools -- and to me that takes actual effort and intent.

    ... and being that Google's cha-ching bling-bling comes almost exclusively from advertising, them acting like the sleazeball scam artist shits that make up the entire advertising industry shouldn't really be a shock.
     
    deathshadow, May 8, 2015 IP
  10. digi_media

    digi_media Active Member

    Messages:
    99
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    68
    #10
    <?php include("filepathwithname.php"); ?>

    Although You know use include_once

    <?php include_once("filepathwithname.php"); ?>

    And another option is require and require_once, at the place of include.
     
    digi_media, May 27, 2015 IP
  11. CoastWeb

    CoastWeb Greenhorn

    Messages:
    6
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    23
    #11
    Getting back to the actual question asked, no, that is not correct. You need to take into account where the file is being included. If the page including it already has a HTML and body tag, you will end up with an html page embedded inside another html page, which is invalid HTML. If you view the source of the page you will see what I mean. You should remove everything except the script tags and whats between them.
     
    CoastWeb, Aug 18, 2015 IP
  12. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #12
    I think the google code should go just before </body> tag, and not just after <body> tag. Also remove the <html> <body> etc tags from the google code because you already have those.

    Also I'd suggest that you use require_once('file.php') instead of a "include_once".
    Take care
     
    JEET, Aug 19, 2015 IP
  13. pmf123

    pmf123 Notable Member

    Messages:
    1,447
    Likes Received:
    75
    Best Answers:
    0
    Trophy Points:
    215
    #13
    remember the original page must be a .php file for the include to work...
     
    pmf123, Aug 19, 2015 IP
  14. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #14
    Oh. My. God! Or flying spaghetti monster. Whatever takes your fancy. The god-damn analytics code is just plain javascript. THERE'S NO NEED, WHATSOEVER, TO INCLUDE IT AS A PHP FILE. Just save the analytics code in a file named analytics.js and make sure you load that javascript on all pages. Or, if you already have a site where files are included, for instance header.php or something similar, just paste the javascript code there.
     
    PoPSiCLe, Aug 19, 2015 IP