Multiple Google Adwords conversion labels on same page - google-analytics

Can I have more than one google adwords conversion label on a single web page? It looks like the javascript variables would just overwrite each other.
What if I remove the javascript variables and just keep the noscript link?
conversion 1
var google_conversion_id = 123;
var google_conversion_language = "en";
var google_conversion_format = "3";
var google_conversion_color = "666666";
var google_conversion_label = "abc";
var google_conversion_value = 0;
conversion 2:
var google_conversion_id = 456;
var google_conversion_language = "en";
var google_conversion_format = "3";
var google_conversion_color = "000000";
var google_conversion_label = "def";
var google_conversion_value = 0;
followed by script tag:
<script type="text/javascript" src="http://www.googleadservices.com/pagead/conversion.js"></script>
and sample noscript tags:
<div style="display:inline;"><img height="1" width="1" style="border-style:none;" alt="" src="http://www.googleadservices.com/pagead/conversion/..."/>
</div>

What worked for us was just including the entire block of code (including the <script type="text/javascript" src="http://www.googleadservices.com/pagead/conversion.js"></script> tag itself) twice. Because the script tag contains code which is executed immediately, including it twice will cause it to execute twice - each time using the variables declared immediately before it.

The Javascript function.
var goog_report = function(id, label, value){
if(typeof(value)==='undefined') value = 0;
var base_url = 'www.googleadservices.com/pagead/conversion/';
var img = new Image(1,1);
img.src = base_url + id +'/?label='+label+'&value='+ value +'&script=0';
};
Set your Google variables.
var goog_id = 1234;
var goog_label = 'xyz';
var goog_value = 10.99;
Example 1: Call within HTML.
<script>
goog_report(goog_id, goog_label, goog_value);
</script>
Example 2: Call within an event handler.
<script>
var handleSomeEvent = function(evt) {
goog_report(good_id, goog_label, goog_value);
};
</script>
Example 3: Call after jQuery Ajax success call.
<script>
$.ajax({
type: "POST",
url: "/charge/",
data: $('form').serialize(),
success: function(data) {
goog_report_purchase(goog_id, goog_label, data.charge_amount);
}
});
</script>
Example 4: Hard-coded OnClick event on Anchor Element
Boom! Conversion.

Note that as of October 2017, you can (and should) use Google's new gtag.js, which is a new web tagging library that replaces the older AdWords website conversion tracking and remarketing tags.
gtag.js allows you to send tracking data to multiple AdWords accounts by adding a call to the ‘config’ command for every account you’ll be using, specifying each account’s conversion ID:
<!-- Global Site Tag (gtag.js) - Google AdWords: GOOGLE_CONVERSION_ID_1 -->
<script async src="https://www.googletagmanager.com/gtag/js?id=AW-GOOGLE_CONVERSION_ID_1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments)};
gtag('js', new Date());
gtag('config', 'AW-GOOGLE_CONVERSION_ID_1');
gtag('config', 'AW-GOOGLE_CONVERSION_ID_2');
</script>
This is the pattern that modern solutions should follow.

You need to insert the tag below each group of variables
// first slot of variables
// second slot of variables
// you don't need 'var' statement anymore
Or you insert on only the content of the noscript tag, but without (is mostly as good as the javascript tag)

Related

Google Analytics Custom dimension is undefined in gtag

I have set up 4 dimensions. My code as follows,
<script>//<![CDATA[
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'UA-XXXXX-1');
gtag('set', {
'user_id': _spPageInfo.userId,
'JobTitle': jobTitle,
'Department': department,
'UserLocation': Location
});
gtag('send', 'pageview');
//]]>
</script>
<script>//<![CDATA[
// <!-- Google Tag Manager -->
(function (w, d, s, l, i) {
w[l] = w[l] || [];
w[l].push({
'gtm.start': new Date().getTime(),
event: 'gtm.js'
});
var f = d.getElementsByTagName(s)[0],
j = d.createElement(s),
dl = l != 'dataLayer' ? '&l=' + l : '';
j.async = true;
j.src =
'https://www.googletagmanager.com/gtm.js?id=' + i + dl;
f.parentNode.insertBefore(j, f);
})(window, document, 'script', 'dataLayer', 'GTM-XXXX');
// <!-- End Google Tag Manager -->
//]]>
</script>
I have set up dimenstion in tag manager as well.
In debug mode i can see "Conatiner Loaded" instead of "Page Load",
And in datalayer i can not see any event name or dimension name.
After 24 hours also its showing undefined.
Please,Can anyone guide me what i am missing here?
you'll need to pass custom_map parameter with your config command you map your named dimension values to custom dimension. See an example here. Moreover, you'll need to call set before config since config is actually passing pageview hit to google analytics so calling set after config won't affect your data
Eventually, your code would look something like this:
gtag('js', new Date());
gtag('set', {'custom_map': {
'dimension1': 'department',
'dimension2': 'jobTitile',
// the same for other custom dimensions
}})
gtag('config', 'UA-XXXXX-1', { 'jobTitile': 'engineer', 'department':'maintenance' });
Moreover, avoid using both GTM and gtag code at the same time otherwise you'll get duplicated data sent to analytics.

