[英]Unable to pass a value from a function to a data layer array using javascript
I am trying to collect the value (Showing 1 -10 of 74) in the below HTML 我正在尝试在下面的HTML中收集值(显示74中的1 -10)
<body>
<p data-brackets-id="1217" class="search">
<!--No Results Found-->
<small data-brackets-id="1218" data-bind="visible:(searchResults().length == 0), html: translations.noResults" style="display: none;">No Results To Display Please Enter A Search Term Above</small>
<!--Error Message-->
<small data-brackets-id="1219" class="error-message" data-bind="visible:errorMessage, html:errorMessage" style="display: none;"></small>
<!--Search Result Count-->
<small data-brackets-id="1220" data-bind="html:currentlyShowing, visible:(searchResults().length > 0)">Showing 1 -10 of 19300</small>
</p>
</body>
by using a window.onload function 通过使用window.onload函数
/** * returns the text for "Showing 1 -10 of 19300" after page loads and pass the value to the dataLayer. / ** *在页面加载后返回“显示19300的1 -10”的文本,并将该值传递给dataLayer。 */ * /
function showing () {
var showResults = document.querySelector(".search small :nth-child(3)").innerHTML;
}
window.onload = showing;
and pass into a dataLayer object (search Results) so that google analytics can grab the value from the dataLayer. 并传递到dataLayer对象(搜索结果)中,以便Google Analytics(分析)可以从dataLayer中获取值。
<!-- start dataLayer -->
<script>
var adobeInfo = {
pageInfo: {
'pageType': "welcome page",
'Search Results': showResults
}
}
<!-- end dataLayer -->
however I can't seem to pass the value of the querySelector to the dataLayer as I keep getting "undefined". 但是我似乎无法将querySelector的值传递给dataLayer,因为我一直在获取“未定义”。 is it the windows.onload that isn't allowing me to pass the value or am I just using the wrong code syntax for what I need in order to accomplish this task? 是windows.onload不允许我传递值,还是我使用了错误的代码语法来完成此任务?
thank you for any help in advance. 谢谢您的任何帮助。
<body> <p data-brackets-id="1217" class="leader-1 no-trailer"> <!--No Results Found--> <small data-brackets-id="1218" data-bind="visible:(searchResults().length == 0), html: translations.noResults" style="display: none;">No Results To Display Please Enter A Search Term Above</small> <!--Error Message--> <small data-brackets-id="1219" class="error-message" data-bind="visible:errorMessage, html:errorMessage" style="display: none;"></small> <!--Search Result Count--> <small data-brackets-id="1220" data-bind="html:currentlyShowing, visible:(searchResults().length > 0)">Showing 1 -10 of 74</small> </p> <script> function results () { alert(" alert inside results function"); var showResults = document.querySelector("leader-1.no-trailer :nth-child(3)").innerHTML; // get the results of the showing info from search page. showResults.select(); } window.onload = results(); </script> <!-- Use dataLayer object to pass values to analytics --> <script> var dataLayer = { pageInfo: { 'pageType': "welcome page", 'show Results': showResults // value of show results should go here (Showing 1 -10 of 74) } } </script> </body>
so I guess my real question is how do I pass the value of a window.onload function to a global variable so that I can pass it to my dataLayer? 所以我想我的真正问题是如何将window.onload函数的值传递给全局变量,以便可以将其传递给dataLayer?
Setting showResult
outside of datalayer
makes the program hard to read and debug. 设置showResult
之外的datalayer
,使程序难以阅读和调试。 My Suggestion is define a loader for the data layer
like bellow: 我的建议是为像下面这样的data layer
定义一个加载器:
var dataLayer = {
pageInfo: {
'pageType': "welcome page",
},
load: function() {
this.showResults = document.querySelector("leader-1.no-trailer :nth-child(3)").innerHTML; // get the results of the showing info from search page.
this.showResults.select();
}
}
// windows onload event
windows.onload = function() {
dataLayer.load();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.