The developer who made a site we're working on has buttons linking to PDF downloads using:
<button onClick="document.location.href='doc.pdf'"> Doc Name </button>
However, we also need to track events for these (we don't want to inflate pageviews with PDF downloads), using:
<button onClick="_gaq.push(['_trackEvent', 'PDFs', 'Download', 'Doc Name']);""> Doc Name </button>
So, the issue is, how can I do GA event tracking without having the two requisite 'onClick's conflicting?
I was thinking about triggering the GA Event with 'onMouseDown,' but this would be less accurate, no?
Many thanks in advance.
<button class="download-pdf" onclick="document.location.href='doc.pdf';_gaq.push(['_trackEvent', 'PDFs', 'Download', 'Doc Name']);"> Doc Name </button>
Related
I have this piece of code on my site:
<div class="product-cta">
<span class="price">2.998,-</span>
<div class="btn-wrapper">
<a class="btn btn-primary btn-addtocart btn-xlarge" href="#" data-basket-url="/kassen/kurv/" data-product-id="25984"><i class="icon icon-cart"></i>Add to cart</a>
</div>
Is it possible to extract the numbers that come after data-product-id= to Google Tag Manager without pushing it through datalayer, only using what is in the existing code? This is on a product page and is to be used with the Facebook Pixel.
Yes, you can try couple methods without pushing it to the dataLayer. You could use JS (natively or jQuery) to scrape that info, or you could also use a built-in auto-event variable like this:
If you want to use the AEV, then you would likely be capturing that data on a click or something as the AEVs are "essentially [macros] that can be used to refer to, for example, the DOM element where a click occurred" (cf. https://www.simoahava.com/analytics/auto-event-tracking-google-tag-manager/).
I've being tried to install google events tracking on several sites but with no luck. I really want to know why its not working.
I installed events tracking on this site http://bedsndrinks.com/, the second "Book Now" button, here is the code I added to the button onClick=”ga(‘send’, ‘event’, ‘book form’, ‘click’, ‘book now’);“
I tried to use Google Analytics Debugger, but I don't see any "event" hitType. one thing I noticed is that the tracking code looks okay in source code but different when I inspect it in Chrome.
You need to check how your onclick is being rendered. When I loaded the page I saw the following:
<input id="sendBook" type="submit" value="Book Now"
onclick="”ga(‘send’," ‘event’,="" ‘book="" form’,="" ‘click’,="" now’);“="">
As you stated in your question, the format should be as below:
onclick="ga('send', 'event', 'book form', 'click', 'book now');
I have a button
<button data-z="myvalue">
click
</button>
which clicks i want to track in Google Analytics (Universal - analytics.js).
I try it with
<button data-z="myvalue"
onClick="ga('send', 'event', 'clickout', 'buttonclick', window.location.href, getAttribute('data-z'));>
click
</button>
With window.location.href i get the current URL (work), and with getAttribute('data-z') i want to get the myvalue from data-z (doesn't work).
Any help is appreciated!
PS: JQuery based solutions are welcome!
You are not telling your getAttribute method to which element the attribute belongs - getAttribute does not exists on its own, it is a method of a DOM object (your button in this case).
Since you are using inline Javascript the DOM object will be available by using the "this" keyword in the click function to call the getAttribute of the button:
<button data-z="myvalue"
onClick="ga('send', 'event', 'clickout', 'buttonclick', window.location.href, this.getAttribute('data-z'));>
click
</button>
Much less expensive than jQuery selectors (however using jQuery you could dispense with inline scripts and attach the click in an javascript function. Mixing HTML and JS is not elegant).
One possible solution, using jQuery, would be to evaluate for the the clicked element first to acquire the attribute value, and then pass that into the onclick handler:
onclick="var d=$(this).data('z'); ga('send', 'event', 'clickout', 'buttonclick', window.location.href, d);"
In Google Analytics I have set up a Goal for a button to be pressed on a sign up contact form. It is to collect email addresses for a mailing list. The problem is that it does not work. I have followed three guides on this offering slightly different code for alternate approaches but have not got it to work.
In GA the Goal is setup as follow...
Goal setup: Custom
Name: Contact
Type: Event
Category Equals to Contact
Action Equals to signup
GA is tracking the page and analytics from users can be viewed so we can see it is working. We are using CloudFlare and GA is added on the fly to all pages served through their cloud.
The code for the button is below.
<button type="submit" onclick=”_gaq.push([‘_trackEvent’, ‘Contact’, ’signup’])” class="btn btn-success">Sign up</button>
Any help on this would be appreciated.
If that is the exact code you are using for the button, then at least you would need to change the smart quotes (ie. the angled quotes) to straight quotes, so it should look like this:
<button type="submit" onclick="_gaq.push(['_trackEvent', 'Contact', 'signup'])" class="btn btn-success">Sign up</button>
I believe there are issues when the smart quotes are used.
I'm trying to add event tracking to my two buttons, but my button code looks like the following. I'm popping the user down to the section they clicked and hiding the other button's section:
<button class="btn btn-primary btn-lg" onclick="$('#order-form').show();$('#digital-download').hide();location.href='#Print'">
Order Print
Version
</button>
Anyone know how would I add the GA tracking code?
// Form Tracking for Google Analytics
$('.form-track').click(function(e){_gaq.push(['_trackEvent', 'Form', 'Completions', 'Form_'+$(this).attr('title')+'_'+location.href]); });
You can try something similar to this, adding the following code to your buttons:
$('.btn.btn-primary.btn-lg').click(function(e){
ga('send','event','your_category', 'your_action',$(this).text().trim());
})
When the button is clicked, it will send the event with your specified category, action, and the button text as the label. Note that you are using UA, so the event tracking syntax in your example is incorrect.