Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Emiel 31 posts 124 karma points
    Oct 24, 2016 @ 07:24
    Emiel
    0

    Possibility to get unique id per object instance in the grid?

    Hi!

    Currently busy implementing a GTM (Google Tag Manager) solution for our client. They requested that for every element on the page an unique id has to be generated in order to let SEO do its work properly (so for example, a button with an attribute "data-gtm="uniqueID")

    However, after spending some time analyzing the possibilities with Umbraco it seems that there is not a way to find the instance of a grid element - this is needed because there are situations where buttons will have the same appearance, text and perhaps even the same href. So after trying a couple of possibilities to combine these properties I have concluded the only way is to have some sort of instance ID of the grid object.

    Does anyone has something for this?

    Thanks in advance!

  • Claushingebjerg 939 posts 2574 karma points
    Oct 27, 2016 @ 07:53
    Claushingebjerg
    0

    Hi Emiel

    As far as i know there is no way to get an id for the grid element. You could do it with javascript/jquery though.

    Something like

    buttonId = 0;
    
    $("button").each(function() {
      buttonId++
      this.id = "button" + buttonId;
    });
    
  • Emiel 31 posts 124 karma points
    Oct 28, 2016 @ 12:00
    Emiel
    100

    Using Javascript/Jquery would be to late for GTM (as GTM needs to be loaded before the page loads). Instead, even though it is not the cleanest way to do it, I made a helper class and used httpcontext for it:

        public static class GtmHelper
    {
    
        public static int GetIndex()
        {
            var gtmIndex = HttpContext.Current.Items["GtmIndex"] as int?;
            if (gtmIndex == null)
            {
                gtmIndex = 1;
            }
            else
            {
                gtmIndex++;
            }
            HttpContext.Current.Items["GtmIndex"] = gtmIndex;
    
            return gtmIndex.Value;
        }
    
    }
    

    Then for each element it is needed on I used this helper in the elements code. That works perfectly. :-)

Please Sign in or register to post replies

Write your reply to:

Draft