can someone to some resources for google analytics.i.e,where it can be used what all it can track..Also what does the following code does
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js'"));
<script>
var pageTracker = _gat._getTracker("UA-XXXXXXXXX");
pageTracker._initData();
pageTracker._setDomainName(".mydomain.com");
pageTracker._trackPageview();
</script>
The first script builds a URL where to download the script from (either from an SSL site if your original site uses HTTPS or from a not secure site if your original site does not use HTTPS). Then it includes a link to embed qa.js from Google.
The second script does the actual tracking (i.e. collects some info/parameters and submits this raw data to Google Analytics)
In regards how it works and what it can track - start with these 2 links:
http://www.google.com/support/analytics/bin/answer.py?hl=en&answer=55539
http://analytics.blogspot.com/search/label/Beginner%20Topics
Related
i would to use fastest asynchronous google analytics snippet, but i'm not a good programmer and so i don't know what is the best:
A)
<!-- Google Analytics -->
<script>
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
ga('create', 'UA-XXXXX-Y', 'auto');
ga('send', 'pageview');
</script>
<script async src='//www.google-analytics.com/analytics.js'></script>
<!-- End Google Analytics -->
https://developers.google.com/analytics/devguides/collection/analyticsjs/tracking-snippet-reference#async-snippet-minified
B)
<script>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script');
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
ga.setAttribute('async', 'true');
document.documentElement.firstChild.appendChild(ga);
})();
</script>
About these codes i would like to know if is a problem to put the code inside not <script> but inside <script type="text/javascript"> and if is a problem to put not before </head> (like suggested for "normal" snippet)
I hope you can help me and sorry for my english :) Thanks a lot!
The would guess the first one. It uses async and doesn't depend on DOM modification while loading the page.
For HTML5, it does not matter if you use <script> or <script type="text/javascript">.
As for where to put in your code, normally javascript should be at the bottom of the page after all of your css/html has loaded. Javascript is loaded synchronously, so each file load will stop the rest of the page from loading until that specific file is fully loaded. The async tag is not fully supported by all browsers, so I would not rely on that. Instead, you should use an asynchronous loader (like requirejs or LAB.js).
As for where you should put the google analytics script, in my experience it doesn't matter too much. It sounds like you're prematurely optimizing -- https://stackoverflow.com/a/385529/5780021. Per google's instructions, I believe it's supposed to go in the header in order to improve their statistics around page load speeds.
I have wordpress plugin which outputs Piwik tracking js on client site:
<script type="text/javascript">
var _paq = _paq || [];
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u=(("https:" == document.location.protocol) ? "https" : "http") + "://piwik.com//";
_paq.push(['setTrackerUrl', u+'piwik.php']);
_paq.push(['setSiteId', 1]);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.type='text/javascript';
g.defer=true; g.async=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
})();
</script>
<noscript><p><img src="http://piwik.com/piwik.php?idsite=1" style="border:0" alt="" /></p></noscript>
The issue is that some of clients already use their own piwik tracking js, and it ends up with two piwik tracking JS outputted on their site.
Then there is conflict, because two tracking JS are writing to the same JS object "_paq"
So it would be great if there is some sollution to this like noConflict() for jQuery.
So would need the way to tell to "piwik.js" which I am including to not read from "_paq" and to read from some my custom object. So far, as I can see I would need to modify piwik.js for that.
I saw that piwik has option for multiple tracking : Multiple Piwik trackers
But I don't have control of another tracking code, I have only control of tracking JS outputed from my plugin.
So any, help would be great for this.
The only way I've been able to workaround this problem has been changing the name (choose a weird one) of the _paq variable in piwik.js and loading piwik.js synchronously.
No more collisions after that
I am working with a client that runs a subscription website that plays embedded videos via javascript. Suddenly SOME of the videos stopped loading for the members.
I know, next to nothing about Javascript with CDATA but I noticed that the code on the page with the broken videos is as follows:
<script>// <![CDATA[
var playerhost = (("https:" == document.location.protocol) ? "https://ezs386ed65eac750a03981460786bfd83bd9.s3.amazonaws.com/Screencasts/ezs3js/secure/" : "http://ezs386ed65eac750a03981460786bfd83bd9.s3.amazonaws.com/Screencasts/ezs3js/player/");
document.write(unescape("%3Cscript src='" + playerhost + "flv/7A8982DC-E9D7-D32B-4E1D561336E838BB.js' type='text/javascript'%3E%3C/script%3E"));
// ]]></script>
On a page where the videos are loading correctly there is a different Javascript embed code
<script type="text/javascript">
var playerhost = (("https:" == document.location.protocol) ? "https://ezs386ed65eac750a03981460786bfd83bd9.s3.amazonaws.com/Screencasts/ezs3js/secure/" : "http://ezs386ed65eac750a03981460786bfd83bd9.s3.amazonaws.com/Screencasts/ezs3js/player/");
document.write(unescape("%3Cscript src='" + playerhost + "flv/7A899626-C7B6-2D94-AD06D645F57C90A6.js' type='text/javascript'%3E%3C/script%3E"));
</script>
Both of these were working up until a week ago and can't account for why the CDATA code is no longer working.
Can anyone explain this based on what you see here?
Apparently it was a change by the CDN network that was causing the issue.
I have a bit of embed code that I need to place on a Google Site, this site will be restricted to our Google domain members. I can place this JavaScript in a Google Gadget and it works fine.
<script type="text/javascript" src='https://HOSTNAME/Forms/js/forms-embed.js?v=9.2'>
</script>
<script type="text/javascript">
var lfembed = new LaserficheForms.Form(null /*element to place form in*/,{bpid: '1', host:'https://HOSTNAME/Forms', params:PARAMS, autoheight: false}).getFrm();
lfembed.style.height = "100%";
</script>
The problem is that I want to be able to get the currently logged in user's email address and pass that in the params: section of the code above.
From what I can tell, you can't do this within a Google Gadget. I can retrieve the current email address with the Google Apps Script object below, but I don't know how to make the two work together.
function doGet() {
var gauser = Session.getActiveUser().getEmail();
var output = ContentService.createTextOutput(gauser);
return output;
}
Another problem is that Google sanitizes some of the JavaScript when I try to include all of the code in just the Google Apps Script.
I really don't have much experience in this area, so if anyone could point me in the right direction I'd appreciate it.
I learned about JSONP today.
I ended up getting this to work by publishing the following Google Apps Script web app.
function doGet(request) {
var gauser = Session.getActiveUser().getEmail();
return ContentService.createTextOutput(
request.parameters.prefix + '(' + JSON.stringify(gauser) + ')')
.setMimeType(ContentService.MimeType.JAVASCRIPT);
}
Then I called the web app service via URL in my XML Gadget
<script src="https://WEBAPPURL?prefix=lfdo"></script>
This placed the username value into the lfdo function, which I inserted into the parameter string I needed it in.
The full Content section of the XML gadget:
<![CDATA[
<script type="text/javascript" src='https://HOSTNAME/Forms/js/forms-embed.js?v=9.2'></script>
<script type="text/javascript">
function lfdo(gauser) {
console.log(gauser);
var lfembed = new LaserficheForms.Form(null /*element to place form in*/,{bpid: '1', host:'https://HOSTNAME/Forms', params:'Name='+ gauser, autoheight: false}).getFrm();
lfembed.style.height = "100%";
gadgets.window.adjustHeight();
}
</script>
<script src="https://WEBAPPURL?prefix=lfdo"></script>
]]>
This article on Google Developers set me in the right direction.
I am adding an external CSS file and an external JS file to my headers and footers.
When loading an HTTPS page, some browsers complain that I am loading unsecured content.
Is there an easy way to make the browser load the external content via HTTPS when the page itself is HTTPS?
Use protocol-relative paths.
Thus not
<link rel="stylesheet" href="http://example.com/style.css">
<script src="http://example.com/script.js"></script>
but so
<link rel="stylesheet" href="//example.com/style.css">
<script src="//example.com/script.js"></script>
then it will use the protocol of the parent page.
nute & James Westgate are right when comenting on the later answer.
If we take a look at miscelanous industry-grade external javascript includes, the successfull ones use both document.location.protocol sniffing and script element injection tu use the proper protocol.
So you could use something like :
<script type="text/javascript">
var protocol = (
("https:" == document.location.protocol)
? "https"
: "http"
);
document.write(
unescape(
"%3Cscript"
+ " src='"
+ protocol
+ "://"
+ "your.domain.tld"
+ "/your/script.js"
+ "'"
+ " type='text/javascript'
+ "%3E"
+ "%3C/script%3E"
) // this HAS to be escaped, otherwise it would
// close the actual (not injected) <script> element
);
</script>
The same can be done for external CSS includes.
And by the way: be careful to only use relative url() path in your CSS, if any, otherwise you might still get the "mixed secure and unsecure" warning....
If you use relative paths ( and the content is on the same domain) then the content should use whichever protocol the page was requested in.
However if you are going across domain to a CDN or resource site, or if you need to use absolute paths, then you will need to use server side script to change the links, or always use https://
In contradiction to the escaped response (which will also work) you could skip that part and use the correct way to add nodes to your dom tree:
<script type="text/javascript" language="javascript">
var fileref=document.createElement('script');
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", document.location.protocol + '//www.mydomain.com/script.js');
document.getElementsByTagName("head")[0].appendChild(fileref);
</script>
But the protocol-relative path would be the way to go.