How to make goal completion work on hashchange? - javascript

I have an AngularJS webapp where there are no separate pages.
I have set up a goal completion so that there's a code that is triggered by a javascript event,
and it has the following structure:
function call_completion(){
window.google_conversion_id = 973404965;
window.google_conversion_language = "en";
window.google_conversion_format = "1";
window.google_conversion_color = "ffffff";
window.google_conversion_label = "doGHCLPK2wkQpfaT0AM";
window.google_conversion_value = 1.000000;
window.google_remarketing_only = false;
var s = document.createElement('script');
s.type = "text/javascript";
s.src = '//www.googleadservices.com/pagead/conversion.js';
document.body.appendChild(s);
}
Yet, I can't see any completions registered in Google Analytics. What could be the problem?
Is this the correct way to do it? Is there perhaps some nifty plugin that gets this problem right?
UPDATE:
For the new Universal Analytics, there is a plugin called angular-ga.
It eliminates the whole question.

You shouldn't append the script to your view, since it won't be executed.
You should use this:
$.getScript( "http://www.googleadservices.com/pagead/conversion.js" );
To get the script working on that page and execute it.
Hope this helps :)

The <noscript> part also has to be included.
Something like this:
$('body')
.append('\
<!-- Google Code for Subscriptions Conversion Page -->\n\
<script type="text/javascript">\n\
/* <![CDATA[ */\n\
var google_conversion_id = 973404965;\n\
var google_conversion_language = "en";\n\
var google_conversion_format = "1";\n\
var google_conversion_color = "ffffff";\n\
var google_conversion_label = "doGHCLPK2wkQpfaT0AM";\n\
var google_conversion_value = 1.000000;\n\
var google_remarketing_only = false;\n\
/* ]]> */\n\
</script>\
');
$('body')
.append('<script type="text/javascript" src="//www.googleadservices.com/pagead/conversion.js">')
.append($('<noscript>')
.append($('<div style="display:inline;">')
.append('<img height="1" width="1" style="border-style:none;" alt="" src="//www.googleadservices.com/pagead/conversion/973404965/?value=1.000000&label=doGHCLPK2wkQpfaT0AM&guid=ON&script=0"/>')
)
);

Related

GTM - JS - parse error primary expression expected

I have an error each time on GTM : Error at line 10, character 32: Parse error primary expression expected, on this code :
<img src="https://secure.adnxs.com/px?id=829787&seg=8170611&order_id='transactionId'_'quantity'&value='transactionTotal'&t=2&other='name'" width="1" height="1" />
<!-- Google Code for CPA Achat TFC Conversion Page -->
<script type="text/javascript">
/* <![CDATA[ */
var google_conversion_id = 850998157;
var google_conversion_language = "en";
var google_conversion_format = "3";
var google_conversion_color = "ffffff";
var google_conversion_label = "_JJWCL6c2HEQjefklQM";
var google_conversion_value = <myvalue>;
var google_conversion_currency = "EUR";
var google_remarketing_only = false;
/* ]]> */
</script>
Thank you for your help !
You need to replace <myvalue> on the following line with the relevant value:
var google_conversion_value = <myvalue>;
More detail can be found here.
You need to remove the "type" attribute from your script blocks (type="text/javascript"), or GTM will assume the target document type is XHTML, and will encode the tag content.

Insert a script tag before another specific script tag

I have these lines to call my JS files and libraries:
<script src="assets/libs/perfect-scrollbar/perfect-scrollbar.jquery.js"></script>
<script src="assets/libs/PACE/pace.min.js"></script>
<script src="assets/js/library.js"></script>
<script src="assets/js/plugins.js"></script>
<script src="assets/js/app.js"></script>
Dynamically, I need to insert a new library. So I would make:
document.body.appendChild(script);
Where script is my new library.
But how can I insert it just before library.js ?
Thanks for your help.
It is working , i did experiment on this and result is Positive. Try This code
var mynewScript = document.createElement("script");
mynewScript.type = "text/javascript";
mynewScript.src = "http://mywebsite.com";
var a = document.getElementsByTagName('script')[2]; // [2] is the index of library.js
var parentT = a.parentNode;
parentT.insertBefore(mynewScript , a);
<script src="assets/libs/perfect-scrollbar/perfect-scrollbar.jquery.js"></script>
<script src="assets/libs/PACE/pace.min.js"></script>
<script>
document.body.appendChild(script);
document.body.appendChild(<library.js file>);
document.body.appendChild(<plugins.js file>);
document.body.appendChild(<app.js file>);
</script>
Add the following scripts dynamically as well!
If you do not want to do this, then you'll have to fetch the DOM elements using $('html').html() in your script and use the technique mentioned here as suggested by #Liam.
BUT! If you want to load the above mentioned scripts after loading your new library, there has to be a reason for that (those scripts depend on that library), in which case you should add those scripts dynamically as well.
I did like this and its work correctly
const ID = 'test-snippet';
// abort if it already exists
if ( document.getElementById( ID ) ) {
return;
}
const scriptTags = document.getElementsByTagName( 'script' );
const lastScriptTag = scriptTags[scriptTags.length - 1];
// create new script element
const js = document.createElement( 'script' );
js.id = ID;
js.src = 'https://some.js/';;
js.type = 'text/javascript';
js.async = true;
// append it after the last script tag
lastScriptTag.parentNode.insertBefore( js, lastScriptTag );

