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

Unsure about Auto Tracking Usage #3271

Closed
janhesters opened this issue May 13, 2019 · 3 comments
Closed

Unsure about Auto Tracking Usage #3271

janhesters opened this issue May 13, 2019 · 3 comments

Comments

@janhesters
Copy link

** Which Category is your question related to? **
Analytics

** What AWS Services are you utilizing? **
Amazon Pinpoint

** Provide additional details e.g. code snippets **
In this question
I asked about tracking with Amplify and got referred to Analytics. I'm currently teaching myself how to use these because I'm having trouble understanding the docs.

I'm using React. I have two questions. 1. Where am I supposed to add the code for the tracking? 2. Is there a way to identify which user created the event?

Here is what I'm currently trying:

async function track() {
  try {
    const user = await Auth.currentAuthenticatedUser();
    const userId = user.username;
    Analytics.autoTrack('pageView', {
      enable: true,
      attributes: { userId },
      type: 'SPA',
    });
  } catch (error) {
    console.log(error);
  }
}

function useTracking() {
  useEffect(() => {
    track();
  }, []);
}

function Home() {
  useTracking();
  return <h2>Home</h2>;
}

function Notes() {
  useTracking();
  return <h2>Notes</h2>;
}

function App() {
  return (
    <Router>
      <div>
        <nav>
          <ul>
            <li>
              <Link to="/">Home</Link>
            </li>
            <li>
              <Link to="/notes/">Notes</Link>
            </li>
          </ul>
        </nav>

        <Route path="/" exact component={Home} />
        <Route path="/notes/" component={Notes} />
      </div>
    </Router>
  );
}

But this doesn't fully work. The route is being recorded, but not the user. Are you supposed to call autoTrack in componentDidMount() / useEffect? Because I tried adding it below my config outside of a Hook and that didn't even record any events. And do I have to track the user like I did under the attributes key? If yes, why could my tracking not be working? If no, how are you supposed to identify users? I'm using Amplify's Auth with Cognito User Pools.

@janhesters
Copy link
Author

Solved it, gonna write a blog post about it and link it here.

@jesper-bylund
Copy link

Hows the blogpost coming @janhesters ?

@github-actions
Copy link

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels or Discussions for those types of questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 26, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants