Analytics
2.2K members online now
2.2K members online now
Dive into multiple domain (Cross/Sub) tracking, implementing Ecommerce and Enhanced Ecommerce, setting up Event tracking, and Universal Analytics code.
 
Guide Me
star_border
Reply

Track Javascript Event - Help

Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

I need some help here and I'm pretty new to this, but it's been driving me insane figuring out why this isn't working.

 

right now I have an index.html page, that has a javascript snippet that turns the entire page into a "button".  I need to track onmousedown event without using GTM.

 

Here's is my code:

GA:

<script>
(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');

ga('create', '*****', 'auto');
ga('send', 'pageview');

</script>

 

And the Javascript snippet that turns the page into one big "button"

 

<script type="text/javascript">
var tile = new HTMLLiveTile.TileController();
window.onmousedown = function() {
tile.openStoreProduct("var1", "var2", "var3");
}
</script>

 

The HTML action I have in the body:

<a href="javascript&colon;void(0)" onmousedown="ga('send', 'event', { eventCategory: 'Button Click', eventAction: 'Click'});"><img src="./images/image.png"></a>

 

How do I make this record the event into my GA account?  I have a feeling I need to track the function when it gets hit, but I don't know how to do that in the HTML snippet.

1 Expert replyverified_user

Re: Track Javascript Event - Help

Top Contributor
# 2
Top Contributor

Hi, Edwin Y, 

I haven't taken too deep of a look, but if doens't look like anything's wrong.

 

You might want to change

href="javascript&colon;void(0)"

to this

href="javascript&colon;void(0)"

I don't think it matters because I've tested both and both work. Have you tried verifying that the event hit is sent using GA Debugger for Chrome?

 

 

Nicky Yuen, Google Analytics Top Contributor
Was my response helpful? If yes, please mark it as the ‘Best Answer.’ Learn how here.
Find me on: Google+ | LinkedIn

Re: Track Javascript Event - Help

Visitor ✭ ✭ ✭
# 3
Visitor ✭ ✭ ✭
thank you for your reply. I'm actually not seeing any events fired in GA debugger.

Some background, with the actual javascript snippet, it creates an overlay on the entire page. You could say the website is just an image with the javascript function being an overlay. I'm trying to figure out how to capture the event when the user clicks on the overlay. When the image appears, I can track a visit and all the amazing things GA can do, but once the user clicks on the image, it takes the user to a third party internal browser. Is it possible with the HTML snippet or does it need to be rewritten? Maybe i need to call a variable in the JS to the HTML?

Re: Track Javascript Event - Help

Top Contributor
# 4
Top Contributor
Hi Edwin, which domain is the image on? Is it the same domain that you control?

What it may come down to is whether or not you have the tracking on the modal or page where you want to do your tracking. If you can't get the tracking onto that element, then you can't track anything (basically).
Nicky Yuen, Google Analytics Top Contributor
Was my response helpful? If yes, please mark it as the ‘Best Answer.’ Learn how here.
Find me on: Google+ | LinkedIn

Re: Track Javascript Event - Help

Visitor ✭ ✭ ✭
# 5
Visitor ✭ ✭ ✭
Yep the image/webpage is on the same domain I control. I did more digging today, and so what happens when the user hits onmousedown, it triggers this piece of code in the external .js script:

var HTMLLiveTile;
(function (HTMLLiveTile) {
var TileController = (function () {
// Setup
function TileController() {
this.inHighlightState = false;
this.inClickState = false;
this._attachHandlers();
}
TileController.prototype._attachHandlers = function () {
var _this = this;
window.addEventListener("mousedown", function (e) {
return _this._handleClick(e, true);
}, false);

Is there a way for me to track that action and have it send the event to GA?

Re: Track Javascript Event - Help

[ Edited ]
Top Contributor
# 6
Top Contributor

The code you had before (onclick="ga('send', ...."); ) should work. Perhaps you could share the link so I could take a look.

Nicky Yuen, Google Analytics Top Contributor
Was my response helpful? If yes, please mark it as the ‘Best Answer.’ Learn how here.
Find me on: Google+ | LinkedIn