I have following script in my web site to push a custom variable to Google analytics. But even after 5 days this data is not appear in analytics. Can anyone notice something that I'm doing wrong here?
<!-- BEGIN GOOGLE ANALYTICS CODE -->
<script type="text/javascript">
//<![CDATA[
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
})();
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'xxxxxxxxxx']);
_gaq.push(['_trackPageview']);
_gaq.push(['_setCustomVar', 1, 'Product SKU','Test Data', 3]);
//]]>
</script>
<!-- END GOOGLE ANALYTICS CODE -->
Additional info : This script is from Google Analytis module for Magento, I'm over writing the _toHTML method in GA.php
The line that actually sets the custom variable comes after your _trackPageview() call, so the custom variable is never actually sent to the Analytics servers. You need to add either a _trackPageview() or _trackEvent() call after you set your custom variable(s).
_gaq.push(['_setAccount', 'xxxxxxxxxx']);
_gaq.push(['_trackPageview']);
_gaq.push(['_setCustomVar', 1, 'Product SKU','Test Data', 3]);
_gaq.push(['_trackEvent', 'Tow Truck', 'go', '-', 0, true]);
Here's some additional reference material on this: http://www.lunametrics.com/blog/2011/12/30/google-analytics-custom-variables-working/
Related
I'm using the new invisible recaptcha on my website, and in the homepage I have two forms with recaptcha validation.
Because I'm using more than one recaptcha in the same page, I had to use the method and set it to explicit.
Anyway, I lost an entire day of searching to understand that I need to use grecaptcha.execute() to make it work, but it didn't work even with it, I'm probably doing something wrong, but I don't know what exactly is it, here's some code:
<script type="text/javascript" charset="utf-8">
var onloadCallback = function() {
var recaptchas = document.querySelectorAll('div[class=g-recaptcha]');
for( i = 0; i < recaptchas.length; i++) {
grecaptcha.render( recaptchas[i].id, {
'sitekey' : '',
'badge' : 'inline',
'size' : 'invisible'
});
grecaptcha.execute(i);
}
}
</script>
In the forms I'm using it like this:
<div class="g-recaptcha" id="rc1"></div>
And in the end of the page:
<script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit" async defer></script>
I really don't know why it isn't working now, I've set grecaptcha.execute to "i" because I've read that it's a 0 based index, so it should work, but it doesn't
For those wondering how to do it, I got it to work by loading both explicitly, and it worked, like this:
<script src="https://www.google.com/recaptcha/api.js?onload=myCallBack&render=explicit" async defer></script>
<script>
var recaptcha1;
var recaptcha2;
var myCallBack = function() {
//Render recaptcha1 on element with ID "recaptcha1"
recaptcha1 = grecaptcha.render('recaptcha1', {
'sitekey' : 'your-site-key',
'theme' : 'light'
});
//Render recaptcha2 on element with ID "recaptcha2"
recaptcha2 = grecaptcha.render('recaptcha2', {
'sitekey' : 'your-site-key',
'theme' : 'light'
});
};
</script>
In summary, I need to find a way to use value from .properties files in a Thymeleaf template.
In details now, I want to have Google Analytics on my page. There are development and production application build profiles. Each of these profiles has its directory with properties. The account details are in these properties files.
For now I assign the appropriate values to variables in the model and use them.
<script type="text/javascript" th:inline="javascript">
var _gaq = _gaq || [];
var _gaId = /*[[${googleAnalyticsId}]]*/'';//these are the variables I'm talking about
var _gaDomainName = /*[[${googleAnalyticsDomainName}]]*/'';
_gaq.push(['_setAccount', _gaId]);
_gaq.push(['_setDomainName', _gaDomainName]);
_gaq.push(['_trackPageview']);
</script>
But it seems quite dirty, let alone the manoeuvring that needs to be done in order to change any of these values.
What I'd like to do in the page template is to just point to the property name like we do in Spring with #Value. E.g.
<script type="text/javascript" th:inline="javascript">
var _gaq = _gaq || [];
var _gaId = /*[[${google.analytics.id}]]*/'';//I want to assign values directly from the properties file
var _gaDomainName = /*[[${google.analytics.domain.name}]]*/'';
_gaq.push(['_setAccount', _gaId]);
_gaq.push(['_setDomainName', _gaDomainName]);
_gaq.push(['_trackPageview']);
</script>
Thanks in advance.
Why you can't use the standard message-source way with #{googleAnalyticsDomainName}?
I have this code in my product page after an item has been added to cart:
<script type="text/javascript>
//<![CDATA[
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
})();
var _gaq = _gaq || [];
_gaq.push(['_setCustomVar', 1, 'Page Type', 'Product', 3])
_gaq.push(['_setCustomVar', 2, 'Product Name', 'Gold Bracelet', 3]);
_gaq.push(['_trackEvent', 'Product Page', 'Added To Basket', 'Gold Bracelet', , 0]); // this is the line that isn't working.
_gaq.push(['_setAccount', 'UA-XXXXXXX-1']);
_gaq.push(['_trackPageview']);
//]]>
</script>
It seems the pageview is tracked, the two custom variables, but not the _trackEvent. Like I said in the title, if I copy and paste this code into Chrome Dev Console, the events show up just fine. I added an alert() to the code that returns the _trackPageview and it worked, so the JS is definitely getting parsed.
Any idea what i'm doing wrong?
Thank you
If the opt_value parameter isn't an integer (or undefined), the event won't be recorded.
Do you want this event to affect bounce rate calculations? If so, the opt_noninteraction can be left off since the default value is false:
_gaq.push(['_trackEvent', 'Product Page', 'Added To Basket', 'Bodysuit...']);
If you don't want the event to affect the bounce rate, use:
_gaq.push(['_trackEvent', 'Product Page', 'Added To Basket', 'Bodysuit...', undefined, true]);
I missed this in my first look at your code -- since _trackEvent is before _setAccount, the data it sends get's recorded to a default account (something like 'UA-XXXXX-X').
Move the _trackEvent call anywhere after _setAccount.
Also, since you've got both _trackEvent and _trackPageview in the same block of code, you'll want to be sure that the opt_noninteraction parameter is true.
Event tracking is not working with my Magento 1.5.0.1 CE installation. I have updated the code app/code/local/Mage/GoogleAnalytics/Block/GA.php to:
<!-- BEGIN GOOGLE ANALYTICS CODE v2 -->
<script type="text/javascript">
//<![CDATA[
var _gaq = _gaq || [];
' . $this->_getPageTrackingCode($accountId) . '
' . $this->_getOrdersTrackingCode() . '
_gaq.push(["_trackPageLoadTime"]);
(function() {
var ga = document.createElement(\'script\'); ga.type = \'text/javascript\'; ga.async = true;
ga.src = (\'https:\' == document.location.protocol ? \'https://ssl\' : \'http://www\') + \'.google-analytics.com/ga.js\';
(document.getElementsByTagName(\'head\')[0] || document.getElementsByTagName(\'body\')[0]).appendChild(ga);
})();
//]]>
</script>
<!-- END GOOGLE ANALYTICS CODE -->';
Then I added an event tracking link to my homepage:
LINK
So I tested this in firefox via firebug and the events are not working. Can someone please help a brother out?
Also the code is being inserted after the opening tag.
This is how it renders:
<!-- BEGIN GOOGLE ANALYTICS CODE v2 -->
<script type="text/javascript">
//<![CDATA[
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXX-X']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
})();
//]]>
</script>
<!-- END GOOGLE ANALYTICS CODE -->
Thanks!
You might not see it in Firebug because it executes so quickly. I would recommend testing via a proxy tool or the Live HTTP Headers plugin. See the tools recommended here.
Your syntax is correct.
Additionally, you may need to add a setTimeout() of 500ms or so to delay the click so that you don't encounter a race condition where the browser goes to that link prior to completing the execution of the tracking call. I have an example of this on a blog post I wrote:
LINK
I placed the google analytics code in my magento site 4 or 5 days ago, I think I have waited enough and that the report should be working.
However I still see Page Load Time as 0.00.
If you see the generated code from magento its like this:
My website is this
<!-- BEGIN GOOGLE ANALYTICS CODE -->
<script type="text/javascript">
//<![CDATA[
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
})();
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-1']);
_gaq.push(['_trackPageview']);
_gaq.push(['_trackPageLoadTime']);
//]]>
</script>
<!-- END GOOGLE ANALYTICS CODE -->
I'm able to get _trackPageLoadTime to work just fine.
You can simulate it by editing the __utma cookie. The second period delimited value is the unique user ID. Just change the second to last digit to be 0, and it will track page load time for you.
Since ga.js is configured to only run for 10% of traffic, and only around 30% of users have browsers that support is, it'll usually only collect data from 3% of your users.