Google Analytics Gtag Multiple Analytics Account Tracking IDs

From what I can see Google seem to be phasing out analytics.js now in favor of their tag manager.
How do I fire google analytics new gtag tracking code for multiple analytics accounts?
Something like this:
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-108285779-2"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-108285779-2');
gtag('config', 'ANOTHER CODE');
gtag('config', 'ANOTHER CODE');
</script>
In short:
Yes, you can add the same type of product multiple times by calling gtag('config', ...) for each respective Google account + property ID you have.
Details:
It's 2021 and I had the same question but was too paranoid to trust this thread's top voted answer because it described a different experience than my own when testing how this works. Firstly, though, in order to answer OP's question in 2021 we have to look at two entries in Google's official docs, because neither entry fully answers the question but when brought together they can give us a bit more confidence in how to solve this:
Can I add more than one type of product using gtag('config', ...)? (Answer: yes.) (Docs)
Can I add more than one of the same type of product using gtag('config', ...)? (Answer: yes.) (Docs)
Here's an example snippet of how I accomplished OP's scenario using JavaScript. If you try this in your browser's console you should see a unique script get added for each ID you set in the below snippet's googleIds array.
Notes:
Notice that the snippet's googleIds array contains five IDs.
Notice that, after running the snippet in your browser console, five <script> tags get set to the page, but that the snippet itself only explicitly built and set one of the tags to the .
The rest of the tags get added after their respective IDs are pushed into the dataLayer, and after the first script is initialized (i.e. the element is constructed + set to the ). The order of these two steps doesn't matter (i.e. You can initialize first and then push your IDs to the dataLayer, or push your IDs to the dataLayer and then initialize).
// An array of IDs I want to load on the same page(s) at the same time
var googleIds = ["AW-00000000", "AW-00000001", "AW-00000002", "DC-00000000", "UA-00000000-1"];
// Setting dataLayer & gtag to window because I'm using a custom code text field in a tag management system
window.dataLayer = window.dataLayer || [];
window.gtag =
window.gtag ||
function() {
window.dataLayer.push(arguments);
};
window.gtag("js", new Date());
// Flag used to ensure script only set with first ID, and rest of IDs are pushed to dataLayer
var gtagScriptExists = false;
// ID validation regex. Only tested with AW-*, but DC & UA are also valid prefixes
var validIdStructure = new RegExp(/(AW|DC|UA)-[0-9]{8,}(-[0-9]{1,})?/);
// Push IDs into dataLayer and set initial gtag/js?id= script to page using first ID in googleIds array
for (var i = 0; i < googleIds.length; i++) {
var gtagId = googleIds[i];
// Validate that the ID being passed isn't a big weirdo
var idIsValid =
typeof gtagId === "string" && gtagId.match(validIdStructure);
if (idIsValid) {
window.gtag("config", gtagId);
// NOTE: gtag script only needs to be set to page once, but each gtag('config', <ID>) that's pushed to the dataLayer will add subsequent gtag/js?id=<ID> scripts to the page
if (!gtagScriptExists) {
// Set initial gtag/js?id=<first ID> script to <head>
var script = document.createElement("script");
script.type = "text/javascript";
script.async = true;
script.src = "//www.googletagmanager.com/gtag/js?id=" + gtagId;
document.getElementsByTagName("head")[0].appendChild(script);
// Update gtag/js?id= script status flag so this initialization script is only set for the first ID, and not all the IDs in the array
gtagScriptExists = true;
}
}
}
Yes, that is correct according to documentation. But it generated no data for me on the subsequent codes until I added
<script async src="https://www.googletagmanager.com/gtag/js?id=ANOTHER_CODE"></script>
Immediately above the code block. Either I stumbled on a working kludge or Google needs to update their documentation.
Yes. You can add multiple accounts, and send to all of them or send individually.
Setup
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXX-1">
</script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
// Global configs
gtag('config', 'G-XXXXXX-1');
gtag('config', 'AW-YYYYYY');
gtag('config', 'DC-ZZZZZZ');
</script>
And this is the place when firing events
<script>
// This is place firing event
// Send to all
// Send to one: Measure Google Ads conversions
gtag('event', 'conversion', {
'send_to': 'AW-YYYYYY/AbC-D_efG-h12_34-567',
'value': 1.0,
'currency': 'USD'
});
</script>
That seems to be the official way to do it according to the documentation.

