AdWords is now Google Ads. Our new name reflects the full range of advertising options we offer across Search, Display, YouTube, and more. Learn more

Analytics
2K members online now
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

What code do I add to event tracking for a link to open in a new window?

Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

Hello,

 

I would like to track an event (clicks on our social media links) through Google Analytics, but when I add the target="_blank code so that the pages still open in new windows, the tracking data no longer works. How can I add code to the following so that the Facebook link will still open in a new window?

 

<a href='http://www.facebook.com/north.island.college' onclick="var href = this.href; ga('send','event','Facebook','click through', this.href, {'hitCallback': function(){document.location = href;}}); return false;">Facebook Button link</a>

 

Much thanks!

Emily

2 Expert replyverified_user
1 ACCEPTED SOLUTION

Accepted Solutions
Marked as Best Answer.
Solution
Accepted by topic author Nic W
May 2016

Re: What code do I add to event tracking for a link to open in a new window?

Top Contributor
# 7
Top Contributor
No problem, Emily. Hopefully it works, because I don't see anything wrong with it, and as I mentioned, I tested it out on my site and it looks to be working well.
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

View solution in original post

Re: What code do I add to event tracking for a link to open in a new window?

Community Manager
# 2
Community Manager
Note: This thread has been moved by a Community Manager to the Google Analytics Community, from it's previous location in the AdWords Community.

Re: What code do I add to event tracking for a link to open in a new window?

Top Contributor
# 3
Top Contributor
Hi, Emily,

I copy and pasted your code above and it's working correctly for me - I can see the event hit, and I'm taken to your FB page. What is the exact code you are using that causes it to not work?
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: What code do I add to event tracking for a link to open in a new window?

Visitor ✭ ✭ ✭
# 4
Visitor ✭ ✭ ✭
Hi Nicky, thanks for checking. The issue is that the event isn't showing up in Google Analytics. When I input the event tracking code without target="_blank", it seems to appear in the GA data. But when I add the target="_blank" code, data no longer appears. From what I've read online, this seems to be a problem for others as well. So, I'm wondering what snippet of code I can add to the string above so that the link opens in a new window, AND it tracks on the back end of GA.

Thanks!
Emily

Re: What code do I add to event tracking for a link to open in a new window?

Top Contributor
# 5
Top Contributor

Hi, Emily, it should be working. Here's what I'm trying:

 

<a href='http://www.facebook.com/north.island.college' target="_blank" onclick="var href = this.href; ga('send','event','Facebook','click through', this.href, {'hitCallback': function(){document.location = href;}}); return false;">Facebook Button link</a>

Please install GA Debugger for Chrome and verify that the hit is being sent through in the console window. Also check with real time reporting.

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: What code do I add to event tracking for a link to open in a new window?

Visitor ✭ ✭ ✭
# 6
Visitor ✭ ✭ ✭
Thank-you for this! I greatly appreciate the info, and advice. Smiley Happy
Marked as Best Answer.
Solution
Accepted by topic author Nic W
May 2016

Re: What code do I add to event tracking for a link to open in a new window?

Top Contributor
# 7
Top Contributor
No problem, Emily. Hopefully it works, because I don't see anything wrong with it, and as I mentioned, I tested it out on my site and it looks to be working well.
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: What code do I add to event tracking for a link to open in a new window?

Visitor ✭ ✭ ✭
# 8
Visitor ✭ ✭ ✭
Yes, it worked perfectly. Thanks again for the help! Smiley Happy

What code do I add to event tracking for a link to open in a new window?

j j
Follower ✭ ✭ ☆
# 9
j j
Follower ✭ ✭ ☆

Always you hopefully works and move to you thread G.A.T.C expat.....