Ad

How To Make Onclick Event Works Via Jquery

- 1 answer

we have some trouble with our shopify store: and we want to try manually send _ga paramteter to checkout.shopify.com. I have experience where i fix this on other store by adding to onclick this line of code:

$html += "<a href=\"/checkout\" onclick=\"ga(function (tracker) {var linkerParam = tracker.get('linkerParam'); document.cookie = '_shopify_ga=' + linkerParam + '; ' + 'path=/';}); document.getElementById('cartform').submit();\" class=\"btn btn-1 btn-block big\">{{ 'general.cart_info.checkout' | t }}</a>"; 

But with this website it doesn't work. This is how it looks like now:

 jQuery('<div class="action"><button class="btn btn-default" onclick="window.location=\'/checkout\'">{{ 'general.header.cart_info.checkout' | t }}</button></div>').appendTo(cart_summary);

This is what i was trying to make:

 jQuery('<div class="action"><a target="_blank" rel="nofollow noreferrer" href="/checkout\" id="checkout-link" class="btn btn-default" onclick=\"ga(function (tracker) {var linkerParam = tracker.get('linkerParam'); document.cookie = '_shopify_ga=' + linkerParam + '; ' + 'path=/';}); document.getElementById('cartform').submit();\">{{ 'general.header.cart_info.checkout' | t }}</button></div>').appendTo(cart_summary);

Also i was trying to make this:

$(document).ready(function() {

    $("a#checkout-link").each(function() {
        var text = $(this).text();
        var thisEvent = $(this).attr("onclick"); // grab the original onclick event
        $(this).click(function(event) { //  when someone clicks these links
            event.preventDefault(); //  don't open the link yet
            ga(function (tracker) {var linkerParam = tracker.get('linkerParam'); 
            document.cookie = '_shopify_ga=' + linkerParam + '; ' + 'path=/';}); 
            document.getElementById('cartform').submit();
            setTimeout(function() { // now wait 300 milliseconds...
                eval(thisEvent); // ... and continue with the onclick event
            },300);
        });
    });

});

With all this ways this doesn't work. I know i'm noob in code, but maybe someone can help me?

Ad

Answer

So do you want to execute that ga function before page gets redirected? If so try creating a simple function that does what you want to do:

function saveLinkerParam(){
 ga(function (tracker) {var linkerParam = tracker.get('linkerParam'); document.cookie = '_shopify_ga=' + linkerParam + '; ' + 'path=/';}); 
}

Then execute it in onclick handler that already exists(just bofore window.location is changed:

jQuery('<div class="action"><button class="btn btn-default" onclick="saveLinkerParam();window.location=\'/checkout\' + ">{{ 'general.header.cart_info.checkout' | t }}</button></div>').appendTo(cart_summary);

(you don't have to create a new function saveLinkerParam but I think it makes code a bit more readable)

I haven't tried tosaveLinkerParam but I think it makes code a bit more readable)

I haven't tried to actually execute this code so let me know if you get any syntax error. Btw I think the problem with code you tried might be syntax, more precisely when you are concatenating the text and variables in jQuery function.

Ad
source: stackoverflow.com
Ad