Insert Google Analytics in php (Am I doing it the

2019-09-18 16:39发布

I'm trying to load Google Analytics on every file using php. This is not a site, these are some affiliate links structure with javascript redirectiib, so I decided to implement GA tracking code on them. They basically work like this:

The root folder is affiliatestuff, and it has:
googleanalytics123.php (File which contains the GA javascript), it doesn't have any php inside, just the GA code.
index.thml: Blank HTML file.

Inside the root folder there is a subfolder named affiliate01: Inside this folder, there is a index.php with the following code (Please not that in the following code I've changed a few stuff for security reasons):

Begin index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<?php
include_once ('http:// my site/go/googleanalytics123.php');
?>

<script type="text/javascript"> function recordOutboundLink(link, category, action) { try { var myTracker=_gat._getTrackerByName(); _gaq.push(['myTracker._trackEvent', category , action ]); setTimeout('document.location = "' + link.href + '"', 100) }catch(err){} } </script>

<script type="text/javascript"><a href="../trafficapp">trafficapp</a>

recordOutboundLink(this, 'Go to Australia', 'http:// my affiliate link /191719/18040');
setTimeout(function(){window.location='http:// my affiliate link/191719/18040';}, 500); // working with a 500ms timeout to make sure the tracking is done correctly

</script></head>
<title>Live and work in Australia</title>
<body>
</body>

</html>

That last file is supposed to redirect the user to the affiliate product's page, it is working, what I'd like to know is if I'm getting the googleanalytics123.php the right way.

There is the code of the googleanalytics123.php

<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-xxxxxxxx-x', 'auto');
  ga('send', 'pageview');

</script>

It's just that inside the script, no php at all. Please not that I've changed the UA on purpose.

So am I doing it the right way? I managed to track the google analytics file, but not the affiliates folder.

1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-09-18 17:22

create a file called analytics.php that contains the google analytics code in it then include that file

analytics.php

<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),include "analytics.php";
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-xxxxxxxx-x', 'auto');
  ga('send', 'pageview');

</script>

Then in your script:

  <?php include "analytics.php"; ?>
  </body>
</html>
查看更多
登录 后发表回答