Google analytics - track event in firefox plugin - javascript

I develop a firefox extension and I want to track events in the extension using Google analytics.
I tried to add the extension and fire an event but it didn't work:
var _gaq = _gaq || [];
var _AnalyticsCode = 'UA-1234567-9';
_gaq.push(['_setAccount', _AnalyticsCode]);
_gaq.push(['_trackPageview']);
(function () {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = 'https://ssl.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
function googleAnalyticsTrackEvent(category, action) {
_gaq.push(['_trackEvent', category, action]);
}
googleAnalyticsTrackEvent('category','action');
Is there any other way I could track events in my plugin?

You can use this library https://code.google.com/p/google-analytics-js/
Google Analytics to track FireFox extension use

Related

Internet Explorer not allowing me to call .pauseVideo()

I'm hoping someone can help me with why my script isn't working in IE 11. Everything works in both Chrome and Firefox and partly works in IE. The problem is that IE doesn't seem to like when I call the function '.pauseVideo()'
IE Console:
Object doesn't support this property or method 'pauseVideo'
I don't know if this is just some syntax error, or if I need to do something different to support the script in IE 11.
Page displaying error - http://goforward.harpercollege.edu/dev-foundation/index-3.php
<script type="text/javascript" src="https://www.youtube.com/iframe_api"></script>
<script type="text/javascript">
window.onYouTubeIframeAPIReady = function() {
for (item in players_list) {
players[players_list[item]] = new YT.Player(players_list[item], {});
}
}
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var players = new Array();
var players_list = [
"popup-youtube-player-1",
"popup-youtube-player-2",
"popup-youtube-player-3",
"popup-youtube-player-4",
"popup-youtube-player-5",
"popup-youtube-player-6"
];
function pauseVideo() {
for (item in players_list) {
players[players_list[item]].pauseVideo();
}
}
function swapvid(id) {
pauseVideo();
$('.video-slider').css('display', 'none');
$('#' + id).closest('.video-slider').css('display', 'inline');
}
</script>

Jquery script is not loaded, on appendChild

I am loading my script files dynamically using this code,
<script type="text/javascript">
var newscript = document.createElement('script');
newscript.type = 'text/javascript';
newscript.src = '../static/js/jquery.js';
document.getElementsByClassName('container')[0].appendChild(newscript);
var newscript = document.createElement('script');
newscript.type = 'text/javascript';
newscript.src = '../static/js/report_app.js';
document.getElementsByClassName('container')[0].appendChild(newscript);
</script>
It throws error as,
Reference error: $ is not defined
in report_app.js.
I thought, jquery.js script must be loaded into the DOM when the next file, report_app.js is being parsed.
But the error shows, jquery.js is not executed. How to ensure one script file is loaded before running the next one?
It is because the script is added asynchronously, you can use the onload callback
function addScript(path, callback) {
var newscript = document.createElement('script');
newscript.type = 'text/javascript';
newscript.src = path;
newscript.onload = callback;
document.body.appendChild(newscript);
}
addScript('http://code.jquery.com/jquery-1.11.2.js', function () {
addScript('../static/js/report_app.js');
})
Demo: Fiddle

org.mozilla.javascript.EcmaError: ReferenceError: \"document\" is not defined

I am using google maps to get the coordinates of a location. My code is running exactly how I want it to however when I click save on the form I get the error in the above title.
Can anyone tell me where I am going wrong?
function init(){
//Calls the loadScript and initialize function and loads the Google Maps API
loadScript('//maps.googleapis.com/maps/api/js?APIKEY&callback=initialize');
}
function loadScript(src,callback){
//Adds the google maps script to the head of the HTML
alert('loading');
var script = document.createElement("script");
script.type = "text/javascript";
if(callback)script.onload=callback;
document.getElementsByTagName("head")[0].appendChild(script);
script.src = src;
}
Apparently all I needed was a try catch around the code:
function loadScript(src,callback){
//Adds the google maps script to the head of the HTML
try{
var script = document.createElement("script");
script.type = "text/javascript";
if(callback)script.onload=callback;
document.getElementsByTagName("head")[0].appendChild(script);
script.src = src;
}catch(e){
alert(e);
}
}

google analytics api in chrome extension

I am manifest and js file in my chrome extension
code in js file:
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'My account No.']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = 'https://ssl.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
// MAIN CODE:
var main_div = document.getElementsByClassName('someclass');
var download_button = document.createElement('a');
download_button.setAttribute("id", "download");
download_button.style.color = "blue";
download_button.href = "#";
download_button.innerHTML = "Download";
main_div[0].appendChild(download_button);
_gaq.push(['_trackEvent', 'download', 'clicked']);
download_button.addEventListener('click', Download_Answer, false);
I want everytime the user click download button the google analytic should track this event how to do this.I am not getting my results
Here's a look at using Google’s Universal Analytics tracking to a Chrome extension.
Assuming you have given Google Analytics the correct permissions in manifest.json, add the following to setup Google Analytics:
// Standard Google Universal Analytics code
(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','https://www.google-analytics.com/analytics.js','ga'); // Note: https protocol here
ga('create', 'UA-XXXXX-YY', 'auto');
ga('set', 'checkProtocolTask', function(){}); // Removes failing protocol check. #see: http://stackoverflow.com/a/22152353/1958200
ga('send', 'pageview', '/options.html'); // A virtual pageview on your options page
Using jQuery, I'd then assign click events similar to this:
$('#someButton').click(function () {
ga('send', 'event', 'button', 'click', 'someButtonAction');
// remaining logic for button click
});

Embed SWF in JavaScript

i want to embed swf into javascript, i know there is a method to do this, but how do i do it? here is my javascript
<script type="text/javascript">
adroll_adv_id = "JD5ZGBNO4RBYVAMMMPX3J7";
adroll_pix_id = "CEJOJM5N5VAHBKCVMT2DML";
(function () {
var oldonload = window.onload;
window.onload = function(){
__adroll_loaded=true;
var scr = document.createElement("script");
var host = (("https:" == document.location.protocol) ? "https://s.adroll.com" : "http://a.adroll.com");
scr.setAttribute('async', 'true');
scr.type = "text/javascript";
scr.src = host + "/j/roundtrip.js";
((document.getElementsByTagName('head') || [null])[0] ||
document.getElementsByTagName('script')[0].parentNode).appendChild(scr);
if(oldonload){oldonload()}};
}());
</script>
I have a flash banner actually, and i want to call this javascript into that banner
If you want to call javascript function from flash, then you need to call like this from your flash code,
Externalnterface.call("function_name","arg");
You need to import the library flash.external.ExternalInterface;

Categories