#GTMTips: Sort custom dimensions by index number – News Couple
ANALYTICS

#GTMTips: Sort custom dimensions by index number


Here’s a helpful #GTMTips tip for you. Have you ever had a Google Tag Manager container, where you’ve been updating Google Analytics tags over the years? And maybe these tags (and today’s Google Analytics settings variants) have been updated with an ever-expanding list of custom dimensions? And maybe this list of custom dimensions is sorted individually, because once you have more than 50 rows it doesn’t seem fun to override each row and update them so that they are sorted by the index of the custom dimensions?

Don’t worry then! I have a solution for you. It includes a small JavaScript snippet that you need to implement in your web browser’s JavaScript console, and it will sort the Custom Dimension fields for you!


X


Simmer . Newsletter

Subscribe to the Simmer newsletter to get the latest news and content from Simo Ahava right in your inbox!

Tip 77: Sort custom dimensions by index number

Here’s what you need to do.

  1. Browse to the Google Tag Manager container, and open a file Google Analytics tag or Google Analytics Settings Variable want to modify it.

  2. Make sure the tag or variable exists Modification mode, which means you can see each field as a text field that you can edit.

  3. Open the JavaScript console in your browser.

  4. Copy and paste the following snippet into the JavaScript console, and hit Enter to run it.

  5. Save the tag or variable.

Here’s the snippet, be sure to copy everyone From it (clicking on it three times should do the job):

var el=document.querySelector('[diff-field$="customDimensionSection"]');var rows=el.querySelectorAll(".simple-table-row[data-ng-repeat]");var newRows=[];rows.forEach(function(row)var inputIdx=row.querySelectorAll('input[type="text"]')[0];var inputVal=row.querySelectorAll('input[type="text"]')[1];newRows.push(idx:inputIdx.value,val:inputVal.value));newRows.sort(function(a,b)if(parseInt(a.idx)>parseInt(b.idx))return 1if(parseInt(a.idx)<parseInt(b.idx))return-1return 0);rows.forEach(function(row,i)var inputIdx=row.querySelectorAll('input[type="text"]')[0];var inputVal=row.querySelectorAll('input[type="text"]')[1];inputIdx.value=newRows[i].idx;inputVal.value=newRows[i].val;inputIdx.dispatchEvent(new Event("change"));inputVal.dispatchEvent(new Event("change")));

Just to clarify, being in Modification The situation should look like this:

Edit mode

You can easily open the JavaScript console in your browser using the hotkey combination:

  • Chrome
    • Wins: Ctrl + Shift + J
    • Mac: Cmd + Opt + J
  • fire fox
    • Wins: Ctrl + Shift + K
    • Mac: Cmd + Opt + K
  • edge
  • internet browser
    • Wins: F12, then click on the Console tab.
  • Safari
    • Mac: Cmd + Opt + C, you need to enable the “Show Develop menu in menu bar” setting in the advanced pane of Safari Preferences

The snippet essentially rewrites each field in the list of custom dimensions so that the rows are in ascending order, sorted by the index number of each custom dimension.

If the snippet stops working, please let me know in the comments below and I will update it.

Naturally, I am eagerly waiting for Sort Fields to be a built-in feature of the Google Tag Manager user interface. But until then, we can hack our way around this limitation.



Source link

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button