Google tag manager how to split an array and send the data seperate with one tag - google-analytics

Im trying to extract parameters from the URL. Then send that data to google analytics via GTM. Using only one tag instead of having mulitple tags for each parameter.
website.com/index?searched=data%7Cdata%7Cdata
The data needs to be seperated. So I've created a variable that seperates each paramater.
function () {
var x = {{URL Query}}.split("|");
var z = x.slice(1, -1);
return z;
}
And a trigger that picks up on click.
I'm fairly new to google tag manager, and i'm unsure of the process to do this. Should I work with Datalayers or do a loop with Custom HTML tag? But then i'm not sure what the code is to send my data to google analytics.

What you need to do is to create multiple variables if you want to capture different parameters in the URL to separate dimensions in Google Analytics. When looking at your code I can see that you want to get the last value separated by "|". Take your code, place it within a "Custom Javascript" variable and then in your Google Analytics tag you can just map the variable to whatever dimension you have set up for this purpose.
Lastly, if you want to pick up more parameters in the URL, just create a a new Custom javascript variable, capture the parameter then map it to another dimension in the Google Analytics tag.
Hope this helps.

Related

Google Tag Manager set field value to one of two variables

We are using Google Tag Manager. It is sending a custom dimension to Google Analytics with a value of {{fc-isSubscribedSession}} variable
The fc-isSubscribedSession variable is a DOM variable that is taken from the data-is-subscribed-session attribute on the page body.
We are migrating our platform. The old user interface makes use of the tag and the data-is-subscribed-session attribute. The new user interface is more like SPA so we are using the data layer to push the data to GTM like
window.dataLayer.push({
'newInterfaceIsSubscribedSession': current_user.is_subscribed?
});
(above is a fragment that is close enough to the real call)
Now the problem is that we must fill the dimension from fc-isSubscribedSession or from newInterfaceIsSubscribedSession.
Do you know if there is a way to fill a dimension from one of two values?
Something like
{{fc-isSusbscribedSession}} or {{newInterfaceIsSubscribedSession}}

Google Analytics 4 / Custom dimension with multiple values per event

Im trying to extend Google Analytics 4 reports with data from the contact form submission.
Im sending event with a custom data through dataLayer to Google Tag Manager. Then GTM sends this to Google Analytics 4.
window.dataLayer.push({
event: 'formSubmission',
formData: {
lead_name: 'Lorem',
lead_email: 'lorem#ipsum.com',
lead_areas: ['web app design', 'mobile development']
}
});
I've correctly registered all custom dimensions and my data proceed correctly.
However, when im trying to create the report using lead_areas, Im facing a trouble that my areas listed as a string with comma-separated values instead of single values.
I don't have much experience in GA4. Please help me understand,
How can I send multiple values for one custom dimension in the scope of single event?
Thanks!
There are two type of custom parameters: text or number. So you can't send an array as value of a parameter but you can send different parameters like lead_area1, lead_area2, etc...

Google Analytics, AMP and Tag Manager, dynamic client calculated metric

