简体   繁体   中英

Capture referring URL parameters w/ Javascript & Pass to Google Analytics

New to javascript. I'm trying to capture the referring url for a goal page, which has some query parameters structured like so:

example.com/vehicle/?stock=12345

Once I get the query parameters, I want to pass them into Google Analytics using the _gaq.push method. I dont' expect anyone to have the gaq.push method memorized, but is my code structured correctly?

<script type="text/javascript">
  var vehicleURL=document.referrer;
  var stock=vehicleURL.substring(vehicleURL.indexOf('?')+7, vehicleURL.length);

  _gaq.push(['track_Event', 'Leads', 'Vehicle', stock]);
</script>

Thanks!

You seem to be on the right track:

var vehicleURL="example.com/vehicle/?stock=12345";
var stock=vehicleURL.substring(vehicleURL.indexOf('?')+7, vehicleURL.length);

alert(stock); //(12345)

_gaq.push(['_trackEvent', 'Leads', 'Vehicle', stock]);

Keep in mind that Google Analytics has little tolerance for error so make sure that _gaq.push is spot on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM