Help with GATC and var _gaq = _gaq || []; - javascript

Here is my GATC. It is located in the FOOTER of my application.
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXX-XX']);
_gaq.push(['_setDomainName', 'mysite.com']);
_gaq.push(['_addIgnoredRef', 'mysite.com']);
_gaq.push(['_setCustomVar',1,'UserType','Visitor', 2]);
_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';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
If I want to set some new custom variables in the BODY page, how would I do that?
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setCustomVar',2,'NewVar','Name', 2]);
</script>
Or
<script type="text/javascript">
_gaq.push(['_setCustomVar',2,'NewVar','Name', 2]);
</script>
To summarize:
I need to define some custom variables in the body of a page and my GATC is in the footer. I'm not quite sure the meaning of _gaq = _gaq and how it will effect this. Should it be in the snippet to define the custom variable in the body and then again in the GATC in the footer?

var _gaq = _gaq || [];
This line uses existing _gaq array or creates a new one and assigns to _gaq if it hasn't been defined(or contains falsy value). Therefore your first option (containing _gaq assignment) is better.

Related

Use PHP to find and remove some javascript tag but leave another tag

i have html containt some googletag like this :
<script type='text/javascript'>
var googletag = googletag || {};
googletag.cmd = googletag.cmd || [];
(function() {
var gads = document.createElement('script');
gads.async = true;
gads.type = 'text/javascript';
var useSSL = 'https:' == document.location.protocol;
gads.src = (useSSL ? 'https:' : 'http:') +
'//www.googletagservices.com/tag/js/gpt.js';
var node = document.getElementsByTagName('script')[0];
node.parentNode.insertBefore(gads, node);
})();
</script>
<script type='text/javascript'>
googletag.cmd.push(function() {
googletag.defineSlot('/6176201/Opera_120x600', [120, 600], 'div-gpt-ad-6176201-opera_120x600').addService(googletag.pubads());
googletag.defineSlot('/6176201/Opera_300x250', [300, 250], 'div-gpt-ad-6176201-opera_300x250').addService(googletag.pubads());
googletag.defineSlot('/6176201/Opera_728x90', [728, 90], 'div-gpt-ad-6176201-opera_728x90').addService(googletag.pubads());
googletag.defineSlot('/6176201/Contextuals_Operaweb', [557, 30], 'div-gpt-ad-6176201-contextuals_operaweb').addService(googletag.pubads());
googletag.pubads().collapseEmptyDivs();
googletag.pubads().enableSingleRequest();
googletag.enableServices();
});
</script>
<script type='text/javascript'>
googletag.cmd.push(function() { googletag.display('div-gpt-ad-6176201-opera_728x90'); });
</script>
<script type='text/javascript'>
googletag.cmd.push(function() { googletag.display('div-gpt-ad-6176201-opera_120x600'); });
</script>
<script type='text/javascript'>
googletag.cmd.push(function() { googletag.display('div-gpt-ad-6176201-opera_300x250'); });
</script>
It place random in html code, i want to find and remove it but do not touch another script tag like google analytics, etc. Just do Google search and look in to a lot of answer but still don't know how to do. Please help.
Try this one regex:
/<script type='text\/javascript'(?:(?!<\/?script).\n)+?(\n*.*?)*?(googletag)(\n*.*?)*(?:(?!<\/?script>))*?<\/script>/gi
Test it online
If each of your googletag occurences have exclusive script tag, I suggest to use preg_replace with this pattern:
%<script>(.*)(googletag)(.*)</script>%
This will match any <script>...</script> tag with at least one occurence of googletag inside, and this should not touch Google Analytics code.
I changed default delimiter / to % to use slash in tag end without problem. Just use preg_replace:
preg_replace(pattern, "", $htmldoc);

Load a generic script with a custom UID

I want to give users a snippet to put on their own sites which loads a generic javascript file with a unique ID for that user. This is so the content which loads up is dependant on which user id is associated with it.
My best guess so far is something like this:
<script type="text/javascript">
var UID = '';
(function() {
var url = www.test.com/embed.js';
})();
</script>
<noscript>Please enable JavaScript to view this content</noscript>
content powered by <span class="logo">Test</span>
I havent had a lot of luck with this code. Can anyone explain how I should be approaching this please?
Here is the way that google does it. Let me know if you need help applying it:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-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';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
Ref. https://developers.google.com/analytics/devguides/collection/gajs/
You need to append <script> tag to the DOM.
You should put your entire script inside a single scope (function)
(function(window, uid) {
var document = window.document;
// create your <script> element
var e = document.createElement("script");
e.src = "//my.website.com/my-script.js?user-id=" + window.encodeURIComponent(uid);
// insert it before the very first <script> element already in DOM
var s = document.body.getElementsByTagName("script")[0];
s.parentNode.insertBefore(e, s);
})(window, "some-user-id");