I want Google Analytics to send a client calculated value on AMP. I also use Google Tag Manager to embed analytics code on the page.
I want to send a value that is the difference between a time (unix) and current time on device. There's a variable on Tag Manager named "{{Client Timestamp}}" and I define server side the other date. But any time I send the value, get's turned to string (no matter if it's custom metric or custom dimension), for instance:
cm3: 1582720980844-1582641262000
Do you know any way I can achieve this? Also tried with a generated hidden element with the value calculated, but I can't manage the set the correct value on this element without user interaction.
<amp-state id="my_data">
<script type="application/json">
{
"my_date": SERVER_GENERATED_TIMESTAMP
}
</script>
</amp-state>
<span class="element_to_send_from_tag_manager" [text]="Date.now() - my_date">sample value</span>
You are trying to make a calculation inside a text field, that's why GTM is treating it as a string no matter what you do. If you want to run a calculation that is the difference of time between those two variables, use a custom javascript variable and push the result of that inside your secondary dimension.
Example of custom JS variable in GTM:
function() {
var result = {{variable1}} - {{variable2}};
return result;
}
EDIT:
Since we are talking about an AMP container, we cannot do the subtraction inside GTM itself, what we can use instead is the Calculated Metrics functionality inside GA itself.

Can't see the Custom Variables on Google Analytics

I want to add a Custom Variables called Custom Variables to my website.And I defined a datalayer on Google Tag Manager.I also add the code snippet on the page which I want to get the Custom Variable.
<script type="text/javascript">
var external_id = jQuery('#edit-external-id').attr("value");
dataLayer = [{
'externalID': external_id
}];
</script>
I debuged the configuration of GTM.Here's the picture
I got the message.But I can't see the value statics on my Google Analytics.I think the menu path of GA is Audience-Custom-Custom Variable.Am I right?Anyone can help me?
Writing values to the dataLayer does not make them appear in Google Analytics; it just makes them available in GTM.
You need to set up a Google Analytics Tag - there is a tag template for Universal Analytics where you can insert the Google Account id. Use the default "all pages" trigger. Since you are receiving data I assume you already have done this.
Now in GTM go to the "variables" menu, click "new" and select "Data Layer Variable" as type. Enter the key from the dataLayer that you want to be evaluated, i.e. "externalID". Give your variable a name, I suggest to simply use the dataLayer key. Save. Now you can use the variable by calling it via its name surrounded by {{curly brackets}}.
Go back to your GA tag. I assume you want to save the value as custom dimension, since custom variables are deprecated in the current GA version. You need to create a custom dimension in Admin/Property/Custom Definitions/Custom Dimensions. When you create a custom dimension you will get a numeric index to address the dimension by. Go to your GA tag, advanced configuration, custom dimensions and click "add". Enter the numeric index and after that select the variable you want to send.
Custom variables do not show up by default in the standard reports; you can either selected them as "secondary dimension" from the dropdown above the data table, or create a custom report.
First, you must never, ever use dataLayer = [{...}] after the container snippet (preferably don't use it ever). You're overwriting the container snippet by reinitializing it as a new Array. As you can see, in your Preview panel there is no "Page View" event (should be just before DOM Ready), which means e.g. the All Pages Trigger won't work.
Second, "Custom Variables" are deprecated. They've been replaced with Custom Dimensions in Universal Analytics.
Third, did you actually add this variable/dimension to a GA Tag?
Here's how it should work:
1) In your script replace dataLayer = [{...}] with:
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
'event' : 'variablePushed',
'externalID' : external_id
});
This is the safe way to interact with dataLayer.
2) In GA Admin / Property Settings / Custom Definitions / Custom Dimensions, create a new Custom Dimension with the appropriate scope, and make note of the assigned Index number. If you are unfamiliar with Custom Dimensions, you have some reading to do. Start with https://support.google.com/analytics/answer/2709828?hl=en
3) Create a new Custom Event Trigger for Event Name variablePushed, name it e.g. "Event - variablePushed" (without quotes).
4) Create a new Data Layer Variable for Variable Name externalID, name it e.g. {{externalID}} (without curly braces).
5) Create a new Event Tag, with Event Category: External ID, Event Action: {{externalID}}, and set the Non-Interaction field to True.
6) Browse to More Settings -> Custom Dimensions in the Event Tag, and add a new Custom Dimension. Set the Index number from (2) and set the value to {{externalID}}.
7) Add the Trigger you created in (3) to this Event Tag.
What happens now is that when the externalID dataLayer.push() takes place, the Event Tag fires a non-interaction event to GA (so it won't affect Bounce Rate), which piggybacks the Custom Dimension with it.

Tracking by id in url with Google Analytics

I want to track views for stories on my site. I want to use Google analytics to do this. Off the bat Im thinking of doing this:
pageTracker._trackEvent('Story', 'View', 'Title of story');
But I also would prefer to track with the story id as well that is passed in the url. So if I want to run a report in GA I would like to have the option of getting stats by story title or by story id that is passed via url. Is that possible?
GA by default does not strip parameters from the URL when _trackPageview is triggered, so you will see unique pages show up in your reports. For example, these two will show up as separate entries:
/somePage.html?id=1
/somePage.html?id=2
edit:
Okay, you can use this to get whatever url parameter you want:
function getParam (n) {
var x=new RegExp("[\\?&]"+n.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]")+"=([^&#]*)");
var r=x.exec(window.location.href);
return(r==null)?'':r[1] ;
}
// example
var story = getParam('story'));
now story has whatever value the story=xxx URL parameter value is, and you can use story as your category value in your event tracking argument

Resources