Search code examples
adobe-analytics

how to track the social media icons using DTM (Dynamic tag manager)


I have the below code in my web site.

I want to track each anchor tag using DTM. I know how to track single element. Since here we have a bunch of different elements, can anyone help how to track them using DTM? I don't want to create separate rule for each element. In a single rule how can we track these elements.


Solution

  • Here is an example of what you can do.

    For Element Tag or Selector put "a.at-share-btn" (no quotes). This will target all the relevant links first. We can look for this too in the next step, but "pre-qualifying" it with this will improve performance so that the rule is not evaluated against every single a click.

    Then, under Rule Conditions, add a Criteria of type Data > Custom.

    In the Custom box, add the following:

    var shareType = this.getAttribute('class').match(/\bat-svc-([a-z_-]+)/i);
    if (shareType&&shareType[1]) {
      _satellite.setVar('shareType',shareType[1]);
      return true;
    }
    return false;
    

    This code looks for the class (e.g. "at-svc-facebook") and puts the last part of it (e.g. "facebook") into a data element named shareType.

    Then, you can reference it using %shareType% in any of the DTM fields. Note: because this data element is made on-the-fly, it will not show up in the auto-complete when you type it out in a field.

    Alternatively, in custom code boxes (e.g. if you are needing to reference it in a javascript/3rd party tag box), you can use _satellite.getVar('shareType')