GA - E-commerce not tracking items

I have severe trouble finding the issue in the following javascript code.
Google Analytics is tracking my transactions just fine, but it does not show any products.
This code is taken from the "view sourcecode" at the receipt page.
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'xxxxxxx-x']); // I replaced my account no.
_gaq.push(['_trackPageview']);
_gaq.push(['_set', 'currencyCode', 'DKK']);
_gaq.push(['_addTrans',
'28996',
'xxxxx.xx', // I replaced the domainname with xxxxx.xx
'104.00',
'20.80',
'0.00',
'fredercia',
'Denmark'
]);
_gaq.push(['_addItem',
'28996',
'150649',
'BRAUN ORAL-B ELTANDBØRSTE',
'99.00',
'1'
]);
_gaq.push(['_addItem',
'28996',
'150000',
'5 dages LOP-salg',
'5.00',
'1'
]);
_gaq.push(['_trackTrans']);
(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';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
Can anyone see the issue here?
Looks like you're missing the category parameter to _addItem -- see the Google Docs for _addItem.
While the category parameter is optional, leaving it out will cause the required quantity parameter to be missed.
You can pass an empty string for category if you don't have one:
_gaq.push(['_addItem',
'28996',
'150649',
'BRAUN ORAL-B ELTANDBØRSTE',
'',
'99.00',
'1'
]);

Double Click Publisher 2 leaderboard banner in one page

Hi I try to put the 2 leaderboard in one page but its shows only one leaderboard.
This is my Header code
<script type='text/javascript'>
var googletag = googletag || {};
googletag.cmd = googletag.cmd || [];
(function() {
var gads = document.createElement('script');
gads.async = true;
gads.type = 'text/javascript';
var useSSL = 'https:' == document.location.protocol;
gads.src = (useSSL ? 'https:' : 'http:') +
'//www.googletagservices.com/tag/js/gpt.js';
var node = document.getElementsByTagName('script')[0];
node.parentNode.insertBefore(gads, node);
})();
</script>
<script type='text/javascript'>
googletag.cmd.push(function() {
googletag.defineSlot('/179670003/Leaderboard', [728, 90], 'div-gpt- ad-1363780690140-0').addService(googletag.pubads());
googletag.enableServices();
});
</script>
This is my Document Body
<!-- Leaderboard -->
<div id='div-gpt-ad-1363780690140-0' style='width:728px; height:90px;'>
<script type='text/javascript'>
googletag.cmd.push(function() { googletag.display('div-gpt-ad-1363780690140-0'); });
</script>
</div>
<!-- Leaderboard -->
<div id='div-gpt-ad-1363780690140-0' style='width:728px; height:90px;'>
<script type='text/javascript'>
googletag.cmd.push(function() { googletag.display('div-gpt-ad-1363780690140-0'); });
</script>
</div>
will it possible to add 2 leaderboard banner in one page?
Try creating a second ad unit called Leaderboard2. You can only display an adunit once per page. Then you could create a placement that includes both of your Leaderboard ad units and target all of your Leaderboard line items at the placement, they will then intelligently be shared between the two Leaderboard ad units.

Simple Question About Adding Data To Javascript

I am using an mvc application and in _Layout.cshtml page I have some javascript I wanna pass it a value of
System.Configuration.ConfigurationManager.AppSettings["anID"].ToString()
here
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount',PASS THAT HERE!]);
How do I do this? I am not sure how to pass data to javascript??
You could try like this:
<%
var value = System.Configuration.ConfigurationManager.AppSettings["anID"].ToString();
%>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', <%= value %>]);
</script>
or with the Razor view engine:
#{
var value = System.Configuration.ConfigurationManager.AppSettings["anID"].ToString();
}
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', #value]);
</script>
In situations where you have more javascript than that, and are keeping it in a separate file from the view, you can also do this:
<script type="text/javascript">
var _anId = #System.Configuration.ConfigurationManager.AppSettings["anID"].ToString();
var _somethingElseNeededInJsFile = #Model.SomethingNeeded;
</script>
Then in the separate javascript file, you can reference the variables easily.
var _gaq = _gaq || [];
_gaq.push(['_setAccount', _anId]);
alert('You needed this: ' + _somethingElseNeededInJsFile);

Categories

Resources