How to pass back a unique ID with JavaScript or PHP?

I need to pass back a unique ID in place of "INSERT+ORDER+ID" for when a user clicks on our "Request Information" button to better track our visitors. Does anyone know how I can accomplish this? Any help would be much appreciated, thanks!
<script type="text/javascript">
var _qevents = _qevents || [];
(function() {
var elem = document.createElement('script');
elem.src = (document.location.protocol == "https:" ? "https://secure" : "http://edge") +
".quantserve.com/quant.js";
elem.async = true;
elem.type = "text/javascript";
var scpt = document.getElementsByTagName('script')[0];
scpt.parentNode.insertBefore(elem, scpt);
})();
_qevents.push(
{qacct:"p-yeADJca0S9FXE",labels:"_fp.event.Request Information Confirmation
Page",orderid:"INSERT+ORDER+ID"}
);
</script>
<noscript>
<img src="//pixel.quantserve.com/pixel
/p-yeADJca0S9FXE.gif?labels=_fp.event.Request+Information+Confirmation+Page&
orderid=INSERT+ORDER+ID" style="display: none;" border="0" height="1" width="1" alt="Quantcast"/>
</noscript>
assuming you mean a random unique ID,
for the javascript event tracking this should work:
// function to generate random id in js
function getRandomId() {
var id = +new Date() + Math.random();
return id.toString().replace('.','');
}
var btnReqInfo = document.getElementById('request_information_btn');
// bind the click on button
btnReqInfo.addEventListener('click', function() {
// track the event
_qevents.push({ qacct:"p-yeADJca0S9FXE", labels: "_fp.event.Request Information ConfirmationPage",orderid: getRandomId() });
, false);
About the content in the noscript tag, you can't do it with static html of course so you have to put in the context of your template (in the php file), something like this that generates a unique ID and then echo it in place of your placeholder.
Since I'm in the mood for structuring and cleaning, I took the liberty of refactoring a bit the code, in case, you can replace all your script with this (assuming you're using plain js (vanilla) and html 5):
<script>
var _qevents = _qevents || [];
(function() {
var
init = function() {
loadScript();
bindUi();
},
loadScript = function() {
var elem = document.createElement('script');
elem.src = (document.location.protocol == "https:" ? "https://secure" : "http://edge") + ".quantserve.com/quant.js";
elem.async = true;
elem.type = "text/javascript";
var scrpt = document.getElementsByTagName('script')[0];
scrpt.parentNode.insertBefore(elem, scrpt);
},
bindUi = function() {
var btnReqInfo = document.getElementById('request_information_btn');
btnReqInfo.addEventListener('click', track.order, false);
},
track = {
order: function() {
_qevents.push({ qacct:"p-yeADJca0S9FXE", labels: "_fp.event.Request Information ConfirmationPage", orderid: utils.getRandomId() });
}
},
utils = {
getRandomId : function() {
var id = +new Date() + Math.random();
return id.toString().replace('.','');
}
};
init();
})();
</script>
I would suggest using an AJAX call that is binded to the onclick event or .click if you're using JQuery
The AJAX call would hit a PHP script or call whatever you're using for analytics.

Convert html code into actionscript code

How can i convert this HTML code into Action script code, i am an android and actionscript developer, i dont know much about HTML and java script. i have tried different forums, i know it can be converted, can someone help me
<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>
Well the code you listed is actually javascript encased in a html tag. Javascript and actionscript are pretty similar, you should be able to use the same code with minimal changes. Just use the javascript as a reference to code it with actionscript

Multiple Javascript Asynchronously Loading in a Page

<script type="text/javascript">
function loadScript(scriptpath) {
var s = document.getElementsByTagName('script')[0];
var ss = document.createElement('script');
ss.type = 'text/javascript';
ss.async = true;
ss.src = scriptpath;
s.parentNode.insertBefore(ss, s);
}
loadScript('compressed1.js');
loadScript('compressed2.js');
</script>
but i am getting error that SS is not defined can somebody help me with above coding?
var ssa
Keep variable names consistent. It's usually a good idea to give meaningful names like newScript

Categories