How to parse a Javascript variable inside HTML <script> by it's name in R?

I would like to retrieve the content of a Javascript variable inlined in an HTML webpage.
I can retrieve the <script> node by it's position but I would like to know if there is a more flexible approach that could look at the variable name itself - the node position may change.
For example the page URL is https://poloniex.com/marginTrading and I'm looking for the content of the variable markets_currencies which contains all the currencies that can be lended. This information is not available through the API.
<script type="text/javascript">
var loggedIn = false;
var dark = false;
var mobile = false;
var mobileDetected = false;
var usid = false;
var markets = {};
var markets_currencies = {"bySymbol":{"1CR":{"id":1,"symbol":"1CR","name":"1CRedit","canLend":0},"ABY":{"id":2,"symbol":"ABY","name":"ArtByte","canLend":0} ... }};
if (window.top !== window.self) window.top.location.replace(window.self.location.href);
</script>
Until now I retrieve the webpage like this :
html <- getURL("https://poloniex.com/marginTrading", followlocation = TRUE)
doc = htmlParse(html, asText=TRUE)
plaintext <- xpathSApply(doc, "//script", xmlValue)
cat(paste(plaintext[[5]], collapse = "\n"))
But until going further I realize that the <script> node position where the variable is stored may change and it's not a flexible solution. What would be the best approach to achieve this ?
Thank you,

How to insert a variable from the DataLayer into custom HTML in Google Tag Manager?

I have a DataLayer variable holding the sum of a purchase. I wish to insert this variable into a custom HTML tag. The value variable in the following code should hold the value of the DataLayer variable.
<!-- Facebook Conversion Code for Checkouts -->
<script>(function() {
var _fbq = window._fbq || (window._fbq = []);
if (!_fbq.loaded) {
var fbds = document.createElement('script');
fbds.async = true;
fbds.src = '//connect.facebook.net/en_US/fbds.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(fbds, s);
_fbq.loaded = true;
}
})();
window._fbq = window._fbq || [];
window._fbq.push(['track', '', {'value':'0.00','currency':'USD'}]);
</script>
You can insert datalayer variables by direct reference through double curly braces, {{value}}. Make sure it is defined as a datalayer variable first.

Using Google AdWords Conversion Tracking in Meteor

I want to embed the Google Adwords Conversion Tracking Code into my Meteor app:
<script type="text/javascript">
/* <![CDATA[ */
var google_conversion_id = XXX;
var google_conversion_language = "en";
var google_conversion_format = "3";
var google_conversion_color = "ffffff";
var google_conversion_label = "XXXX";
var google_remarketing_only = false;
/* <![CDATA[ */
</script>
<script type="text/javascript" src="//www.googleadservices.com/pagead/conversion.js"></script>
<noscript>
<div style="display:inline;">
<img height="1" width="1" style="border-style:none;" alt="" src="//www.googleadservices.com/pagead/conversion/XXXX/?label=XXXX&guid=ON&script=0"/>
</div>
</noscript>
I tried to put it right into the html code of a template and I tried to append it to the html after the template rendered:
Template.success_page.rendered = function () {
$("body").append(
'.....');
}
Both ways do not work. The Tag Assistant Chrome Plugin gives me the error "No HTTP response detected".
And on the browser console I can see
"Failed to execute 'write' on 'Document': It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened."
How do I have to embed it to make it work?
I just integrated it using this:
https://developers.google.com/adwords-remarketing-tag/asynchronous/
It is coming from a remarketing perspective, but the conversion tag and the remarketing tag are basically the same thing.
Just set "google_remarketing_only" to false

Resources