How to unset values in Google Analytics? - google-analytics

I use google analytics for websites and mobile apps.
Sometimes i send some custom dimensions
var dimensionValue = 'SOME_DIMENSION_VALUE';
ga('set', 'dimension1', dimensionValue);
After some user actions i want to unset this values, how it can be done?

You can't really unset dimensions.
If your dimensions are hit-scoped you do not need to, they only pertain to the interactions following the set calls on the same page.
If your dimensions are session-scoped only the last value is stored, so "unsetting" them would be the same as not setting them at all. There is no real unset mechanism.
If your dimensions are user-scoped then you can change the value, but not remove it.
So GA does not have a real "unset" mechanism. The usual workaround is to define a "neutral" value and send that when to other value applies, but you still need to think of the scope restrictions mentioned above.

You can actually send the values without setting'em in your datalayer by attaching it after the rest of event fields
var mydata = {
dimesion3: 'hello',
dimension5: 'world'
};
ga('myproperty.send','event','category','action','label','value',mydata);
If you don't actually want to set some of the above fields just remove them.
ga('myproperty.send','event','category','action',mydata);

Related

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.

What is the correct syntax to set a custom dimension in Google Analytics?

I'd like to send some custom dimensions back to GA with every blog post that is read on my website - author and category.
When I set up the custom dimensions in GA I got this code to use:
var dimensionValue = 'SOME_DIMENSION_VALUE';
ga('set', 'dimension2', dimensionValue);
However, in the GA docs it specifies a different syntax using "send" rather than "set".
https://developers.google.com/analytics/devguides/collection/analyticsjs/custom-dims-mets
ga('send', 'pageview', { 'dimension4': '<?=$categories?>'});
We are using universal google analytics, but this conflicting information means I'm not sure which syntax to use.
Thanks in advance
The first code block is how you set the custom dimension (CD) that can be sent with any hit: event, pageview, transaction, etc.
The second code block is how you set the CD and send it with a pageview. So it's a specific example of the first method.
Both are valid, it's just that the second example is more complete.
When sending data to CDs, don't forget to create and define them in the GA configuration as well.

CustomDimension send via ga('set',...)

I have two buttons, to set gender.
For each gender button I've set up a customDimension like ga('set', 'dimension1', 'male'); or ga('set', 'dimension1', 'female');
In the Javascript Console from FireBug I see that the ga() command is executed without errors. In GoogleAnalytics I have set a customDimension
Must I send this data specific via ga('send',...... or is this not needed??
If yes, which parameters do I have to set?
I can't see any received Data in GoogleAnalytics GUI and I have waited about 48 hours.
Custom Metrics and Dimensions must always be sent with an interaction hit, else they will not get recorded.
In addition, fields set with ga('set'... must be followed by an interaction hit - "set" in this case literally means "set this field for subsequent use in interaction hits". The difference between using set and passing the custom dimensions via the configuration object of an interaction hit (pageviews, events etc) is that "set" will affect all following hits while passing the custom dimension/metric as a parameter to a hit will only affect that specific hit.
So if you use ga('set', 'dimension1', 'male') and you have after that one pageview and two events the dimension will be recorded three times (not so much a problem with custom dimensions, potentially a big problem with custom metrics).
If you do instead:
ga('send', 'pageview', {
'dimension1': 'male'
});
the dimension will be send only once.
But no matter how you do it, the data will only be sent along hit data, so you need a pageview, event or transaction if you want any results.

Google Analytics Get Custom Dimension After Setting One

In Google Analytics, after setting a custom dimension via:
var tracker = ga.getAll()[0];
tracker.set('dimension1', 'myCustomValue');
tracker.send('pageview');
I'd like to then GET the custom dimension value for that user when a user navigates to another page, via something like:
var tracker = ga.getAll()[0];
var dimension1_value = tracker.get('dimension1'); // 'myCustomValue'
Is there a way to do this?
There is no way native to Google Analytics to do this. Values are not stored in cookies anymore and there is no backchannel to the analytics server, get after set only works on the same page.
But of course nothing stops you from setting your own cookie/session storage and store the value(s) there (but then you knew that).

Resources