How can I prevent an external JS file from breaking my site? - javascript

I'm currently using Mixpanel on my site. For some reasons they go down from time to time and that disables my site from loading.
Is there a way I can make the load async or some other idea where it doesn't bring down my site when they have downtime?
Here's the code:
<script>
var mp_protocol = (("https:" == document.location.protocol) ? "https://" : "http://");
document.write(unescape("%3Cscript src='" + mp_protocol + "api.mixpanel.com/site_media/js/api/mixpanel.js' type='text/javascript'%3E%3C/script%3E"));
</script>
Thanks

Do not document.write it to the page, append it.
<script>
$( function(){
var mp_protocol = (("https:" == document.location.protocol) ? "https://" : "http://");
jQuery.getScript(unescape(mp_protocol + "api.mixpanel.com/site_media/js/api/mixpanel.js"));
});
</script>

Related

javascript analytics (send variables to php)

what does this code do?
( i'm trying to build a web app analytics system, it must send javascript variables too php and then, php add variables to database ...
its a sample code that do this procces. I would like to "know that this code how sends javascript variables to php?" )
(function() {var no=0;var rand=Math.round(Math.random()*50000);var ref = escape(document.referrer) ; var u=(("https:" == document.location.protocol) ? "https" : "http") + "://analyticstest.com/rx/?xmain2/statsave/9101/50d77b1jkfd24c5db5ef36b22ed8a";
u = u + "&height=" + screen.height + '&width=' + screen.width +'&now=12552222091&phprand=56bf3sdkjnf220912528&rand='+rand+ '&title=1' ;
u = u + "&ref="+ref;
document.write ("<iframe scrolling=no frameborder=0 allowtransparency='true' src='"+u+"' border=0 width='0' height='0' style=' display:none;visibility:hidden'></iframe>");

how to use Postmedia digital cordova plugin (DFP)

I would like to use DFP (double click for Plublisher) for my android app, I already have the generated scripts and markup as below:
<script type='text/javascript'>
(function() {
var useSSL = 'https:' == document.location.protocol;
var src = (useSSL ? 'https:' : 'http:') +
'//www.googletagservices.com/tag/js/gpt.js';
document.write('<scr' + 'ipt src="' + src + '"></scr' + 'ipt>');
})();
</script>
<script type='text/javascript'>
googletag.defineSlot('/*****/****_APP_1024x66', [1024, 66], 'div-gpt-ad- ********-0').addService(googletag.pubads());
googletag.pubads().enableSyncRendering();
googletag.pubads().enableSingleRequest();
googletag.enableServices();
</script>
DOCUMENT BODY
<div id='div-gpt-ad-**********' style='width:1024px; height:66px;'>
<script type='text/javascript'>
googletag.display('div-gpt-ad-*********');
</script>
</div>)
But, it does not display on Mobile (android project). That's why I would like to use the DFP plugin (PostMedia), but I do not know how to use that? as the plugin itself would not be good enough.
for instance, I have already had the code(above) to put in my app, but I wanna know how can i integrate that plugin with my code.
Note: In the plugin we have:
createBannerAd: function (options, successCallback, failureCallback) {
var defaults = {
'adUnitId': 1404187116182-0,
'adSize': (250, 150),
'tags': undefined,
'networkId': 4271715,
'backgroundColor': "#fff"
};
what if we want to have bunch of ads and of course we have bunch of codes generated by Google and FDP? we need to create a service ? or what?
I really appreciate any response a head,
I am using Cordova 2.8.1 / AngularJS
Here is how we use it
var successCreateBannerView = function() {
//console.log("addBanner Success");
DFPPlugin.requestAd({
'isTesting': false
}, success, error);
};
var success = function() {
//console.log("requestAd Success");
};
var error = function(message) {
//console.log("requestAd Failed " + message);
};
var options = {
'adUnitId': '/xxx/xxxx', // Replace the /xxx/xxxx with your adslot
'adSize': 'BANNER',
'tags': {},
'backgroundColor': '#FFFFFF'
};
DFPPlugin.createBannerAd(options, successCreateBannerView, error);

javascript tag in innerhtml

Like the title says I'm trying to add a javascript tag into a innerHTML.
I have a combobox and depending on which value is selected it should load a script into a div
the script looks like this:
<script type="text/javascript">
_cashieProductID=292016;
document.write(unescape("%3Cscript src='" +
('https:' == document.location.protocol ? 'https://' : 'http://') +
"somelink.js'
type='text/javascript'%3E%3C/script%3E"));
</script>
when I put this into my script it shows all kind of text on my page that is part of the combobox script. So I hope someone can help me how to insert this code into my script so it will show the content that gets loaded with the code given above.
HTML
<select id="product" onchange="validate()">
function validate()
{
pName = document.getElementById('product');
var value = pName.options[pName.selectedIndex].value;
if(value == "4OFC")
loadJs(detail,"myjsSrc");
else if(/*some conditon*/)
{
// use this ladder to change the script address based on value of select box
}
else{
}
}
function loadJs(targetDiv,source)
{
var ele = document.getElementById(targetDiv);
if(ele)
{
var scr = document.createElement('script');
scr.type = 'text/javascript';
scr.src = source;
ele.appendChild(scr);
}
}
Just try the actual string in document.write, like this:
document.write("<script src='" + ('https:' == document.location.protocol ? 'https://' : 'http://') + "somelink.js' type='text/javascript'></script>");

simple solution to run third party javascript-tags in paralell? ("asyncronously")?

is there any simple solution to run third party javascript-tags in paralell?
just a few scripts on a html page, which shall not load one after another/sequencial
i know for advertisments it works useing an adserver like doubleclick but that is not necessary here and its not only ads
some need to stay able to analyse the html content the user-agent, ip, referrer or more
examples, that you may very well know :
1.
<script src="http://another-ad-service.com/get-js.aspx?&cwadformat=728*90"</script>
2.
<script language="JavaScript" type="text/javascript">
var 1a_account = '12345';
var 1a_zonesize = '12345-5';
var 1a_adtype = 'js';
</script>
<script type="text/javascript" src="http://agency1a.somewhere/12345.js"></script>
3.
<script type='text/javascript'>
<!--//<![CDATA[
document.MAX_ct0 ='';
var m3_u = (location.protocol=='https:'?'https://an-advertisment-agency.honolulu...'
var m3_r = Math.floor(Math.random()*99999999999);
document.write ("<scr"+"ipt type='text/javascript' src='"+m3_u);
document.write ("zoneid=12345");document.write("&nodis=1");
document.write ('&cb=' + m3_r);
if (document.MAX_used != ',') document.write ("&exclude=" + document.MAX_used);
document.write (document.charset ? '&charset='+document.charset :
(document.characterSet ? '&charset='+document.characterSet : ''));
document.write ("&loc=" + escape(window.location));
if (document.referrer) document.write ("&referer=" + escape(document.referrer));
if (document.context) document.write ("&context=" + escape(document.context));
if ((typeof(document.MAX_ct0) != 'undefined') &&
(document.MAX_ct0.substring(0,4) == 'http')) {
document.write ("&ct0=" + escape(document.MAX_ct0));
}
if (document.mmm_fo) document.write ("&mmm_fo=1");
document.write ("'></scr"+"ipt>");
//]]>--></script>
4.
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-12345-2']);
_gaq.push(['_setDomainName', 'website.edu']);
_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') + '.gooooogle-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<script type="text/javascript"> window.google_analytics_uacct = "UA-12345-2"; </script>
When load external JavaScript files, you can add the async attribute to your script tag.
<script src="http://another-ad-service.com/get-js.aspx?&cwadformat=728*90" async></script>
The script needs to be self-sufficient and not depend on other code you are loading before that script, though.

Google analytics Report manipulation

I need a modified JavaScript which inflated the pageviews, unique visitors and visitor count!
For educational purposes because I want to see if there are certain patterns when artificaly altering the G. Analytics report
I coded this but it does not work:
<script type="text/javascript">
var i=0;
for (i=0;i<=10;i++) {
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-19629541-9']);
_gaq.push(['_trackPageview']);
_gaq.push([_setDomainName, '.domain.com']);
_gaq.push([_setDomainName, 'domain.com']);
(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);
})();
}
</script>​​​​
and this Code only alters the pageviews:
<script>
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script>
// Google Analytics
var pageTracker;
try {
pageTracker = _gat._getTracker("UA-15064357-1"); // pour dystroy.org
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
for (var i=10; i-->0;) pageTracker._trackPageview();
} catch(err) {console.log(err);}
</script>​
I am asking becasue I recenlty viewed an analytics report which did not quite mach up with all the underlaying data
An analytics report can only be trusted as long as you trust the site developers.
A developer can modify it to register the events and page views he wants to have. That's totally normal.
It's a tool for the developers and the site owners, not an independent proof of the popularity of a site.
EDIT : I just tried to add a loop on one of my sites (for which I really don't care about analytics) : http://dystroy.org/re7210/
<script>
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script>
// Google Analytics
var pageTracker;
try {
pageTracker = _gat._getTracker("UA-15064357-1"); // pour dystroy.org
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
for (var i=10; i-->0;) pageTracker._trackPageview();
} catch(err) {console.log(err);}
</script>
And it works :
Only multiples of 10.

Categories