综合开发

Redirection Javascript – With Analytics

微信扫一扫,分享到朋友圈

Redirection Javascript – With Analytics

I am trying to track how many users visit my site through one marketing channel. Customers are given a url, specificaly for that advert, that url then sends the user to the actual site. Whilst tracking them in the process. I’m not totally confident this will do it:

//Include jQuery
<script language="text/javascript" src="https://www.tuicool.com/articles/3MzqAbb/jquery.js">
<script language="text/javascript">
//Get IP address to block (ours)
var clientip = "<?php echo $_SERVER['REMOTE_ADDR']; ?>";
$(document).ready(function() {
//If IP is forbidden
if(clientip == "xxx.xxx.xxx.xxx")
{
alert("Redirect has been disabled for you");
} else {
//GOOGLE ANALYTICS CODE
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-1']);
_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);
})();
//Redirect User
window.location = 'http://www.companysite.co.uk';
}
});
</script>

This code is supposed to only run the analytics code if the user’s IP doesn’t match the forbidden one.

Sure, that should work, but if you’re going to mix server-side and client-side code like that, why not just use the server-side code to output one bit of client-side code or the other in the first place. Something like

<!--Include jQuery-->
<script language="text/javascript" src="https://www.tuicool.com/articles/3MzqAbb/jquery.js">
<script language="text/javascript">
$(document).ready(function() {
<?php
if ($_SERVER['REMOTE_ADDR'] == 'xxx.xxx.xxx.xxx') {
?>
alert("Redirect has been disabled for you");
<?php
} else {
?>
//GOOGLE ANALYTICS CODE
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-1']);
_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);
})();
//Redirect User
window.location = 'http://www.companysite.co.uk';
<?php
}
?>
});
</script>

One other thing though..you should probably wrap that window.location in a setTimeout() of 500-1000ms to give the GA code a chance to execute before redirecting.

微信扫一扫,分享到朋友圈

Redirection Javascript – With Analytics

Load data incrementally and optimized Parquet writer with AWS Glue

上一篇

Benchmarking Ozone: Cloudera’s next-generation Storage for CDP

下一篇

你也可能喜欢

评论已经被关闭。

插入图片

热门栏目

Redirection Javascript – With Analytics

长按储存图像,分享给朋友