how can i grab the arguments of a javascript function?
below is my code,
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-45-9");
pageTracker._trackPageview();pageTracker._addTrans("3128", "", "20.4600", "0.0000", "8.1900", "SFO", "CA", "US");
pageTracker._addItem("3128", "23317", "Cheese ", "", "6.2900", "1");
pageTracker._addItem("3128", "23318", "Lazzaroni", "", "1.9900", "1");
pageTracker._addItem("3128", "23316", "Italian Food", "", "3.9900", "1");
pageTracker._trackTrans();
</script>
For the code above, I want to grab the arguments of the functions pageTracker._addItem() to use it in my javascript开发者_Go百科 code. How would i do that?
The problem is I do not have access to the complete source code, but only i can add some javascript and html to it, this site is hosted at network solutions, and they do not provide access to their code for some plans.... so i need to grab the arguments of the javascript function above and use it my javascript, which i can add to the code...
Thanks
Use this:
GAitems = [];
GAitems.oldAdd = pageTracker._addItem;
pageTracker._addItem = function() {
GAitems.push(arguments);
GAitems.oldAdd.apply(pageTracker, arguments)
}
And all the added items will be available in the GAitems
array for you (and they are also added to Google Analytics correctly).
Based on the new information, it makes more sense to answer this thing now.
Is the Network Solutions source contained in an iframe? If yes, do the protocol, domain, and ports match for where your script lives, and where the Network Solutions script comes from? If it does not, then you won't be able to access any part of it.
If it's not in an iframe, and not hidden inside closures, then you can get access to the arguments of pageTracker._addItem(..)
, but there's one more problem with that. Since I am guessing that you can't arbitrarily insert your code in the middle of their code, you can't do this:
var pageTracker = _gat._getTracker("UA-45-9");
// with a swift ninja move, replace pageTracker._addItem with your
// own implementation here before its first usage
// but after it has been created
..
pageTracker._addItem("3128", "23317", "Cheese ", "", "6.2900", "1");
..
So I think you will have to go one step higher here, and swap out the implementation of _gat._getTracker(..)
itself to return a modified pageTracker
object. However, the order of things must be:
- Google Analytics is included (so _gat gets defined)
- Your code comes here which swaps out the implementation of
._getTracker
and modifies the resulting object's_addItem
method. - Network Solutions code follows
If you cannot place your code between Google Analytics <script>
, and Network Solutions code, then there's little that you can do. However, if that were possible, here's how you could do the intercept:
(function(_getTracker) { // _getTracker refers to the original method
_gat._getTracker = function() { // swap out the original _gat._getTracker
var _pageTracker = _getTracker.apply(_gat, arguments);
var _addItem = _pageTracker._addItem; // original _addItem
_pageTracker._addItem = function() { // swap out the original _addItem
console.log(arguments); // record the arguments here
_addItem.apply(_pageTracker, arguments); // send to original method
};
return _pageTracker; // return the modified pageTracker
};
})(_gat.getTracker);
Assuming you mean you want to analyze above code in some JavaScript that comes after, I would say there is no really easy way to do this. You may be able to get the added item
entries back out of the pageTracker
object but that sounds hard.
If at all possible, I would recommend keeping track of the added items in an own array.
精彩评论