0

I'm measuring submissions of a form that appears on multiple pages as an Analytics event. I want to pass the document.location of the current page in an Analytics event.

Here is what I tried:

1. onClick="ga('send', 'event', 'email2', 'signup', document.location);"
2. onClick="var _loc = document.location;ga('send', 'event', 'email2', 'signup', #&39;_loc#&39;);"

In situation #1 the event label passed in Analytics is document.location and not the actual URL

In situation #2 the event wasn't passed to Analytics

Note that I'm using Universal Analytics

Reference for event tracking

Bogdan
  • 1,053
  • 5
  • 20
  • 41

1 Answers1

2

The actual URL of the page is window.location.href.

onClick="ga('send', 'event', 'email2', 'signup', window.location.href);"

document.location and window.location are objects, not the URL itself. If you want to pass the URL, not the location object, then you should fetch the .href property as shown above.

Though doucment.location and window.location both have the info you want, you probably want to use window as the source of the info, not document. See this answer for use of window.location vs. document.location.

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979