Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce data-ga and displayName in Google Analytics tracking #254

Merged
merged 7 commits into from
May 20, 2020

Conversation

MrOrz
Copy link
Member

@MrOrz MrOrz commented May 18, 2020

This PR sends the data-ga property or displayName of the clicked component to Google Analytics, so that we can track click of component of interest.

See the eventCategory (in non-bold text) on the right side of the screen recording:
onclick-path. The innerText of the clicked element is sent as eventLabel.

Collected events in Google analytics:
image

This fixes #237 .

Related Spec

This PR implements "成效追蹤" part of the following spec:

Google Tag Manager setup

image

User defined variable Clicked Element Path

This shows the the clicked item's component name (<ComponentName>) or data-ga property ([ga propety]) when their descendent is clicked.

function() {
  var currentNode = {{Click Element}};
  if(!currentNode) return '';

  var reactInstanceKey = Object.keys(currentNode).find(function(key){
    return key.startsWith('__reactInternalInstance$');
  });
  
  if(!reactInstanceKey) return '';

  var currentInst = currentNode[reactInstanceKey];
  var path = [];
  
  while(currentInst) {
    if(currentInst.memoizedProps &&
       currentInst.memoizedProps['data-ga'] &&
       // data-ga may be passed down to multiple levels of child components.
       // we don't want to record duplicated 'data-ga'.
       path[0] !== '[' + currentInst.memoizedProps['data-ga'] + ']') {
      path.unshift('[' + currentInst.memoizedProps['data-ga'] + ']');
    } else if(currentInst.elementType &&
       currentInst.elementType.displayName &&
       // Ignore names like "WithStyles(ForwardRef(ButtonGroup))"
       currentInst.elementType.displayName.indexOf('(') === -1) {
      path.unshift('<' + currentInst.elementType.displayName + '>');
    }
    currentInst = currentInst.return;
  }

  return path.join(' ▸ ');
}

Trigger Clicked element with path

image

Tag Click event to GA

image

@MrOrz MrOrz changed the title Introduce data-ga and displayName tracking Introduce data-ga and displayName in Google Analytics tracking May 18, 2020
@MrOrz MrOrz marked this pull request as ready for review May 18, 2020 10:54
@MrOrz MrOrz force-pushed the name-components-to-track branch from 5166453 to 4ad400d Compare May 20, 2020 05:42
@MrOrz MrOrz force-pushed the name-components-to-track branch from 4ad400d to c36969b Compare May 20, 2020 05:44
@MrOrz MrOrz merged commit 268b1ed into dev May 20, 2020
@MrOrz MrOrz deleted the name-components-to-track branch May 29, 2020 10:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Setup displayName of important component
2 participants